<template>
  <p-modal
    :title="title"
    :visible="isvisible"
    @ok="handleOk"
    width="60%"
    :maskClosable="false"
    :destroyOnClose="true"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <p-grid-form
      ref="forms"
      :span="24"
      :labelCol="{ span: 6 }"
      :wrapperCol="{ span: 18 }"
      :items="items"
      @submit="hanlderSubmit"
    />
  </p-modal>
</template>
<script>
import { remote } from "../../../basedata/warehouse/config/remote";
import { ruleReplenishAdd, ruleReplenishEedit } from "../api/api";
import API from "../api/default";
let warehouseId = "";
export default {
  data() {
    return {
      items: [
        { type: "input", name: "warehouseId", label: "仓库ID", hidden: true },
        {
          type: "searchModal",
          name: "warehouseName",
          label: "仓库",
          title: "仓库",
          targetValueKey: "warehouseName",
          destroyOnClose: true,
          required: true,
          targetClick: true,
          value: "",
          toolBar: {
            items: [
              {
                type: "input",
                name: "warehouseName",
                label: "仓库名称",
                span: 8,
                value: "",
              },
              {
                type: "input",
                name: "warehouseCode",
                label: "仓库编码",
                span: 8,
                value: "",
              },
            ],
          },
          tableConfig: {
            rowKey: "warehouseName",
            rowSelectType: 1,
            hasRowSelectCheck: true,
            columns: [
              { dataIndex: "warehouseName", title: "仓库名称", key: "warehouseName" },
              { dataIndex: "warehouseCode", title: "仓库编码", key: "warehouseCode" },
              { dataIndex: "warehouseId", title: "仓库ID", key: "warehouseId" },
            ],
          },
          value: "",
          remoteUrl: API.warehouse,
          on: {
            ok(event, value, rowData, form) {
              form.setFieldsValue({
                warehouseId: rowData[0].warehouseId,
              });
              warehouseId = rowData[0].warehouseId;
            },
            change(event, value, options, form) {
              if (!value) {
                form.setFieldsValue({
                  warehouseId: "",
                  desWarehouseAreaName: "",
                  destWarehouseAreaId: "",
                  srcWarehouseAreaId: "",
                  srcWarehouseAreaName: "",
                });
                warehouseId = "";
              }
            },
          },
        },
        { type: "input", name: "srcWarehouseAreaId", label: "库区ID", hidden: true },
        {
          type: "search",
          name: "srcWarehouseAreaName",
          label: "库区",
          targetValueKey: "warehouseAreaName",
          destroyOnClose: true,
          required: true,
          targetClick: true,
          value: "",
          toolBar: {
            items: [
              {
                type: "input",
                name: "warehouseAreaName",
                label: "库区名称",
                span: 8,
                value: "",
              },
              {
                type: "input",
                name: "warehouseAreaCode",
                label: "库区编码",
                span: 8,
                value: "",
              },
            ],
          },
          tableConfig: {
            rowKey: "warehouseAreaId",
            rowSelectType: 1,
            hasRowSelectCheck: true,
            columns: [
              { dataIndex: "warehouseAreaName", title: "库区名称", key: "warehouseAreaName" },
              { dataIndex: "warehouseAreaCode", title: "库区编码", key: "warehouseAreaCode" },
              { dataIndex: "warehouseAreaId", title: "库区ID", key: "warehouseAreaId" },
            ],
            remoteBefore(res, remoteParams) {
              remoteParams.params.warehouseId = warehouseId; // 添加仓库id查询
            },
          },
          value: "",
          remoteUrl: API.wmsWhArea,
          on: {
            ok(event, value, rowData, form) {
              form.setFieldsValue({
                srcWarehouseAreaId: rowData[0].warehouseAreaId,
              });
            },
            change(event, value, options, form) {
              if (!value) {
                form.setFieldsValue({ srcWarehouseAreaId: "" });
              }
            },
          },
        },
        { type: "input", name: "destWarehouseAreaId", label: "库区ID", hidden: true },
        {
          type: "search",
          name: "warehouseAreaName",
          label: "目标库区",
          targetValueKey: "warehouseAreaName",
          destroyOnClose: true,
          targetClick: true,
          required: true,
          value: "",
          toolBar: {
            items: [
              { type: "input", name: "warehouseAreaName", label: "库区名称", span: 8, value: "" },
              { type: "input", name: "warehouseAreaCode", label: "库区编码", span: 8, value: "" },
            ],
          },
          tableConfig: {
            rowKey: "warehouseAreaId",
            rowSelectType: 1,
            hasRowSelectCheck: true,
            columns: [
              { dataIndex: "warehouseAreaName", title: "库区名称", key: "warehouseAreaName" },
              { dataIndex: "warehouseAreaCode", title: "库区编码", key: "warehouseAreaCode" },
              { dataIndex: "warehouseAreaId", title: "库区ID", key: "warehouseAreaId" },
            ],
            remoteBefore(res, remoteParams) {
              remoteParams.params.warehouseId = warehouseId; // 添加仓库id查询
            },
          },
          value: "",
          remoteUrl: API.wmsWhArea,
          on: {
            ok(event, value, rowData, form) {
              form.setFieldsValue({
                destWarehouseAreaId: rowData[0].warehouseAreaId,
                desWarehouseAreaName: rowData[0].warehouseAreaName,
              });
            },
            change(event, value, options, form) {
              if (!value) {
                form.setFieldsValue({
                  destWarehouseAreaId: "",
                  desWarehouseAreaName: "",
                });
              }
            },
          },
        },

        {
          type: "radio",
          name: "confirmMode",
          label: "确认次数",
          required: true,
          value: 2,
          options: [
            { label: "一次", value: 1 },
            { label: "二次", value: 2 },
          ],
        },

        {
          type: "number",
          name: "sort",
          label: "执行顺序",
          required: true,
          value: "",
        },
      ],
      isvisible: false,
      confirmLoading: false,
      warehouseId: "",
    };
  },
  components: {},
  props: {
    visibleDetial: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "",
    },
    setRecord: {
      type: Object,
      default: {},
    },
    optionsData: {
      type: Array,
      default: [],
    },
  },
  watch: {
    visibleDetial(newVal) {
      this.items[5].options = this.optionsData;
      this.isvisible = newVal;
      if (this.setRecord.replenishRuleId) {
        // 判断修改
        warehouseId = this.setRecord.warehouseId;
        this.$nextTick(() => {
          this.$refs.forms.form.setFieldsValue({
            warehouseId: this.setRecord.warehouseId,
            warehouseName: this.setRecord.warehouseName,
            srcWarehouseAreaId: this.setRecord.srcWarehouseAreaId,
            confirmMode: this.setRecord.confirmMode,
            srcWarehouseAreaName: this.setRecord.srcWarehouseAreaName,
            destWarehouseAreaId: this.setRecord.destWarehouseAreaId,
            destWarehouseAreaName: this.setRecord.destWarehouseAreaName,
            warehouseAreaId: this.setRecord.destWarehouseAreaId,
            warehouseAreaName: this.setRecord.destWarehouseAreaName,
            sort: this.setRecord.sort,
            isFlage: this.setRecord.isEnabled == "1" ? true : false,
          });
        });
      }
    },
  },
  created() {},
  methods: {
    callback(key) {},
    handleCancel() {
      // 取消
      this.isvisible = false;
      this.$emit("cancelRule", false);
    },
    selectChange(data) {
      // 勾选框
      // this.
      this.checkData = data;
    },
    handleOk() {
      // 确认
      this.$refs.forms.form.validateFields((error, values) => {
        if (!error) {
          if (this.setRecord.replenishRuleId) {
            //修改
            values.replenishRuleId = this.setRecord.replenishRuleId;
            this.confirmLoading = true;
            this.changeData(values);
          } else {
            // 新增
            this.confirmLoading = true;
            this.saveData(values);
          }
        }
      });
    },
    saveData(values) {
      // 保存
      ruleReplenishAdd(values).then((res) => {
        this.confirmLoading = false;
        if (res.success) {
          this.$message.success("成功添加规则！");
          this.$emit("requestSuccess", true);
        } else {
          this.$exception(res);
        }
      });
    },
    changeData(values) {
      // 修改
      ruleReplenishEedit(values).then((res) => {
        this.confirmLoading = false;
        if (res.success) {
          this.$message.success("成功修改规则！");
          this.$emit("requestSuccess", true);
        } else {
          this.$exception(res);
        }
      });
    },
    hanlderSubmit(key) {
      // 查询
      // event.preventDefault()
      // this.$emit('hanlderSubmit', event, form)
      // this.$refs[key][0].form.validateFields((error, values) => {
      //   if (!error) {
      //     this.remoteParams = { ...this.tableRemoteParams }
      //     this.remoteParams.params = {
      //       ...values
      //     }
      //   }
      // })
    },
  },
};
</script>
