<script>
import useColumns from "./columns";
import {
  getDemandWindowList,
  saveDemandWindow,
} from "@/api/purchase/purchaseDemand.js";
export default {
  name: "Demand",
  props: {
    dict: {
      type: [Array, Object],
      default: () => [],
    },
  },
  components: {
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
  },
  data() {
    const { DemandColumns } = useColumns();
    return {
      title: "需求窗口期",
      visible: false,
      loading: false,
      DemandColumns: DemandColumns,
      innerValue: [],
    };
  },
  methods: {
    async open() {
      // 需求窗口期配置
      this.visible = true;
      try {
        this.loading = true;
        let { code, rows } = await getDemandWindowList();
        if (code === 200) {
          this.innerValue = rows;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    close() {
      this.visible = false;
      this.innerValue = [];
    },

    async saveDemand() {
      try {
        let params = this.innerValue;
        let { code } = await saveDemandWindow(params);
        if (code === 200) {
          this.$modal.notifySuccess("保存成功");
          this.close();
        }
      } catch (error) {}
    },

    changeDeadline(row) {
      row.delFlag = "2";
    },
  },
  created() {},
};
</script>

<template>
  <el-button type="primary" :size="$attrs.size" @click="open">
    {{ title }}

    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div slot="footer">
        <el-button :size="$attrs.size" @click="close">取 消</el-button>
        <el-button type="primary" :size="$attrs.size" @click="saveDemand"
          >保 存</el-button
        >
      </div>

      <el-super-ux-table
        v-loading="loading"
        v-model="innerValue"
        :columns="DemandColumns"
        :size="$attrs.size"
        :dict="dict"
        height="450"
      >
        <template slot="deadline" slot-scope="scope">
          <component
            v-bind="scope.attr"
            v-model="scope.row[scope.item.key]"
            :size="$attrs.size"
            :source.sync="scope.row"
            @input="changeDeadline(scope.row)"
          >
          </component>
        </template>
      </el-super-ux-table>
      <!-- <span style="color: red">注:周计划星期填写范围1-7，月计划填写范围1-12</span> -->
    </el-dialog>
  </el-button>
</template>
