<script>
import { edit, type } from "@/api/material/label";
export default {
  name: "EditDialog",
  data() {
    const initDict = {
      labelType: [
        { label: "类别", value: 1 },
        { label: "标签", value: 2 },
      ],
      parentId: [],
    };
    const initColumns = [
      {
        type: "select",
        prop: "labelType",
        label: "标签类别",
        required: true,
      },
      {
        type: "select",
        prop: "parentId",
        label: "上级类别",
        required: false,
      },
      {
        type: "text",
        prop: "name",
        label: "名称",
        required: true,
      },
      {
        type: "textarea",
        prop: "description",
        label: "说明",
        required: false,
      },
    ];
    const initForm = () => {
      let form = {};
      initColumns.forEach((item) => (form[item.prop] = ""));
      return form;
    };
    return {
      loading: false,
      dialogFormVisible: false,
      form: initForm(),
      columns: initColumns,
      dict: initDict,
    };
  },
  methods: {
    fetchType() {
      this.loading = true;
      type()
        .then((res) => {
          this.dict.parentId = res.map((item) => ({
            value: item.id,
            label: item.name,
          }));
        })
        .finally(() => {
          this.loading = false;
        });
    },

    fetchItem(prop) {
      console.log(prop);
      this.dialogFormVisible = true;
      this.form = prop;
      this.fetchType();
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.labelType === 1) this.form.parentId = 0;
          edit(this.form)
            .then((res) => {
              let { code } = res;
              if (code === 200) {
                this.dialogFormVisible = false;
                this.$message.success("success");
                this.$parent.$children[0].fetchList();
                this.form = {};
              }
            })
            .finally(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {},
};
</script>

<template>
  <el-dialog destroy-on-close title="编辑" :visible.sync="dialogFormVisible">
    <el-form :model="form" ref="addForm" label-position="top">
      <el-form-item prop="labelType" label="标签类别" required>
        <el-select v-model="form.labelType" disabled>
          <el-option
            v-for="item in dict.labelType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.labelType === 2"
        prop="parentId"
        label="上级类别"
        :required="form.labelType === 2"
      >
        <el-select v-model="form.parentId">
          <el-option
            v-for="item in dict.parentId"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="name" label="名称" required>
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item prop="description" label="说明">
        <el-input
          v-model="form.description"
          type="textarea"
          autosize
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="loading" @click="dialogFormVisible = false"
        >取 消</el-button
      >
      <el-button type="primary" :disabled="loading" @click="onSubmit('addForm')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<style scoped></style>
