<template>
  <div>
    <el-dialog
      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="name">
          <el-input
            v-model="form.name"
            style="width: 300px"
            size="small"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <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
            :limit="1"
            :value="form.picUrl"
            :file-size="50"
            :is-public="true"
            @input="pictureSelect"
          />
        </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="2">否</el-radio>
          </el-radio-group>
        </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 } from "@/api/business/category";
export default {
  mixins: [CustomFieldsMixin],
  props: {
    // 弹框显示
    dialogShow: {
      type: Boolean,
      default: false,
    },
  },
  created() {

  },
  data() {
    return {
      loading: false,
      form: {
        picUrl: [],
        isShow: 1,
      },
      rules: {
        name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
        picUrl: [
          {
            type: "array",
            required: true,
            message: "请上传分类图片",
            trigger: "change",
          },
        ],
        isShow:  [
          { required: true, message: "请选择", trigger: "change" },
        ],
      },
    };
  },
  methods: {

    changeType(){
      this.$refs["form"].clearValidate();
    },

    // 添加图片
    pictureSelect(data) {
      this.form.picUrl = data;
    },

    // 关闭弹框
    close() {
      this.$emit("close");
      this.loading = false;
      this.$refs["form"].resetFields();
    },

    // 确认
    saveClick() {
      this.loading = true;
      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 data = {
        ... form,
        picUrl: this.form.picUrl[0].fileName,
      };
      goodsCategoryCreate(data)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess("添加成功");
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped>
.channel {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>