<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="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="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 { goodsTagCreate, goodsTagDetail, goodsTagUpdate } from "@/api/business/tag";
export default {
  mixins: [CustomFieldsMixin],
  props: {
    // 弹框显示
    dialogShow: {
      type: Boolean,
      default: false,
    },
    // 标签ID
    tagId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      form: {
        isShow: 1,
        sort: 0
      },
      rules: {
        name: [
          { required: true, message: "请输入标签名称", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "请输入排序", trigger: "blur" },
          {
            pattern: /^([0-9]\d*)$/,
            message: "请输入合法的数字",
            trigger: ["blur", "change"],
          },
        ],
        isShow:  [
          { required: true, message: "请选择", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    title() {
      return this.tagId ? '编辑标签' : '添加标签';
    },
  },
  created() {
    if(this.tagId){
      goodsTagDetail({ tagId: this.tagId }).then(res => {
        const { name, sort, isShow } = res && res.data
        this.form = {
          name, sort, isShow
        }
      })
    }
  },
  methods: {
    // 关闭弹框
    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 request = this.tagId ? goodsTagUpdate : goodsTagCreate
      if(this.tagId){
        form.tagId = this.tagId
      }
      request(form)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess(this.tagId ? "修改成功" : '添加成功');
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped>
.channel {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>