<!-- 采购员上级采购经理匹配——详情 -->
<script>
import useColumns from "./columns";
import { ADD, DETAILS, EDIT } from "@/api/business/purchase/match";
export default {
  name: "MatchDetails",
  props: {
    dict: {
      type: Object,
    },
    addType: {
      type: String,
      default: "add",
    },
    data: {
      type: Array,
      default: () => [],
    },
  },
  components: {
    ElSuperForm: () => import("@/components/super-form/index.vue"),
    ElPopoverMultipleTreeSelect: () =>
      import("@/components/popover-tree-select/multiple.vue"),
  },
  data() {
    const { FormColumns } = useColumns();
    const params = this.$init.params(FormColumns);
    const rules = this.$init.rules(FormColumns);
    return {
      rules,
      params: {
        ...params,
        enable: "0",
      },
      FormColumns,
      width: "100%",
      visible: false,
      loading: false,
    };
  },
  computed: {
    title: {
      get() {
        const { addType } = this;
        if (addType === "add") {
          return "新 增";
        }
        if (addType === "edit") {
          return "编 辑";
        }
        return "查 看";
      },
      set() {},
    },
    innerValue: {
      get() {
        let { data } = this.$props;
        return data.length ? data[0] : null;
      },
      set() {},
    },
  },
  methods: {
    setVisible(prop) {
      this.visible = prop;
    },
    //
    async fetchItem(id) {
      try {
        this.loading = true;
        let { code, msg, data } = await DETAILS({ id });
        if (code === 200) {
          this.params = data;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    beforeOpen() {
      let {
        $props: { addType },
        innerValue,
      } = this;
      if (addType !== "add") {
        this.fetchItem(innerValue.id);
      }
    },
    hide() {
      this.visible = false;
      this.params = { ...this.$init.params(this.FormColumns), enable: "0" };
      this.$emit("refresh");
    },
    useSave(prop) {
      console.log(this.params,"params");
      this.$refs[prop].$refs[prop].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            let {
              $props: { addType },
              params,
            } = this;
            let { code, msg } =
              addType === "edit"
                ? await EDIT({ ...params })
                : await ADD({ ...params });
            if (code === 200) {
              this.$notify.success(msg);
              this.hide();
            }
          } catch (err) {
            console.error(err);
          } finally {
            this.loading = false;
          }
        } else {
          return false;
        }
      });
    },
  },
  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
          v-show="addType !== 'see'"
          type="primary"
          :size="$attrs.size"
          :loading="loading"
          @click="useSave('superForm')"
          >保 存</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"
      ref="superForm"
      label-width="auto"
      label-position="right"
      style="padding: 20px"
      :disabled="addType === 'see'"
    >
      <template slot="classOneObjectList" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :closable="addType !== 'see'"
        >
        </component
      ></template>
    </el-super-form>
  </el-drawer>
</template>

<style scoped>
>>> .el-drawer__header {
  margin-bottom: 0;
  padding: 5px 20px;
}
</style>
