<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="85px"
    >
      <QueryFormWrapper>
        <el-col :span="6">
          <el-form-item label="物料编码" prop="materialNo">
            <el-input
              v-model="queryParams.materialNo"
              placeholder="请输入物料编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料名称" prop="materialName">
            <el-input
              v-model="queryParams.materialName"
              placeholder="请输入物料名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="检验状态" prop="testStatus">
            <DictSelect
              v-model="queryParams.testStatus"
              placeholder="请选择检验状态"
              dict-type="WMS_TEST_STATUS"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="供应商名称" prop="supplierName">
            <el-input
              v-model="queryParams.supplierName"
              placeholder="请输入供应商名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <EasyAdvancedQuery
              class="mr10"
              label-width="98px"
              @advQuery="advQuery"
            >
              <template #default="{ model }">
                <el-row :gutter="10" type="flex">
                  <el-col :span="12">
                    <el-form-item label="物料批次" prop="materialLots">
                      <el-input
                        v-model="model.materialLots"
                        placeholder="请输入物料批次"
                        clearable
                        @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="采购订单" prop="purchaseOrderNo">
                      <el-input
                        v-model="model.purchaseOrderNo"
                        placeholder="请输入采购订单"
                        clearable
                        @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="收料类别" prop="receiptType">
                      <DictSelect
                        v-model="model.receiptType"
                        placeholder="请选择收料类别"
                        dict-type="WMS_MATERIAL_RECEIVING_CATEGORY"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="检验结果" prop="noPassType">
                      <DictSelect
                        v-model="model.noPassType"
                        placeholder="请选择检验结果"
                        dict-type="WMS_NO_PASS_TYPE"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="是否超收" prop="overReceipt">
                      <DictSelect
                        v-model="model.overReceipt"
                        placeholder="请选择是否超收"
                        dict-type="YES_NO"
                      />
                    </el-form-item>
                  </el-col>

                  <el-col :span="12">
                    <el-form-item label="创建时间" prop="createTime">
                      <el-date-picker
                        v-model="model.createTime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </template>
            </EasyAdvancedQuery>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 列表 -->
    <IncomingRegister
      ref="incomingRegister"
      @uploadedFiles="handleUploaded"
      @columns="getColumns"
    />

    <!-- test ==== 打印 -->
    <PrintTemplate ref="packingLabelPrinitTemplate" :print-data="printData" />

    <!-- 东苏发单据打印 -->
    <IssuePrinit ref="issuePrint" />
    <!-- 附件上传 -->
    <AttachmentUpload ref="attachmentUpload" @files="getFileList" />
  </div>
</template>

<script>
import {
  exportIncomingReceiptExcel,
  getSupplierData,
} from "@/api/wms/incoming/register";
import PrintTemplate from "./components/PrintTemplate.vue";
import IncomingRegister from "../../inquirePurchase/complexDocuments/components/IncomingRegister.vue";
// 东苏发单据打印
import IssuePrinit from "../../output/productionIssue/issueDetails/IssuePrinit.vue";
const AccessTokenKey = "ACCESS_TOKEN";
import AttachmentUpload from "./components/AttachmentUpload.vue";

