<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="deliveryTime">
              <el-date-picker
                v-model="queryParams.deliveryTime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                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>
        </template>
      </SearchBar>
    </el-form> -->

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" border size="mini">
      <el-table-column
        label="物料编码"
        align="center"
        prop="materialNo"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="客户编码"
        align="center"
        prop="customerCode"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="客户名称"
        align="center"
        prop="customerName"
        width="160"
        show-overflow-tooltip
      />
      <el-table-column
        label="本次出货数量"
        align="center"
        prop="nowDeliveredQty"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.nowDeliveredQty"
            @change="deliverChange"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="已出货数量"
        align="center"
        prop="deliveredQty"
        width="120"
      />
      <el-table-column
        label="应出货数量"
        align="center"
        prop="shouldDeliveredQty"
        width="120"
      />
      <el-table-column
        label="单位"
        align="center"
        prop="unitName"
        width="150"
        show-overflow-tooltip
      />
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        v-hasPermi="['wms:sale-out-order-master:create']"
        type="primary"
        :loading="ensureLoading"
        @click="submitForm"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { createSaleOutOrderMaster } from "@/api/wms/orders/sale";

export default {
  name: "DeliveryConfimation",
  props: {
    currentRows: {
      type: Array,
      default: () => [],
    },
    model: {
      type: Object,
      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,
      },
      // 表单参数
      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;
      this.$emit("notificationClose", this.queryParams.remark);
    },
    dialogOpen() {
      console.log(this.model);
      this.list = this.model.list;
      // this.list = this.currentRows.map(item => {
      //   item.nowDeliveredQty = item.shouldDeliveredQty - item.deliveredQty
      //   return item
      // })
    },
    // 创建送货通知单
    submitForm() {
      const { deliveryTime, remark } = this.queryParams;
      if (
        (deliveryTime === undefined || deliveryTime === null) &&
        (remark === null || remark === undefined)
      ) {
        this.$modal.msgWarning("请选择出货日期和填写备注");
        return;
      }
      const noticeList = this.currentRows.map((item) => {
        const {
          customerCode,
          customerName,
          nowDeliveredQty,
          id: saleOrderDetailId,
          materialName,
          materialNo,
        } = item;
        return {
          customerCode,
          customerName,
          nowDeliveredQty,
          saleOrderDetailId,
          materialName,
          materialNo,
        };
      });
      const params = {
        deliveryTime: this.queryParams.deliveryTime,
        remark: this.queryParams.remark,
        list: noticeList,
      };
      this.ensureLoading = true;
      createSaleOutOrderMaster(params)
        .then((res) => {
          this.$modal.msgSuccess("创建成功！");
          this.open = false;
          this.$emit("createComplete");
        })
        .finally(() => {
          this.ensureLoading = false;
        });
    },
    deliverChange(v) {
      if (typeof v === "string") {
        v = parseFloat(v);
      }
    },
  },
};
</script>
