<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="500px"
    :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: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :size="'small'"
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="规格类型" prop="active_name">
              <search-spec
                :value="ruleForm.active_name"
                :disabled="false"
                :size="'mini'"
                :isDetail="false"
                :placeholder="'规格类型'"
                @searchChange="active_namesearchChange"
              />
            </el-form-item>
            <el-form-item label="规格值" prop="logo_url">
              <el-radio-group v-model="ruleForm.logo_url">
                <el-radio
                  v-for="item in specVlist"
                  :key="item.id + item.name"
                  :label="item.id"
                  >{{ item.name }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
            <div style="width: 100%; padding: 0 0 0 80px">
              <el-input
                style="width: 209px"
                v-model="sinput"
                :disabled="id == 'edit'"
                :size="'small'"
                placeholder="规格值名称，如红色"
                maxlength="20"
              />
              <el-button
                style="margin: 0 0 0 10px"
                icon="el-icon-plus"
                :size="'small'"
                >新增规格值</el-button
              >
            </div>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right; padding-top: 18px">
          <el-button
            v-if="id !== 'edit'"
            :size="'small'"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false" :size="'small'">{{
            id == "edit" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/goodStore/active";
import resToken from "@/mixins/resToken";
export default {
  name: "active",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "",
      sinput: "",
      showModelThis: this.showModel,
      select: "1",
      specVlist: [
        { id: "1", name: "111" },
        { id: "2", name: "222" },
      ],
      activeOptions: [],
      actionList: [],
      ruleForm: {
        id: "",
        active_name: "",
        logo_url: "",
      },
      rulesThis: this.rules,
      rules: {
        active_name: [
          { required: true, message: "品牌名称不能为空", trigger: "change" },
        ],
        logo_url: [
          { required: true, message: "品牌名称不能为空", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.title = "添加商品类型";
        this.rulesThis = this.rules;
      } else if (this.id === "005") {
        this.title = "修改商品类型";
        this.rulesThis = this.rules;
      } else {
        this.title = "品牌商品类型";
        this.rulesThis = {};
      }
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { id, active_name, logo_url } = this.sitem;
          this.ruleForm = {
            id: id || "",
            active_name: active_name || "",
            logo_url: logo_url || "",
          };
        }
      });
    },

    active_namesearchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.active_name = code;
      } else {
        this.ruleForm.active_name = "";
      }
      this.$refs.ruleForm.validateField("active_name");
      this.getlist();
    },
    async getlist() {
      const { active_name } = this.ruleForm;
      const { code, data, message } = await asyncRequest.list({
        active_name: active_name,
      });
      if (code === 0) {
        this.options = data;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "003") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功!" : "修改成功!";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
     async submitForm1() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = {};
          if (this.id === "003") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功!" : "修改成功!";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.active {
}
</style>
