<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    :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="spec_id">
              <search-spec
                :value="ruleForm.spec_id"
                :disabled="sitem.isMust"
                :size="'mini'"
                :isDetail="false"
                :placeholder="'规格类型'"
                @searchChange="spec_idsearchChange"
              />
            </el-form-item>
            <el-form-item label="规格值" prop="spec_value_id">
              <div
                v-if="specVlist.length > 0"
                style="max-height: 600px; overflow-y: scroll"
              >
                <el-radio-group
                  v-model="ruleForm.spec_value_id"
                  @change="spec_value_id_change"
                >
                  <el-radio
                    v-for="item in specVlist"
                    :key="item.id + item.spec_value"
                    :label="item.id"
                    >{{ item.spec_value }}</el-radio
                  >
                </el-radio-group>
              </div>
              <div class="no-data" v-else>暂无规格值,请添加！</div>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-input
            style="width: 209px"
            v-model="sinput"
            class="fl"
            :disabled="id == 'edit'"
            :size="'small'"
            placeholder="规格值名称，如红色"
            maxlength="100"
          />
          <el-button
            style="margin: 0 0 0 10px"
            icon="el-icon-plus"
            :size="'small'"
            @click="add_spec"
            class="fl"
            >新增规格值</el-button
          >
          <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/goodsCost";
import resToken from "@/mixins/resToken";
export default {
  name: "brand",
  props: ["showModel", "index", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "商品规格",
      sinput: "",
      showModelThis: this.showModel,
      specVlist: [],
      ruleForm: {},
      rulesThis: this.rules,

      rules: {
        spec_id: [
          {
            required: true,
            message: "规格类型不能为空",
            trigger: "change",
          },
        ],
        spec_value_id: [{ 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;
      this.sinput = "";
      this.resetFormData();
      this.rulesThis = this.rules;
      if (this.index + "" === "-1") {
        this.title = "添加商品规格值";
      } else {
        this.title = "修改商品规格值";
      }
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.resetFormData();
        }
      });
    },
    async resetFormData() {
      const {
        id,
        spec_id,
        spec_value,
        spec_value_id,
        spec_value_value,
        isMust,
      } = this.sitem;
      this.ruleForm = {
        index: this.index,
        id: id || "",
        spec_id: spec_id || "",
        spec_value: spec_value || "",
        isMust: isMust || false,
        spec_value_id: spec_value_id || "",
        spec_value_value: spec_value_value || "",
      };
      if (spec_id) {
        await this.getlist();
      }
    },
    async add_spec() {
      const { spec_id } = this.ruleForm;
      if (spec_id) {
        if (this.sinput) {
          const { code, data, message } = await asyncRequest.valueadd({
            spec_id: spec_id,
            spec_value: this.sinput,
          });
          if (code === 0) {
            await this.getlist();
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
        } else {
          this.$message.warning("请输入规格值！");
        }
      } else {
        this.$message.warning("请选择规格类型！");
      }
    },
    async spec_idsearchChange(e) {
      const { id, spec_name } = e;
      this.ruleForm.spec_id = id || "";
      this.ruleForm.spec_value = spec_name || "";
      this.$refs.ruleForm.validateField("spec_id");
      await this.getlist();
    },
    spec_value_id_change(e) {
      if (e) {
        const { id, spec_value } = this.specVlist.find((i) => i.id === e);
        this.ruleForm.spec_value_id = id || "";
        this.ruleForm.spec_value_value = spec_value || "";
      } else {
        this.ruleForm.spec_value_id = "";
        this.ruleForm.spec_value_value = "";
      }
      this.$refs.ruleForm.validateField("spec_value_id");
    },
    async getlist() {
      const { spec_id } = this.ruleForm;
      if (spec_id) {
        const { code, data, message } = await asyncRequest.valueall({
          spec_id: spec_id,
        });
        if (code === 0) {
          this.specVlist = data;
        } else if (code >= 100 && code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(message);
        }
      } else {
        this.specVlist = [];
      }
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.showModelThis = false;
          this.$emit("refresh", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.goodsCostAdd {
  .no-data {
    text-align: center;
    line-height: 32px;
    color: #8492a6;
    // border: 1px solid #ccc;
    border-radius: 6px;
  }
}
</style>