export default {
  name: "IncomingReceipt",
  components: {
    PrintTemplate,
    IncomingRegister,
    IssuePrinit,
    AttachmentUpload,
  },
  provide() {
    return {
      paramsQuery: this.queryParams,
    };
  },
  data() {
    return {
      // 生产日期
      produceDate: "",
      printData: [],
      // 遮罩层
      loading: true,
      sureLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 其他入库
      otherShow: true,
      // 物料过期遮罩
      dialogVisible: false,
      // 总条数
      total: 0,
      // WMS 入库业务-来料登记主列表
      list: [],
      // 弹出层标题
      title: "",
      currentData: [],
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        // pageNo: 1,
        // pageSize: 10,
        areaTest: null,
        materialLots: null,
        purchaseOrderNo: null,
        materialNo: null,
        receiptQty: null,
        testStatus: null,
        createTime: [],
        materialName: null,
      },
      // 显示客户
      receiptShow: true,
      // 物料编码
      materialDisabled: false,
      // 物料编码列表
      materialList: [],
      // 采购单列表
      orderOptions: [],
      // 上传的文件
      uploadFiles: [],
      //  过保质期提示
      warningText: null,
      // 单据打印参数
      tokens: "",
      baseUrl: "",
      reportId: "",
      isMultiple: "0",
      // 登记的参数
      registerParams: {
        areaTest: null,
        materialLots: null,
        purchaseOrderNo: null,
        materialNo: null,
        receiptQty: null,
        testStatus: null,
      },
      // 表单参数
      form: {
        pieceQty: null,
        unitCode: null,
      },
      columnList: [],
      registerForm: {},
      // 表单校验
      rules: {
        purchaseOrderNo: [
          {
            required: true,
            message: "请输入关键字选择采购订单",
            trigger: "change",
          },
        ],
        materialNo: [
          {
            required: true,
            message: "请输入关键字选择物料编码",
            trigger: "change",
          },
        ],
        bagQty: [
          { required: true, message: "到货包数不能为空", trigger: "change" },
        ],
        receiptQty: [
          { required: true, message: "到货包数不能为空", trigger: "blur" },
        ],
        mantissaQty: [
          { required: true, message: "到货尾数不能为空", trigger: "change" },
        ],
        pieceQty: [
          { required: true, message: "到货件数不能为空", trigger: "change" },
        ],
        customerCode: [
          { required: true, message: "请选择客户", trigger: "change" },
        ],
        receiptType: [
          { required: true, message: "请选择物料类别", trigger: "change" },
        ],
        produceDate: [
          { required: false, message: "生产日期不能为空", trigger: "change" },
        ],
        supplierCode: [
          { required: false, message: "供应商不能为空", trigger: "change" },
        ],
        departmentNo: [
          { required: false, message: "物料编码不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.tokens = localStorage.getItem(AccessTokenKey);
    this.baseUrl = process.env.VUE_APP_REPORT_API;
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.$nextTick(() => {
        const incomingRefs = this.$refs.incomingRegister;
        incomingRefs.getList();
        // 打印单据report Id
        this.reportId = this.$route.meta.reportId[0];
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.toggle = !this.toggle;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        areaTest: undefined,
        materialLots: undefined,
        purchaseOrderNo: undefined,
        materialNo: undefined,
        receiptQty: undefined,
        testStatus: undefined,
        produceDate: undefined,
        width: undefined,
        length: undefined,
        thickness: undefined,
        pieceQty: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$refs.incomingRegister.queryParams.pageNo = 1;
      this.$refs.incomingRegister.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.resetForm("registerForm");
      this.$refs.incomingRegister.advParams = {};
      this.advParams = {};
      this.handleQuery();
    },
    // 小数点问题处理
    getHandleNumber(val) {
      return Math.round(parseFloat(val) * 100000000) / 100000000;
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有WMS 入库业务-来料登记主数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportIncomingReceiptExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "WMS 入库业务-来料登记主.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    // 扫码获取登记数据
    handleSupplierData() {
      const { materialLots } = this.registerParams;
      const params = {
        batchNumber: materialLots,
      };
      getSupplierData(params).then((res) => {
        if (res.data === null) {
          this.$modal.msgWarning("暂无数据，请检查条码是否正确!");
          return;
        }
        this.currentData = res.data || {};
        this.registerParams = res.data || {};
      });
    },
    // 高级查询
    advQuery(advQueryParams) {
      this.advParams = advQueryParams;
      this.$refs.incomingRegister.advParams = this.advParams;
      this.handleQuery();
    },
    // 附件上传
    handleUpload() {
      this.$refs.attachmentUpload.isUploadShow = -1;
      this.$refs.attachmentUpload.title = "上传附件";
      this.$refs.attachmentUpload.visible = true;
    },
    // 获取上传的附件
    getFileList(data) {
      this.uploadFiles = data;
    },
    // 查看附件
    handleUploaded(data) {
      this.$refs.attachmentUpload.fileList = data;
      this.$refs.attachmentUpload.title = "查看附件";
      this.$refs.attachmentUpload.isUploadShow = 0;
      this.$refs.attachmentUpload.visible = true;
    },
    getColumns(data) {
      this.columnList = data;
    },
    // 获取显示隐藏列
    getColumnsList() {
      this.$nextTick(() => {
        this.$refs.incomingRegister.getColumnsList();
      });
    },
  },
};
</script>
<style scoped>
.validateDate {
  font-size: 16px;
}
</style>
