<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    status-icon
   
    :size="'mini'"
    :rules="rulesThis"
    label-width="112px"
  >
    <el-row>
      <el-col :span="12">
        <el-form-item label="实时金价" prop="gold_price">
          <digital-input
            :values="ruleForm.gold_price"
            :placeholder="'修改实时金价'"
            :min="0"
            :max="100000000000"
            :position="'right'"
            :precision="2"
            :size="'mini'"
            :newTime="newTime"
            :controls="false"
            :append="'元'"
            @reschange="rate_change"
          />
        </el-form-item>
      </el-col>
      <el-col
        :span="12"
        style="text-align: right"
        
      >
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseIn/purchaseOrder";
import resToken from "@/mixins/resToken";

export default {
  name: "Account",
  props: ["showModel", "sitem", "id", "type",'newTime',"cost_detailArr"],
  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "projectDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  watch: {
    // showModel: function (val) {
    //   this.showModelThis = val;
    //   if (val) {
    //     this.initForm();
    //   }
    // },
    //    newTime: function (val) {
    //   if (val) {
    //     this.initForm();
    //   }
    // },
  },
  data() {
    const validate_sale_price = (rule, value, callback) => {
      if (value === "" || value === undefined) {
        callback(new Error("实时金价不能为空！"));
      } else if (
        value === "0" ||
        value === "0." ||
        value === "0.0" ||
        value === "0.00"
      ) {
        callback(new Error("实时金价不能为零！"));
      } else {
        callback();
      }
    };
    return {
      cost_detailArr:[],
      loading: false,
      ruleForm: {
        cgdNo: "",
        gold_price: "0",
      },
      rulesThis: this.rules,
      rules: {
        gold_price: [
          {
            required: true,
            validator: validate_sale_price,
            trigger: "blur",
          },
        ],
      },
      newTime:""
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    //项目毛利率编辑
    rate_change(e) {
      this.ruleForm.gold_price = e ;
      this.ruleForm.cgdNo = this.sitem.cgdNo;

      
      
      
      this.$refs.ruleForm.validateField("gold_price");
    },

    //初始化整个组件
    async initForm() {
      // this.loading = true;
      console.log(this.sitem)
      this.company = "";
      
      this.ruleForm = {
        cgdNo: this.sitem.cgdNo,
        gold_price: this.sitem.gold_price,
      };
      console.log(this.ruleForm )
      this.rulesThis = this.rules;
      this.newTime = new Date().valueOf()+""
      console.log(this.newTime )

      //新增商品信息请求
      // const res = await asyncRequest.cost_detail({ spuCode: this.sitem.spuCode });
      // if (res && res.code === 0 && res.data) {
      //   this.cost_detailArr = res.data;
      //   console.log(this.cost_detailArr)
      // } else if (res && res.code >= 100 && res.code <= 104) {
      //   await this.logout();
      // } else {
      //   this.$message.warning(res.message);
      // }
      
      // this.loading = false;
    },
    //初始化整个表单
    // async resetForm() {
    //   // 重置
    //   await this.$nextTick(() => {
    //     if (this.$refs.ruleForm) {
    //       this.$refs.ruleForm.resetFields();
    //       this.$refs.ruleForm.clearValidate();
    //       const { low_rate, cgdNo, status } = this.sitem;
    //       this.status = status || "";
    //       this.ruleForm = {
    //         cgdNo: cgdNo || "",
    //         gold_price: low_rate || "0",
    //       };
    //     }
    //   });
    // },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid)
        if (valid) {
            if (this.loading) {
            return;
          }
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = await asyncRequest.cgdeditgoldprice(model);
          this.loading = false;
          if (res && res.code === 0) {
            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>
.account {
}
</style>
