<script>
import { add, type } from "@/api/material/label";
export default {
  name: "MaterialAddLabelDialog",
  data() {
    const initDict = {
      label_type: [
        { label: "类别", value: 1 },
        { label: "标签", value: 2 },
      ],
      parent_id: [],
    };
    const initColumns = [
      {
        type: "select",
        prop: "label_type",
        label: "标签类别",
        required: true,
      },
      {
        type: "select",
        prop: "parent_id",
        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.parent_id = res.map((item) => ({
            value: item.id,
            label: item.name,
          }));
        })
        .finally(() => {
          this.loading = false;
        });
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.label_type === 1) this.form.parent_id = 0;
          add(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() {
    this.fetchType();
  },
};
</script>

<template>
  <el-dialog destroy-on-close title="新增" :visible.sync="dialogFormVisible">
    <el-form :model="form" ref="addForm" label-position="top">
      <el-form-item prop="label_type" label="标签类别" required>
        <el-select v-model="form.label_type">
          <el-option
            v-for="item in dict.label_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.label_type === 2"
        prop="parent_id"
        label="上级类别"
        :required="form.label_type === 2"
      >
        <el-select v-model="form.parent_id">
          <el-option
            v-for="item in dict.parent_id"
            :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>
