<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
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="金属类型" prop="type">
              <search-metal-kind
                :value="ruleForm.type"
                :disabled="id !== '003'"
                :isDetail="false"
                :placeholder="'金属类型'"
                @searchChange="noble_metalsearchChange"
              />
            </el-form-item>
            <el-form-item label="最新价格" prop="price" :disabled="id == '007'">
              <digital-input
                :values="ruleForm.price"
                :placeholder="'最新价格'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :controls="false"
                :disabled="id === '007'"
                :append="'元'"
                @reschange="reschange"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="id !== '007'" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            id == "007" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/goodStore/goldPrice";
import resToken from "@/mixins/resToken";
export default {
  name: "goldPrice",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加实时金价",
      showModelThis: this.showModel,

      actionList: [],
      ruleForm: {
        id: "",
        price: 0,
        type: "",
      },
      rulesThis: this.rules,
      rules: {
        price: [
          { required: true, message: "最新价格不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "金属类型不能为空", trigger: "chenge" },
        ],
      },
    };
  },
  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 === "003") {
        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, price, type } = this.sitem;
          this.ruleForm = {
            id: id || "",
            price: price || "0",
            type: type || "",
          };
        }
      });
    },
    //贵金属种类选择
    noble_metalsearchChange(e) {
      console.log(e);
      const { id, code, label } = e;
      this.ruleForm.type = id ? id : "";
      this.$refs.ruleForm.validateField("type");
    },
    reschange(e) {
      this.ruleForm.price = e;
      this.$refs.ruleForm.validateField("price");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            let model = JSON.parse(JSON.stringify(this.ruleForm));
            model.price += "";
            model.type += "";
            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>
.goldPrice {
}
</style>
