<template>
  <div class="data-info">
    <el-card>
      <el-form :model="dataForm" ref="dataFormRef" label-width="120px">
        <el-form-item label="父级分类:">
          <el-select
            v-model="dataForm.parent_id"
            placeholder="请选择你级分类"
            size="large"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in parentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称:">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <!-- <el-form-item label="分类介绍:">
           <el-input type="textarea" v-model="dataForm.intro"></el-input>
       </el-form-item> -->
        <el-form-item label="分类状态:">
          <el-select
            v-model="dataForm.status"
            placeholder="请选择分类状态"
            size="large"
            style="width: 100%"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="text-align: right; margin-top: 20px">
      <el-button type="warning" @click="btnSave">确认保存</el-button>
    </div>
  </div>
</template>

<script>
import {
  createTemplateCategory,
  updateTemplateCategory,
  getTemplateCategoryInfo,
  searchTemplateCategory,
} from "@/api/template";
export default {
  emits: ["onClose", "onSearch"],
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  watch: {
    id: {
      handler(v) {
        let _this = this;
        if (v == null || v <= 0) return;
        _this.getInfo(v);
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      activeName: "base",
      currentCategory: [],
      dataForm: {
        id: 0,
        name: "",
        parent_id: 0,
        intro: "",
        status: 5,
      },
      parentList: [],
      statusOptions: [
        {
          value: "",
          label: "请选择状态",
        },
        {
          value: 5,
          label: "启用",
        },
        {
          value: 6,
          label: "停用",
        },
      ],
    };
  },

  mounted() {
    this.initParentCategory();
  },
  methods: {
    resetForm() {
      // 重置所有表单字段
      this.dataForm = {}; // 或者设置为初始值
      // 如果使用 el-form，可以调用其 resetFields 方法
      this.$refs.dataFormRef.resetFields();
    },
    initParentCategory() {
      let _this = this;
      searchTemplateCategory({
        page: 1,
        pageSize: 999,
        parent_id: 0,
        status: 5,
      }).then((res) => {
        if (res.status != 200) return;
        _this.parentList.push({ id: 0, name: "父级分类" });
     /*    for (var i = 0; i < res.data.dataList.length; i++) {
          _this.parentList.push({
            id: res.data.dataList[i].id,
            name: res.data.dataList[i].name,
          });
        } */
      });
    },
    //保存更新
    btnSave(e) {
      let _this = this;
      if (_this.dataForm.id > 0) {
        updateTemplateCategory(_this.dataForm).then((res) => {
          if (res.status != 200) return;
          _this.$alert("模板类别信息更新成功");
          _this.$emit("onSearch");
          _this.$emit("onClose");
        });
      } else {
        createTemplateCategory(_this.dataForm).then((res) => {
          if (res.status != 200) return;
          _this.$alert("模板类别信息创建成功");
          _this.$emit("onSearch");
          _this.$emit("onClose");
        });
      }
    },

    //产品详情
    getInfo(id) {
      let _this = this;
      let par = {
        id: id,
      };
      getTemplateCategoryInfo(par).then((res) => {
        if (!res) return;
        if (res.status != 200) return;
        res.data.parent_id = Number(res.data.parent_id);
        _this.dataForm = res.data;
      });
    },

    onChangeStatus(e) {
      this.dataForm.status = e;
    },
    //修改分类信息
    onChangeCategory(e) {
      let _this = this;
      _this.dataForm.categoryId = e;
    },
  },
};
</script>

<style lang="scss">
@import "./dataInfo.scss";
</style>
