<!--
 * @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="$t('MN_F_40')">
              <el-input
                v-model="select.productCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_39')">
              <el-input
                v-model="select.productName"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_381')">
              <el-input
                v-model="select.prefix"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_382')">
              <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="$t('MN_F_T_384')">
              <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="$t('MN_F_T_386')">
              <el-input
                v-model="select.tailNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <!-- 二层 -->
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_388')">
              <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="$t('MN_F_T_390')">
              <el-input
                v-model="select.endNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_391')">
              <el-input
                v-model="select.currentNumber"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_392')">
              <el-input
                v-model="select.totalBarCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_393')">
              <el-input
                v-model="select.usedBarCode"
                clearable
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="$t('MN_F_T_394')">
              <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="$t('w_0445')">
                  <el-date-picker
                    v-model="creationTime1"
                    type="daterange"
                    :range-separator="$t('w_0139')"
                    :start-placeholder="$t('CK_KS_4')"
                    :end-placeholder="$t('CK_KS_5')"
                    @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"
            > {{ $t('MN_T_2') }}</el-button>
            <el-button class="my_reset" @click="reset"> {{ $t('MN_T_3') }}</el-button>
          </el-col>
        </el-row>
        <div
          class="openClose"
          @click="changeActive"
        >
          <i :class="isActive ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" />
          <div>{{ isActive ? $t('MN_F_T_545') : $t('MN_F_T_546') }}</div>
        </div>
      </el-form>

      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="small"
            class="thebtn1"
            @click="openDig"
          >{{ $t('w_0328') }}</el-button>
          <el-button
            size="small"
            class="thebtn2"
            @click="toCopy"
          >{{ $t('MN_F_T_395') }}</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="$t('MN_F_40')"
          />
          <el-table-column
            align="center"
            prop="productName"
            :label="$t('MN_F_39')"
          />
          <el-table-column
            align="center"
            prop="prefix"
            :label="$t('MN_F_T_381')"
          />
          <el-table-column
            align="center"
            prop="isYear"
            :label="$t('MN_F_T_382')"
          >
            <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="$t('MN_F_T_384')"
          >
            <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="$t('MN_F_T_386')"
          />
          <el-table-column
            align="center"
            prop="isInitialize"
            :label="$t('MN_F_T_388')"
          >
            <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="$t('MN_F_T_390')"
          />
          <el-table-column
            align="center"
            prop="currentNumber"
            :label="$t('MN_F_T_391')"
          />
          <el-table-column
            align="center"
            prop="totalBarCode"
            :label="$t('MN_F_T_392')"
          />
          <el-table-column
            align="center"
            prop="usedBarCode"
            :label="$t('MN_F_T_393')"
          />
          <el-table-column
            align="center"
            prop="enableState"
            :label="$t('MN_F_T_376')"
          >
            <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="$t('MN_F_T_394')"
          />
          <el-table-column
            align="center"
            prop="arrivalTime"
            :label="$t('w_0445')"
          />
          <el-table-column
            align="center"
            prop="creationTime"
            :label="$t('PER_DA_9')"
          />
          <el-table-column
            align="center"
            prop="time"
            width="120"
            :label="$t('MY_ORD_14')"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="btnHeng">
                <el-button
                  type="text"
                  size="small"
                  style="color: #6962F6"
                  @click="toChecked(scope.row.pkId)"
                >
                  {{$t('ENU_CONTROL_TYPE_2')}}
                </el-button>
                <el-button
                  type="text"
                  size="small"
                  style="color: #E70DEC"
                  @click="openDetails(scope.row.pkId)"
                >
                  {{$t('MN_F_T_403')}}
                </el-button>
                <el-button
                  type="text"
                  size="small"
                  style="color: #c73030"
                  @click="handleDelete(scope.row.pkId)"
                >
                  {{$t('CK_KS_47')}}
                </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?$t('w_0328'):$t('ENU_CONTROL_TYPE_2')"
      :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="$t('MN_F_40')"
              prop="productCode"
            >
              <el-input
                v-model="form.productCode"
                clearable
                :placeholder="$t('S_C_70')"
                @blur="getProduct"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item :label="$t('MN_F_39')">
              <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="$t('MN_F_T_381')"
              prop="prefix"
            >
              <el-input
                v-model="form.prefix"
                clearable
                :placeholder="$t('S_C_70')"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item
              :label="$t('MN_F_T_382')"
              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="$t('MN_F_T_384')"
              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="$t('MN_F_T_386')"
              prop="tailNumber"
            >
              <el-input
                v-model="form.tailNumber"
                clearable
                :placeholder="$t('S_C_70')"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="$t('MN_F_T_388')"
              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="$t('S_C_70')"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;">
          <el-col :span="11">
            <el-form-item
              :label="$t('MN_F_T_391')"
              prop="currentNumber"
            >
              <el-input
                v-model="form.currentNumber"
                clearable
                :placeholder="$t('S_C_70')"
              />
            </el-form-item>
          </el-col>
          <el-col
            :span="11"
            style="margin-left: 50px"
          >
            <el-form-item
              :label="$t('MN_F_T_394')"
              prop="batch"
            >
              <el-input
                v-model="form.batch"
                clearable
                :placeholder="$t('S_C_70')"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item
            :label="$t('w_0445')"
            prop="effectiveDate"
          >
            <el-date-picker
              v-model="form.effectiveDate"
              type="datetime"
              value-format="yyyy-MM-dd hh:mm:ss"
              :placeholder="$t('CK_KS_38')"
            />
          </el-form-item>
        </el-col>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          class="cancels"
          @click="handleClose"
        >{{ $t('ENU_P_TYPE0') }}</el-button>
        <el-button
          type="primary"
          class="submit"
          @click="onSubmit('form')"
        >{{ $t('ENU_P_TYPE4') }}</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="$t('MN_F_T_395')"
      :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="$t('MN_F_T_404')"
              prop="generateCount"
            >
              <el-input
                v-model="form1.generateCount"
                clearable
                :placeholder="$t('S_C_70')"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          class="cancels"
          @click="shengcheng = false"
        >{{ $t('ENU_P_TYPE0') }}</el-button>
        <el-button
          class="submit"
          type="primary"
          @click="toSubmit('form1')"
        >{{ $t('ENU_P_TYPE4') }}</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: this.$t('ENU_POPUP_TYPE_1'),
          val: 0
        },
        {
          text: this.$t('ENU_POPUP_TYPE_2'),
          val: 1
        }
      ],
      form: {},
      form1: {},
      creationTime1: [],
      isCopy: false,
      selPkId: [],
      dialogVisible: false,
      shengcheng: false,
      rules: {
        isYear: [
          { required: true, message: this.$t('MN_F_T_383'), trigger: 'change' }
        ],
        isMonth: [
          { required: true, message: this.$t('MN_F_T_385'), trigger: 'change' }
        ],
        isInitialize: [
          { required: true, message: this.$t('MN_F_T_389'), trigger: 'change' }
        ],
        effectiveDate: [
          { required: true, message: this.$t('MN_F_T_402'), trigger: 'change' }
        ],
        productCode: [
          { required: true, message: this.$t('MN_F_T_401'), trigger: 'blur' }
        ],
        prefix: [{ required: true, message: this.$t('MN_F_T_400'), trigger: 'blur' }],
        tailNumber: [
          { required: true, message: this.$t('MN_F_T_387'), trigger: 'blur' }
        ],
        endNumber: [
          { required: true, message: this.$t('MN_F_T_399'), trigger: 'blur' }
        ],
        currentNumber: [
          { required: true, message: this.$t('MN_F_T_398'), trigger: 'blur' }
        ],
        batch: [{ required: true, message: this.$t('MN_F_T_397'), trigger: 'blur' }]
      },
      rules1: {
        generateCount: [
          { required: true, message: this.$t('MN_F_T_396'), 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(this.$t('MN_F_T_303'), this.$t('w_0034'), {
        confirmButtonText: this.$t('w_0035'),
        cancelButtonText: this.$t('ENU_P_TYPE0'),
        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>
