<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="100px" size="small">
      <!--      <el-form-item label="批次标识" prop="batchFlag">-->
      <!--        <el-select v-model="queryParams.batchFlag" placeholder="请选择批次标识" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.active_flag"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="条码类型" prop="barcodeType">-->
      <!--        <el-select v-model="queryParams.barcodeType" placeholder="请选择条码类型" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.barcode_type"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="销售订单编号" prop="saleorderCode">
        <el-input
          v-model="queryParams.saleorderCode"
          clearable
          placeholder="请输入销售订单编号"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mes:barcode:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['mes:barcode:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mes:barcode:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['mes:barcode:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :columns="columns" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="barcodeList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column v-if="columns[0].visible" align="center" label="主键标识" prop="barcodeId"/>
      <el-table-column v-if="columns[4].visible" align="center" label="条码类型" prop="barcodeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.barcode_type" :value="scope.row.barcodeType"/>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[17].visible" align="center" label="生产计划编号" prop="planCode" width="110"/>
      <el-table-column v-if="columns[20].visible" align="center" label="销售订单编号" prop="saleorderCode" width="110"/>
      <el-table-column v-if="columns[13].visible" align="center" label="采购订单号" prop="poNo" width="100"/>
      <el-table-column v-if="columns[6].visible" align="center" label="批次代码" prop="batchCode" width="100"/>
      <el-table-column v-if="columns[5].visible" align="center" label="条码内容" prop="barcodeInfo" width="100"/>
      <el-table-column v-if="columns[3].visible" align="center" label="批次标识" prop="batchFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.active_flag" :value="scope.row.batchFlag"/>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[8].visible" align="center" label="物料名称" prop="materialName" width="100"/>
      <el-table-column v-if="columns[9].visible" align="center" label="供应商ID" prop="manufacturerId"/>
      <el-table-column v-if="columns[10].visible" align="center" label="数量" prop="amount"/>
      <el-table-column v-if="columns[14].visible" align="center" label="生产日期" prop="productionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[15].visible" align="center" label="接受日期" prop="acceptedDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acceptedDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[16].visible" align="center" label="最晚出库日期" prop="lastOutstockDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastOutstockDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[7].visible" align="center" label="托盘RFID代码" prop="palletInfoCode" width="110"/>
      <el-table-column v-if="columns[11].visible" align="center" label="打印机台名称" prop="machineName" width="110"/>
      <el-table-column v-if="columns[12].visible" align="center" label="打印次数" prop="printNumber"/>
      <el-table-column v-if="columns[2].visible" align="center" label="打印人" prop="printPerson"/>
      <el-table-column v-if="columns[1].visible" align="center" label="打印时间" prop="printTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.printTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[18].visible" align="center" label="生产计划明细编号" prop="planDetailCode"/>
      <el-table-column v-if="columns[19].visible" align="center" label="销售订单ID" prop="saleOrderId"/>
      <el-table-column v-if="columns[21].visible" align="center" label="项目号" prop="projectNo"/>
      <el-table-column v-if="columns[22].visible" align="center" label="流水号" prop="serialNumber"/>
      <el-table-column v-if="columns[23].visible" align="center" label="备注" prop="remark"/>
      <el-table-column v-if="columns[24].visible" align="center" label="绑定状态" prop="bindStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bind_status" :value="scope.row.bindStatus"/>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[25].visible" align="center" label="绑定托盘的人" prop="bindBy" width="110"/>
      <el-table-column v-if="columns[26].visible" align="center" label="绑定托盘时间" prop="bindTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bindTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns[27].visible" align="center" label="更新人" prop="updateBy"/>
      <el-table-column v-if="columns[28].visible" align="center" label="更新时间" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-printer"
            size="mini"
            type="text"
            @click="handlePrintBarCode(scope.row)"
          >打印
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['mes:barcode:edit']"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['mes:barcode:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改条码信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="生产计划编号" prop="planCode">
          <el-input v-model="form.planCode" placeholder="请点击右侧检索生产计划编号" readonly>
            <el-button slot="append" icon="el-icon-search" @click="handleProductPlanAdd"></el-button>
          </el-input>
        </el-form-item>
<!--        <el-form-item label="销售订单编号" prop="saleorderCode">-->
<!--          <el-input v-model="form.saleorderCode" :disabled="true" placeholder="请输入销售订单编号"/>-->
<!--        </el-form-item>-->
        <!--        <el-form-item label="条码类型" prop="barcodeType">-->
        <!--          <el-radio-group v-model="form.barcodeType">-->
        <!--            <el-radio-->
        <!--              v-for="dict in dict.type.barcode_type"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.value"-->
        <!--            >{{dict.label}}</el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="条码内容" prop="barcodeInfo">-->
        <!--          <el-input v-model="form.barcodeInfo" placeholder="请输入条码内容" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="批次代码" prop="batchCode">-->
        <!--          <el-input v-model="form.batchCode" placeholder="请输入批次代码" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="托盘RFID代码" prop="palletInfoCode">-->
        <!--          <el-input v-model="form.palletInfoCode" placeholder="请输入托盘RFID代码" />-->
        <!--        </el-form-item>-->
        <el-form-item v-if="false" label="物料ID" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料ID"/>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" :disabled="true" placeholder="请输入物料名称"/>
        </el-form-item>
        <!--        <el-form-item label="供应商ID" prop="manufacturerId">-->
        <!--          <el-input v-model="form.manufacturerId" placeholder="请输入供应商ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="数量" prop="amount">
          <el-input-number v-model="form.amount" :min="0" placeholder="请输入数量"/>
        </el-form-item>

        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker v-model="form.productionDate"
                          clearable
                          placeholder="请选择生产日期"
                          type="date"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="接受日期" prop="acceptedDate">-->
        <!--          <el-date-picker clearable-->
        <!--                          v-model="form.acceptedDate"-->
        <!--                          type="date"-->
        <!--                          value-format="yyyy-MM-dd"-->
        <!--                          placeholder="请选择接受日期">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <el-form-item label="最晚出库日期" prop="lastOutstockDate">
          <el-date-picker v-model="form.lastOutstockDate"
                          clearable
                          placeholder="请选择最晚出库日期"
                          type="date"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
        <!--                <el-form-item label="绑定状态" prop="bindStatus">-->
        <!--                  <el-radio-group v-model="form.bindStatus">-->
        <!--                    <el-radio-->
        <!--                      v-for="dict in dict.type.bind_status"-->
        <!--                      :key="dict.value"-->
        <!--                      :label="dict.value"-->
        <!--                    >{{dict.label}}</el-radio>-->
        <!--                  </el-radio-group>-->
        <!--                </el-form-item>-->
        <!--                <el-form-item label="绑定托盘的人" prop="bindBy">-->
        <!--                  <el-input v-model="form.bindBy" placeholder="请输入绑定托盘的人" />-->
        <!--                </el-form-item>-->
        <!--                <el-form-item label="绑定托盘时间" prop="bindTime">-->
        <!--                  <el-date-picker clearable-->
        <!--                                  v-model="form.bindTime"-->
        <!--                                  type="date"-->
        <!--                                  value-format="yyyy-MM-dd"-->
        <!--                                  placeholder="请选择绑定托盘时间">-->
        <!--                  </el-date-picker>-->
        <!--                </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加生产计划编号对话框 -->
    <el-dialog :visible.sync="productPlanOpen" append-to-body title="选择生产计划编号">
      <add-ProductPlan ref="productPlanRef" :defineData="defineData" @selection="handleSelection"></add-ProductPlan>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProductPlanForm">确 定</el-button>
        <el-button @click="productPlanOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBarcode, getBarcode, delBarcode, addBarcode, updateBarcode, printBarCode} from "@/api/mes/barcode";
import addProductPlan from '@//views/mes/productplan/addProductPlan.vue';

export default {
  name: "Barcode",
  dicts: ['bind_status', 'active_flag', 'barcode_type'],
  components: {
    'add-ProductPlan': addProductPlan,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      barcodeInfos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 条码信息表格数据
      barcodeList: [],
      // 是否显示添加生产计划弹出层
      productPlanOpen: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        printTime: null,
        printPerson: null,
        batchFlag: null,
        barcodeType: '3',
        barcodeInfo: null,
        batchCode: null,
        palletInfoCode: null,
        materialId: null,
        manufacturerId: null,
        amount: null,
        machineName: null,
        printNumber: null,
        poNo: null,
        productionDate: null,
        acceptedDate: null,
        lastOutstockDate: null,
        planCode: null,
        planDetailCode: null,
        saleOrderId: null,
        saleorderCode: null,
        projectNo: null,
        serialNumber: null,
        bindStatus: null,
        bindBy: null,
        bindTime: null,
        updateBy: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderCode: [
          {required: true, message: "工单编号不能为空", trigger: "blur"}
        ],
        batchFlag: [
          {required: true, message: "批次标识不能为空", trigger: "change"}
        ],
        barcodeType: [
          {required: true, message: "条码类型不能为空", trigger: "change"}
        ],
        materialId: [
          {required: true, message: "物料ID不能为空", trigger: "blur"}
        ],
      },
      columns: [
        {key: 0, label: `主键标识`, visible: false},
        {key: 1, label: `打印时间`, visible: true},
        {key: 2, label: `打印人`, visible: true},
        {key: 3, label: `批次标识`, visible: true},
        {key: 4, label: `条码类型`, visible: false},
        {key: 5, label: `条码内容`, visible: true},
        {key: 6, label: `批次代码`, visible: true},
        {key: 7, label: `托盘RFID代码`, visible: true},
        {key: 8, label: `物料名称`, visible: true},
        {key: 9, label: `供应商ID`, visible: false},
        {key: 10, label: `数量`, visible: true},
        {key: 11, label: `打印机台名称`, visible: true},
        {key: 12, label: `打印次数`, visible: true},
        {key: 13, label: `采购订单号`, visible: false},
        {key: 14, label: `生产日期`, visible: true},
        {key: 15, label: `接受日期`, visible: true},
        {key: 16, label: `最晚出库日期`, visible: true},
        {key: 17, label: `生产计划编号`, visible: true},
        {key: 18, label: `生产计划明细编号`, visible: false},
        {key: 19, label: `销售订单ID`, visible: false},
        {key: 20, label: `销售订单编号`, visible: true},
        {key: 21, label: `项目号`, visible: false},
        {key: 22, label: `流水号`, visible: false},
        {key: 23, label: `备注`, visible: true},
        {key: 24, label: `绑定状态`, visible: true},
        {key: 25, label: `绑定托盘的人`, visible: true},
        {key: 26, label: `绑定托盘时间`, visible: true},
        {key: 27, label: `更新人`, visible: false},
        {key: 28, label: `更新时间`, visible: false},
      ],
    };
  },
  created() {
    this.getList();
  },
  props: {
    defineData: {
      type: Object,
      default: {}
    }
  },
  // watch: {
  //   'form.batchFlag': function (newVal, oldVal) {
  //     // 在 form.batchFlag 变化时执行
  //     if (newVal === '1') {
  //       this.amountVisible = true;
  //     } else if (newVal === '0') {
  //       this.amountVisible = false;
  //     }
  //   },
  // },
  methods: {
    getData() {
      this.form = {
        planCode: this.defineData.planCode,
        saleorderCode: this.defineData.saleorderCode,
        materialId: this.defineData.materialId,
        materialName: this.defineData.materialName,
        batchFlag:'0',
        barcodeType:'3',
        saleOrderId:null,

      }
    },
    /** 查询条码信息列表 */
    getList() {
      this.loading = true;
      listBarcode(this.queryParams).then(response => {
        this.barcodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        barcodeId: null,
        printTime: null,
        printPerson: null,
        batchFlag: '0',
        barcodeType: '3',
        barcodeInfo: null,
        batchCode: null,
        palletInfoCode: null,
        materialId: null,
        manufacturerId: null,
        amount: 1,
        machineName: null,
        printNumber: null,
        poNo: null,
        productionDate: null,
        acceptedDate: null,
        lastOutstockDate: null,
        planCode: null,
        planDetailCode: null,
        saleOrderId: null,
        saleorderCode: null,
        projectNo: null,
        serialNumber: null,
        remark: null,
        bindStatus: null,
        bindBy: null,
        bindTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.barcodeId)
      this.barcodeInfos = selection.map(item => item.barcodeInfo)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.barcodeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 提交采购订单按钮 */
    submitProductPlanForm() {
      let selectedRow = this.$refs.productPlanRef.selectedRow;
      this.form.barcodeType = '3';//条码类型（1原材料,2半成品,3成品,4背板）
      this.form.planCode = selectedRow.planCode;
      this.form.materialId = selectedRow.materialId;
      this.form.materialName = selectedRow.materialName;
      this.form.saleOrderId = selectedRow.saleOrderId;
      this.form.saleorderCode = selectedRow.saleorderCode;
      this.form.batchFlag = '0';
      this.productPlanOpen = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if (this.defineData?.planCode) {
        this.getData()
      }
      this.open = true;
      this.title = "添加成品条码信息";
    },

    /** 打印条码按钮操作 */
    handlePrintBarCode(row) {
      const barcodeIds = row.barcodeId || this.ids;
      printBarCode(barcodeIds).then(response => {
        this.$modal.msgSuccess("打印条码成功");
      }).catch(() => {

      });
    },
    /** 新增销售订单按钮操作 */
    handleProductPlanAdd() {
      this.productPlanOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const barcodeId = row.barcodeId || this.ids
      getBarcode(barcodeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成品条码信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.barcodeId != null) {
            updateBarcode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBarcode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const barcodeIds = row.barcodeId || this.ids;
      const barcodeInfos = row.barcodeInfo || this.barcodeInfos;
      this.$modal.confirm('是否确认删除成品条码内容为"' + barcodeInfos + '"的数据项？').then(function () {
        return delBarcode(barcodeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/barcode/export', {
        ...this.queryParams
      }, `barcode_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
