<template>
  <el-dialog
    v-dialogDrag
    title="送货通知单"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @close="dialogClose"
    @open="dialogOpen"
  >
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <SearchBar :show-more="false" :collapse-span="6">
        <template #header>
          <el-col :span="6">
            <el-form-item label="通知日期" prop="noticeDate">
              <el-date-picker
                v-model="queryParams.noticeDate"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="请选择通知日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="备注" prop="remark" label-width="88px">
              <el-input
                v-model="queryParams.remark"
                placeholder="请输入备注"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              label="送货批次"
              prop="supplierBatch"
              label-width="88px"
            >
              <el-input
                v-model="queryParams.supplierBatch"
                placeholder="请输入送货批次"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
      </SearchBar>
    </el-form>

    <!-- 列表 -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="currentRows"
      border
      size="mini"
    >
      <el-table-column
        label="物料编号"
        align="center"
        prop="materialNo"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column label="规格型号" align="center" prop="size" />
      <el-table-column
        label="物料单位名称"
        align="center"
        prop="unitName"
        width="120"
      />
      <el-table-column label="长度(m)" align="center" prop="length" width="100">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.length"
            placeholder="请输入长度"
            oninput="value=value.replace(/[^0-9]{0,1}(\d*(?:\.\d{0,8})?).*$/g, '$1')"
            @change="lengthChange(scope.row.length, scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="宽度(mm)" align="center" prop="width" width="100">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.width"
            placeholder="请输入宽度"
            oninput="value=value.replace(/[^0-9]{0,1}(\d*(?:\.\d{0,8})?).*$/g, '$1')"
            @change="widthChange(scope.row.width, scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="厚度(mm)"
        align="center"
        prop="thickness"
        width="100"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.thickness"
            placeholder="请输入厚度(mm)"
            oninput="value=value.replace(/[^0-9]{0,1}(\d*(?:\.\d{0,8})?).*$/g, '$1')"
          />
        </template>
      </el-table-column>
      <el-table-column label="包数" align="center" prop="bagQty" width="120">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.bagQty"
            placeholder="请输入包数"
            oninput="value=value.replace(/^\D*(\d*(?:.\d)?).*$/g, '$1')"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="每包数量"
        align="center"
        prop="pieceQty"
        width="120"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.pieceQty"
            placeholder="请输入每包数量"
            oninput="value=value.replace(/[^0-9]{0,1}(\d*(?:\.\d{0,8})?).*$/g, '$1')"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="尾数"
        align="center"
        prop="mantissaQty"
        width="100"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.mantissaQty"
            placeholder="请输入尾数"
            oninput="value = value.replace(/[^\d.]/g, '').replace(/(\.\d{8}).*/g, '$1')"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="生产日期"
        align="center"
        prop="produceDate"
        width="245"
      >
        <template slot-scope="scope">
          <el-date-picker
            v-model="scope.row.produceDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择生产日期"
          />
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="size" width="120">
        <template slot-scope="scope">
          <el-input v-model="scope.row.size" placeholder="请输入规格" />
        </template>
      </el-table-column>
      <el-table-column
        label="预计到货日期"
        align="center"
        prop="expectArriveDate"
        width="250"
      >
        <template slot-scope="scope">
          <el-date-picker
            v-model="scope.row.expectArriveDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择到货日期"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="供应商送货数量"
        align="center"
        prop="deliveryQty"
        width="120"
      >
        <template slot-scope="scope">
          <el-input v-model="scope.row.deliveryQty" placeholder="请输入数量" />
        </template>
      </el-table-column>
      <el-table-column
        label="应收货数量"
        align="center"
        prop="shouldReceiveQty"
        width="120"
      />
      <el-table-column
        label="送货数量"
        align="center"
        prop="confirmQty"
        width="120"
      />
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        v-hasPermi="['wms:delivery-notice:create']"
        type="primary"
        :loading="ensureLoading"
        @click="submitForm"
        >确定</el-button
      >
    </div>

    <!-- 单据打印 -->
    <IssuePrinit ref="issuePrinit" />
  </el-dialog>
</template>

<script>
import { createDeliveryOrder } from "@/api/wms/orders/purchase";
import IssuePrinit from "../../output/productionIssue/issueDetails/IssuePrinit.vue";
const AccessTokenKey = "ACCESS_TOKEN";

export default {
  name: "DeliveryConfimation",
  components: {
    IssuePrinit,
  },
  props: {
    currentRows: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      ensureLoading: false,
      // 总条数
      total: 0,
      // ERP采购订单明细列表
      list: [],
      // 弹出层标题
      title: "",
      tokens: "",
      baseUrl: "",
      reportId: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        remark: null,
        noticeDate: null,
        supplierBatch: null,
      },
      // currentRows: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  methods: {
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        detailErpId: undefined,
        inStockId: undefined,
        erpInStockOrderId: undefined,
        erpInStockOrderNo: undefined,
        detailNumber: undefined,
        materialNo: undefined,
        materialName: undefined,
        materialQty: undefined,
        materialModels: undefined,
        unitName: undefined,
        shouldReceiveAmount: undefined,
        receiveQty: undefined,
        returnQty: undefined,
        materialStatus: undefined,
        supplierCode: undefined,
        supplierName: undefined,
        batch: undefined,
        price: undefined,
        taxPrice: undefined,
        allAmount: undefined,
        taxRate: undefined,
        comment: undefined,
        deliveryTime: undefined,
        erpMark: undefined,
      };
      this.resetForm("form");
    },
    dialogClose() {
      this.queryParams.remark = null;
      this.queryParams.noticeDate = null;
    },
    dialogOpen() {
      this.tokens = localStorage.getItem(AccessTokenKey);
      this.baseUrl = process.env.VUE_APP_REPORT_API;
      this.reportId = this.$route.meta.reportId;
    },
    // 小数点问题处理
    getHandleNumber(val) {
      return Math.round(parseFloat(val) * 100000000) / 100000000;
    },
    // 根据宽度计算每包数量
    widthChange(v, row) {
      const { length } = row;
      if (
        length === null ||
        length === undefined ||
        length === 0 ||
        v == null ||
        v === undefined ||
        v === 0
      ) {
        row.pieceQty = undefined;
        return;
      }
      row.pieceQty = this.getHandleNumber((v / 1000) * length);
    },
    // 根据长度计算到货数量
    lengthChange(v, row) {
      const { width } = row;
      if (width === null || width === undefined || width === 0) {
        row.pieceQty = undefined;
        return;
      }
      row.pieceQty = this.getHandleNumber((width / 1000) * v);
    },
    // 创建送货通知单
    submitForm() {
      const { noticeDate, remark } = this.queryParams;
      if (
        (noticeDate === undefined || noticeDate === null) &&
        (remark === null || remark === undefined)
      ) {
        this.$modal.msgWarning("请选择通知日期和填写备注");
        return;
      }
      const params = {
        noticeDate: this.queryParams.noticeDate,
        remark: this.queryParams.remark,
        supplierBatch: this.queryParams.supplierBatch,
        list: this.currentRows,
      };
      this.ensureLoading = true;
      createDeliveryOrder(params)
        .then((res) => {
          this.$modal.msgSuccess("创建成功！");
          this.open = false;
        })
        .finally(() => {
          this.ensureLoading = false;
        });
    },
  },
};
</script>
