<template>
  <el-dialog
    v-loading="loading"
    :title="'添加' + title"
    :center="true"
    align="left"
    top="10vh"
    width="400px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            v-loading="loading"
            :model="ruleForm"
            status-icon
            :label-position="'left'"
            size="mini"
            :rules="rulesThis"
            label-width="100"
          >
            <el-form-item prop="name" label="名称">
              <el-input v-model="ruleForm.name" placeholder="名称" />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" class="bottom-btn tr">
          <el-button type="primary" @click="submitForm" :size="'mini'">保 存 </el-button>
          <el-button @click="showModelThis = false" :size="'mini'">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>

export default {
  name: "shop",
  props: ["showModel", "title", "valKey", "option"],
  data() {
    return {
      loading: false,
      showModelThis: this.showModel,
      ruleForm: {
        name: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "change",
          },
        ],
      },
      rulesThis: this.rules,
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {},
  methods: {
    async initForm() {
      this.checkAll = false;
      console.log(this.valKey);
      this.loading = true;
      this.alterOption = [];
      await this.resetForm();
      this.rulesThis = this.rules;
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            name: "",
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const { name } = this.ruleForm;
          console.log(this.option);
          let index = this.option.findIndex((s) => s === name);
          if (index !== -1) {
            this.$message.warning("已有" + name, "不能重复添加！");
            return;
          }
          this.$emit("refresh", {
            valKey: this.valKey,
            name: name,
          });
          this.showModelThis = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
