<script>
import Columns from "./columns";
import {
  addDivision,
  getDivisions,
  editDivision,
} from "@/api/purchase/basic.js";
// 调用物料分类详情接口用于数据回显
import { getDetail } from "@/api/classify/basic";
export default {
  name: "Division",
  props: {
    dict: {
      type: Object,
    },
  },
  components: {
    ElSuperForm: () => import("@/components/super-form/index.vue"),
    ElPopoverTreeSelect: () =>
      import("@/components/popover-tree-select/index.vue"),
  },
  data() {
    const { FormColumns } = Columns();
    const params = this.$init.params(FormColumns);
    const rules = this.$init.rules(FormColumns);
    // FormColumns.forEach(({ item, attr: { dataMapping } }) => {
    //   if (dataMapping) {
    //     for (const key in dataMapping) {
    //       if (Object.hasOwnProperty.call(dataMapping, key)) {
    //         params[key] = "";
    //       }
    //     }
    //   }
    // });
    return {
      rules,
      params,
      width: "100%",
      visible: false,
      loading: false,
      FormColumns: FormColumns,
      pageStu: "add",
    };
  },
  computed: {
    title: {
      get() {
        return "新增";
      },
      set() {},
    },
  },
  methods: {
    setStatus(prop, status) {
      this.visible = prop;
      this.pageStu = status;
    },
    // 打开前
    beforeOpen() {},
    // 详情
    async fetchItem(prop) {
      try {
        // try
        this.loading = true;

        let { code, data } = await getDivisions(prop.id);

        if (code == 200) {
          this.params = data;
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },

    // 关闭
    hide() {
      this.visible = false;
      this.$emit("success");
      this.params = this.$init.params(this.FormColumns);
    },
    // 保存
    async useSave() {
      try {
        let { params, pageStu } = this;
        this.$modal.loading("保存中...");
        let { code, msg } =
          pageStu === "edit"
            ? await editDivision(params)
            : await addDivision(params);

        if (code == 200) {
          this.$notify.success(msg);
          this.hide();
        }
      } catch (error) {
      } finally {
        this.$modal.closeLoading();
      }
    },
    // 编辑
    useEdit() {
      this.pageStu = "edit";
    },
    // 物料分类
    async materialClassifyChange(prop) {
      console.log(prop, "prop");
      try {
        let id = prop[0] ? prop[0].id : "";
        let {
          code,
          data: { oneClass, twoClass, threeClass, fourClass },
        } = await getDetail(id);
        if (code == 200) {
          this.params.materialClassifyOneName = oneClass;
          this.params.materialClassifyTwoName = twoClass;
          this.params.materialClassifyThreeName = threeClass;
          this.params.materialClassifyFourName = fourClass;
        }
      } catch (error) {}
    },
  },
  created() {},
};
</script>

<template>
  <el-drawer
    v-bind="$attrs"
    v-on="$listeners"
    :size="width"
    :visible.sync="visible"
    destroy-on-close
    :show-close="false"
    @close="hide"
    @open="beforeOpen"
    v-loading="loading"
  >
    <div
      slot="title"
      style="display: flex; justify-content: space-between; align-items: center"
    >
      <h3>{{ title }}</h3>
      <div>
        <el-button
          type="primary"
          :size="$attrs.size"
          :loading="loading"
          @click="useSave('superForm')"
          v-if="pageStu !== 'see'"
          >保 存</el-button
        >
        <el-button
          type="primary"
          :size="$attrs.size"
          :loading="loading"
          @click="useEdit"
          v-if="pageStu === 'see'"
          >编 辑</el-button
        >
        <el-button :size="$attrs.size" :loading="loading" @click="hide"
          >取 消</el-button
        >
      </div>
    </div>
    <el-super-form
      v-model="params"
      :dict="dict"
      :rules="rules"
      :size="$attrs.size"
      :columns="FormColumns"
      :disabled="pageStu === 'see'"
      ref="superForm"
      label-width="auto"
      label-position="right"
      style="padding: 20px"
    >
      <template slot="materialClassify" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          @change="materialClassifyChange"
        >
        </component
      ></template>
    </el-super-form>
  </el-drawer>
</template>
