<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogShow"
      width="800px"
      :before-close="close"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="上级分类" prop="parentId">
          <el-select
            v-model="form.parentId"
            placeholder="请选择上级分类"
            filterable
            clearable
          >
            <el-option :label="item.name" :value="item.categoryId" v-for="(item, index) in parentList" :key="index" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 300px"
            size="small"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model.number="form.sort"
            style="width: 300px"
            size="small"
            placeholder="请输入排序"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类图片" prop="picUrl">
          <image-upload
            v-model="categoryPicUrl"
            :limit="1"
          />
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-radio-group v-model="form.isShow" size="small">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">不显示</el-radio>
          </el-radio-group>
          <div>选择“不显示”，则用户看不到该分类</div>
        </el-form-item>
      </el-form>

      <!-- 按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :disabled="loading" @click="saveClick"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import CustomFieldsMixin from "@/mixins/CustomFields";
import { goodsCategoryCreate, goodsCategoryDetail, goodsCategoryUpdate, goodsCategoryListTree } from "@/api/business/category";
export default {
  mixins: [CustomFieldsMixin],
  props: {
    // 弹框显示
    dialogShow: {
      type: Boolean,
      default: false,
    },
    // 分类ID
    categoryId: {
      type: [String, Number],
      default: null
    },
  },
  data() {
    return {
      //一级分类列表
      parentList: [{
        name:'',
        categoryId:'',
      }],
      loading: false,
      form: {
        isShow: 1,
        sort: 0,
        parentId: '',
      },
      rules: {
        name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
        picUrl: [
          {
            required: true,
            message: "请上传分类图片",
            trigger: "change",
          },
        ],
        sort: [
          { required: true, message: "请输入排序", trigger: "blur" },
          {
            pattern: /^([0-9]\d*)$/,
            message: "请输入合法的数字",
            trigger: ["blur", "change"],
          },
        ],
        isShow:  [
          { required: true, message: "请选择", trigger: "change" },
        ],
      },
    };
  },

  computed: {
    title() {
      return this.categoryId ? '编辑分类' : '添加分类';
    },
    categoryPicUrl: {
      get() {
        return this.form.picUrl ? this.form.picUrl.split(',').map(item => {
          return {
            fileName: item
          }
        }) : []
      },
      set(val) {
        this.$set(this.form, 'picUrl', val.map(item => { return item.fileName }).toString())
      }
    }
  },
  created() {
    if(this.categoryId){
      goodsCategoryDetail({ categoryId: this.categoryId }).then(res => {
        const { name, sort, picUrl, isShow, parentId } = res && res.data
        this.form = {
          name, sort, picUrl, isShow, parentId:parentId == 0?"":parentId
        }

      })
    }
    this.getParentList()
  },
  methods: {
    //获取一级列表
    getParentList(){
      goodsCategoryListTree( "pageNum=1&",{}).then((res) => {
          if (res.code == 0) {
            this.parentList = res.rows;
          }
        })
    },
    // 关闭弹框
    close() {
      this.$emit("close");
      this.loading = false;
      this.$refs["form"].resetFields();
    },

    // 确认
    saveClick() {
      this.loading = true;
      if(this.form.parentId == ""){
        this.form.parentId = 0
      }
      const subForm = this.$refs["form"];
      subForm.validate((valid) => {
        if (valid) {
          this.submitForm(this.form);
        } else {
          this.loading = false;
          // 提示第一个error
          this.getFormErrorMessage(subForm);
          return false;
        }
      });
    },

    // 提交
    submitForm(form) {
      let request = this.categoryId ? goodsCategoryUpdate : goodsCategoryCreate
      if(this.categoryId){
        form.categoryId = this.categoryId
      }
      request(form)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess(this.categoryId ? "修改成功" : '添加成功');
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped>
.channel {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
