<template>
  <div>
    <detail-page
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :tableAutoLoad="false"
      :loading="loading"
      :items="items"
      :optionsData="optionsData"
      :codeDatas="codeDatas"
      :dataSource="dataSource"
      @addRule="addRule"
      @selectChange="selectChange"
      @delData="delData"
      @changeClick="changeClick"
      ref="detial"
    >
    </detail-page>
    <addRule
      :title="titleName"
      :visibleDetial="visibleDetial"
      :optionsData="optionsData"
      :setRecord="setRecord"
      @cancelRule="cancelRule"
      @requestSuccess="requestSuccess"
    ></addRule>
  </div>
</template>

<script>
import DetailPage from "./components/detial";
import addRule from "./components/addRule";
import API from "./api/default";
import { ruleReplenishList, ruleReplenishDel, getOptions, getCodes } from "./api/api";
import { mapGetters } from "vuex";
import { diffOption, forSelectText } from "@/views/kzyWMS/utils/index.js";
import { sorter } from "../../utils/sort";
import { upcloud_base_strategy } from "@/config/serviceSettings";
const items = [
  { type: "input", name: "warehouseId", label: "仓库ID", hidden: true },
  {
    type: "search",
    name: "warehouseName",
    label: "仓库",
    destroyOnClose: true,
    targetValueKey: "warehouseName",
    targetClick: true,
    required: true,
    span: 6,
    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" },
      ],
    },
    remoteUrl: API.warehouse,
    on: {
      ok(event, value, rowData, form) {
        form.setFieldsValue({
          warehouseId: rowData[0].warehouseId,
        });
      },
      change(event, value, rowData, form) {
        if (!value) {
          form.setFieldsValue({
            warehouseId: "",
          });
        }
      },
    },
  },
];
const columns = function (self) {
  return [
    { dataIndex: "warehouseName", title: "仓库", key: "warehouseName" },
    {
      dataIndex: "srcWarehouseAreaName",
      title: "源库区",
      key: "srcWarehouseAreaName",
      scopedSlots: { customRender: "srcWarehouseAreaName" },
    },
    {
      dataIndex: "destWarehouseAreaName",
      title: "目标库区",
      key: "destWarehouseAreaName",
      scopedSlots: { customRender: "destWarehouseAreaName" },
    },
    { dataIndex: "confirmMode", title: "确认次数", key: "confirmMode", scopedSlots: { customRender: "confirmMode" } },
    //todo
    { dataIndex: "sort", title: "执行顺序", key: "sort" },
    { dataIndex: "action", title: "操作", align: "center", key: "action", scopedSlots: { customRender: "action" } },
  ];
};

export default {
  components: { DetailPage, addRule },
  computed: {
    ...mapGetters(["warehouse"]),
  },
  data() {
    return {
      items,
      columns: columns(this),
      title: "补货规则",
      titleName: "新增补货规则",
      loading: false,
      visibleDetial: false,
      setRecord: {},
      optionsData: [],
      codeDatas: [],
      tableRowKey: "replenishRuleId",
      selectedRowKeys: [],
      dataSource: [],
      selectedRows: [],
      tableRemoteUrl: {
        url: upcloud_base_strategy + "/rule/replenish/v1/list",
        method: "get",
      },
      btnOptions: [
        {
          name: "新增",
          icon: "",
          action: "addRule",
        },
      ],
    };
  },
  created() {
    // this.queryData()
    this.queryCode();
    diffOption(columns(this)).then((res) => {
      console.log(res);
      this.columns = sorter(res);
    });
  },
  mounted() {
    this.$refs.detial.$refs.gridform.form.setFieldsValue({
      warehouseId: this.warehouse.warehouseId,
      warehouseCode: this.warehouse.warehouseCode,
      warehouseName: this.warehouse.warehouseName,
    });
  },
  methods: {
    addRule() {
      // 新增
      this.setRecord = {};
      this.titleName = "新增补货规则";
      this.visibleDetial = true;
    },
    cancelRule(val) {
      this.visibleDetial = val;
    },
    requestSuccess(val) {
      this.visibleDetial = false;
      this.queryData();
    },
    delData(record) {
      // 删除
      let params = {
        replenishRuleId: record.replenishRuleId,
      };
      ruleReplenishDel(params).then((res) => {
        this.loading = false;
        if (res.success) {
          this.$message.success("删除成功！");
          this.queryData();
        } else {
          this.$exception(res);
        }
      });
    },
    queryCode() {
      // 字典查询
      let params = {
        type: "wms_storeroom",
      };
      getCodes(params).then((res) => {
        if (res.success) {
          this.codeDatas = res.data;
        } else {
          this.$exception(res);
        }
      });
    },
    changeClick(record) {
      // 修改
      this.titleName = "修改补货规则";
      this.setRecord = record;
      this.visibleDetial = true;
    },
    queryData(arrList) {
      // 查询
      let values = this.$refs.detial.$refs.gridform.form.getFieldsValue();
      let params = { ...values };

      this.loading = true;
      ruleReplenishList(params).then((res) => {
        this.loading = false;
        if (res.success) {
          this.dataSource = res.data;
          // this.$message.success('生成盘点计划成功！')
        } else {
          this.$exception(res);
        }
      });
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRows = selectedRows;
      this.selectedRowKeys = selectedRowKeys;
    },
  },
};
</script>

<style></style>
