<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"
    append-to-body
  >
    <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="specid">
              <search-spec
                :value="ruleForm.specid"
                :disabled="false"
                :size="'mini'"
                :isDetail="false"
                :placeholder="'规格类型'"
                @searchChange="specidsearchChange"
              />
            </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="20"
          />
          <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: {
        specid: [
          {
            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.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(async () => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          if (this.index + "" !== "-1") {
            this.specVlist = [];
            const { specid, spec_name, spec_value_id, spec_value_name } = this.sitem;
            this.ruleForm = {
              index: this.index,
              specid: specid || "",
              spec_name: spec_name || "",
              spec_value_id: spec_value_id || "",
              spec_value_name: spec_value_name || "",
            };
            await this.getlist();
            console.log(this.ruleForm);
          }
        }
      });
    },
    resetFormData() {
      this.specVlist = [];
      this.ruleForm = {
        index: "-1",
        specid: "",
        spec_name: "",
        spec_value_id: "",
        spec_value_name: "",
      };
    },
    async add_spec() {
      const { specid } = this.ruleForm;
      if (specid) {
        if (this.sinput) {
          const { code, data, message } = await asyncRequest.valueadd({
            spec_id: specid,
            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 specidsearchChange(e) {
      const { id, spec_name } = e;
      this.ruleForm.specid = id || "";
      this.ruleForm.spec_name = spec_name || "";
      this.$refs.ruleForm.validateField("specid");
      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_name = spec_value || "";
      } else {
        this.ruleForm.spec_value_id = "";
        this.ruleForm.spec_value_name = "";
      }
      this.$refs.ruleForm.validateField("spec_value_id");
    },
    async getlist() {
      const { specid } = this.ruleForm;
      if (specid) {
        const { code, data, message } = await asyncRequest.valueall({
          spec_id: specid,
        });
        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;
          console.log(this.ruleForm);
          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>
