<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2022-11-08 10:26:10
-->
<template>
  <div class="page">
    <div class="main">
      <el-form
        ref="select"
        :model="select"
        style="padding: 0px 10px 0px 0;background-color: #fff"
        label-width="100px"
      >
        <el-row :gutter="10">
          <!-- 一层 -->
          <el-col :span="4">
            <el-form-item :label="'产品编号'">
              <el-input
                v-model="select.productCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'产品名称'">
              <el-input
                v-model="select.productName"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'前缀'">
              <el-input
                v-model="select.prefix"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'包含年份'">
              <el-select
                v-model="select.isYear"
                clearable
                placeholder=""
              >
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'包含月份'">
              <el-select
                v-model="select.isMonth"
                clearable
                placeholder=""
              >
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'尾号位数'">
              <el-input
                v-model="select.tailNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <!-- 二层 -->
          <el-col :span="4">
            <el-form-item :label="'自动初始化'">
              <el-select
                v-model="select.isInitialize"
                clearable
                placeholder=""
              >
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'初始尾号'">
              <el-input
                v-model="select.endNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'当前尾号'">
              <el-input
                v-model="select.currentNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'条码总数'">
              <el-input
                v-model="select.totalBarCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'已使用总数'">
              <el-input
                v-model="select.usedBarCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'追溯批次'">
              <el-input
                v-model="select.batch"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-collapse-transition>
            <div v-show="isActive">
              <el-col :span="8">
                <el-form-item :label="'生效日期'">
                  <el-date-picker
                    v-model="creationTime1"
                    type="daterange"
                    :range-separator="'至'"
                    :start-placeholder="'开始日期'"
                    :end-placeholder="'结束日期'"
                    @change="changeTime1"
                  />
                </el-form-item>
              </el-col>
            </div>
          </el-collapse-transition>
          <el-col
            :span="4"
            style="margin-left: 30px"
          >
            <el-button
              class="my_search"
              @click="getDataList"
            > {{ '搜索' }}</el-button>
            <el-button class="my_reset" @click="reset"> {{ '重置' }}</el-button>
          </el-col>
        </el-row>
        <div
          class="openClose"
          @click="changeActive"
        >
          <i :class="isActive ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" />
          <div>{{ isActive ? '点击收起' : '点击展开' }}</div>
        </div>
      </el-form>

      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="small"
            class="thebtn1"
            @click="openDig"
          >{{ '添加' }}</el-button>
          <el-button
            size="small"
            class="thebtn2"
            @click="toCopy"
          >{{ '生成' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="660px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            align="center"
            prop="productCode"
            :label="'产品编号'"
          />
          <el-table-column
            align="center"
            prop="productName"
            :label="'产品名称'"
          />
          <el-table-column
            align="center"
            prop="prefix"
            :label="'前缀'"
          />
          <el-table-column
            align="center"
            prop="isYear"
            :label="'包含年份'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isYear ? 'isRed' : 'isGreen'">
                {{ scope.row.isYear | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isMonth"
            :label="'包含月份'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isYear ? 'isRed' : 'isGreen'">
                {{ scope.row.isYear | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="tailNumber"
            :label="'尾号位数'"
          />
          <el-table-column
            align="center"
            prop="isInitialize"
            :label="'自动初始化'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isInitialize ? 'isRed' : 'isGreen'">
                {{ scope.row.isInitialize | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="endNumber"
            :label="'初始尾号'"
          />
          <el-table-column
            align="center"
            prop="currentNumber"
            :label="'当前尾号'"
          />
          <el-table-column
            align="center"
            prop="totalBarCode"
            :label="'条码总数'"
          />
          <el-table-column
            align="center"
            prop="usedBarCode"
            :label="'已使用总数'"
          />
          <el-table-column
            align="center"
            prop="enableState"
            :label="'启用状态'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.enableState ? 'isRed' : 'isGreen'">
                {{ scope.row.enableState | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="batch"
            :label="'追溯批次'"
          />
          <el-table-column
            align="center"
            prop="arrivalTime"
            :label="'生效日期'"
          />
          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            align="center"
            prop="time"
            width="120"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="btnHeng">
                <el-button
                  type="text"
                  size="small"
                  style="color: #6962F6"
                  @click="toChecked(scope.row.pkId)"
                >
                  {{'修改'}}
                </el-button>
                <el-button
                  type="text"
                  size="small"
                  style="color: #E70DEC"
                  @click="openDetails(scope.row.pkId)"
                >
                  {{'明细'}}
                </el-button>
                <el-button
                  type="text"
                  size="small"
                  style="color: #c73030"
                  @click="handleDelete(scope.row.pkId)"
                >
                  {{'删除'}}
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
    <el-dialog
      :title="addOrEdit?'添加':'修改'"
      :visible.sync="dialogVisible"
      width="33%"
      center
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="auto"
      >
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="'产品编号'"
              prop="productCode"
            >
              <el-input
                v-model="form.productCode"
                clearable
                :placeholder="'请输入'"
                @blur="getProduct"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item :label="'产品名称'">
              <el-input
                v-model="form.productName"
                clearable
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="'前缀'"
              prop="prefix"
            >
              <el-input
                v-model="form.prefix"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item
              :label="'包含年份'"
              prop="isYear"
            >
              <el-select
                v-model="form.isYear"
                clearable
                placeholder=""
              >
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="'包含月份'"
              prop="isMonth"
            >
              <el-select
                v-model="form.isMonth"
                clearable
                placeholder=""
              >
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item
              :label="'尾号位数'"
              prop="tailNumber"
            >
              <el-input
                v-model="form.tailNumber"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="'自动初始化'"
              prop="isInitialize"
            >
              <el-select
                v-model="form.isInitialize"
                clearable
              >
                <el-option
                  v-for="item in agreeList"
                  :key="item.val"
                  :label="item.text"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item
              label="初始尾数"
              prop="endNumber"
            >
              <el-input
                v-model="form.endNumber"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="'当前尾号'"
              prop="currentNumber"
            >
              <el-input
                v-model="form.currentNumber"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item
              :label="'追溯批次'"
              prop="batch"
            >
              <el-input
                v-model="form.batch"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item
            :label="'生效日期'"
            prop="effectiveDate"
          >
            <el-date-picker
              v-model="form.effectiveDate"
              type="datetime"
              value-format="yyyy-MM-dd hh:mm:ss"
              :placeholder="'请选择'"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          class="cancels"
          @click="handleClose"
        >{{ '取消' }}</el-button>
        <el-button
          type="primary"
          class="submit"
          @click="onSubmit('form')"
        >{{ '确认' }}</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="'生成'"
      :visible.sync="shengcheng"
      width="34%"
      show-close
      center
    >
      <el-form
        ref="form1"
        :rules="rules1"
        :model="form1"
        label-width="auto"
      >
        <el-row>
          <el-col
            :span="24"
            style="padding-bottom: 170px"
          >
            <el-form-item
              :label="'生成数量'"
              prop="generateCount"
            >
              <el-input
                v-model="form1.generateCount"
                clearable
                :placeholder="'请输入'"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          class="cancels"
          @click="shengcheng = false"
        >{{ '取消' }}</el-button>
        <el-button
          class="submit"
          type="primary"
          @click="toSubmit('form1')"
        >{{ '确认' }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import * as api from '@/api/configManage.js'
import { accountSave, accountUpdate } from '@/api/configManage'
import { getToken } from '@/utils/auth'
import { specialArea } from '@/api/configManage'

export default {
  name: 'ProductList',
  components: {
    topBar
  },
  filters: {
    isAgree(val,that) {
      if (!val) {
        return that.$t('ENU_POPUP_TYPE_1');
      } else {
        return that.$t('ENU_POPUP_TYPE_2');
      }
    },
  },
  data() {
    return {
      that: this,
      // 折叠搜索框
      isActive: false,
      select: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50
      },
      addOrEdit: '',
      total: 0,
      // 套餐类型
      loading:false,
      tableData: [],
      agreeList: [
        {
          text: '允许',
          val: 0
        },
        {
          text: '禁止',
          val: 1
        }
      ],
      form: {},
      form1: {},
      creationTime1: [],
      isCopy: false,
      selPkId: [],
      dialogVisible: false,
      shengcheng: false,
      rules: {
        isYear: [
          { required: true, message: '请选择包含年份', trigger: 'change' }
        ],
        isMonth: [
          { required: true, message: '请选择包含月份', trigger: 'change' }
        ],
        isInitialize: [
          { required: true, message: '请选择自动初始化', trigger: 'change' }
        ],
        effectiveDate: [
          { required: true, message: '请选择生效时间', trigger: 'change' }
        ],
        productCode: [
          { required: true, message: '请输入正确的产品编码', trigger: 'blur' }
        ],
        prefix: [{ required: true, message: '请输入前缀', trigger: 'blur' }],
        tailNumber: [
          { required: true, message: '请输入尾号位数', trigger: 'blur' }
        ],
        endNumber: [
          { required: true, message: '请输入初始位数', trigger: 'blur' }
        ],
        currentNumber: [
          { required: true, message: '请输入当前尾数', trigger: 'blur' }
        ],
        batch: [{ required: true, message: '请输入追溯批次', trigger: 'blur' }]
      },
      rules1: {
        generateCount: [
          { required: true, message: '请输入生成数量', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // 获取列表
    this.getDataList()
  },
  methods: {
    reset() {
      this.select = {}
      this.queryParams = {
        pageNum: 1,
        pageSize: 50
      }
      this.creationTime1 = []
      this.getDataList()
    },
    toSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          api
            .bar_code_itemsSave({
              pkBarCode: this.selPkId,
              generateCount: this.form1.generateCount
            })
            .then((res) => {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.shengcheng = false
              this.getDataList()
            })
        } else {
          return false
        }
      })
    },
    getProduct() {
      api
        .get_product_code({ productCode: this.form.productCode })
        .then((res) => {
          this.form.pkProduct = res.data.pkId
          // this.form.productName = res.data.productName
          this.$set(this.form, 'productName', res.data.productName)
        })
    },
    changeTime1(val) {
      this.select.startDate = val[0]
      this.select.endDate = val[1]
    },

    // 展开折叠
    changeActive() {
      this.isActive = !this.isActive
    },

    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.bd_bar_codeDel(id).then((res) => {
          // 获取列表
          this.getDataList()
        })
      })
    },
    openDig() {
      this.addOrEdit = true
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.addOrEdit == true) {
            url = api.bar_code_codeSave
          } else {
            url = api.bar_code_codeUpdate
          }
          url(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.dialogVisible = false
              this.getDataList()
            }
          })
        } else {
          return false
        }
      })
    },
    openDetails(id) {
      this.$router.push({
        path: '/configManage/barcodeConfiguration/details',
        query: {
          id: id
          // id: 2,
        }
      })
    },
    // 编辑
    toChecked(id) {
      api.bar_code_itemsGetOne(id).then((res) => {
        this.form = res.data
        this.dialogVisible = true
        this.addOrEdit = false
      })
    },
    // 获取列表
    getDataList() {
      this.loading = true
      api
        .bd_bar_codeList(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
          this.loading = false
        })
    },
    toCopy() {
      // this.toChecked(this.selPkId, 2)
      this.shengcheng = true
    },
    handleSelectionChange(val) {
      this.selPkId = val.map((item) => {
        return item.pkId
      })
      console.log(
        '%c [ this.selPkId ]-606',
        'font-size:13px; background:#be39b5; color:#ff7df9;',
        this.selPkId
      )
      // this.selPkId = val[0].pkId
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>
<style scoped lang="scss">
.submit {
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}
.cancels {
  width: 209px;
  height: 48px;
  background: #cccccc;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    background: #f9f9f9;
    margin-top: 10px;
    border-radius: 10px;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
        .thebtn3 {
          background: #6962f6;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  margin-right: 10px;
  color: #666666;
  text-align: center;
}
.btnHeng {
  display: flex;
}
</style>
