<template>
  <el-dialog
    v-loading="loading"
    :title="'编辑活动信息'"
    :center="true"
    align="left"
    top="12vh"
    width="600px"
    :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="skuCode">
              <el-input
                v-model="ruleForm.skuCode"
                placeholder="商品编号"
                maxlength="100"
                disabled
              />
            </el-form-item>
            <el-form-item label="商品名称" prop="good_name">
              <el-input
                v-model="ruleForm.good_name"
                disabled
                placeholder="商品名称"
                maxlength="100"
              />
            </el-form-item>
            <el-form-item label="活动库存" prop="activity_stock">
              <el-input
                v-model="ruleForm.activity_stock"
                placeholder="活动库存"
                disabled
                maxlength="100"
              />
            </el-form-item>
            <el-form-item label="起订量" prop="moq_num">
              <digital-input
                :values="ruleForm.moq_num"
                :placeholder="'起订量'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="0"
                :controls="false"
                :append="''"
                @reschange="moq_num_change"
              />
            </el-form-item>
            <el-form-item label="成本单价" prop="cost_price">
              <digital-input
                :values="ruleForm.cost_price"
                :placeholder="'起订量'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :controls="false"
                :append="'元'"
                @reschange="cost_price_change"
              />
            </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>
export default {
  name: "editMinorderForm",
  props: ["showModel", "sitem"],
  data() {
    const validate_moq_num = (rule, value, callback) => {
      let activity_stock = parseInt(this.ruleForm.activity_stock) || 0;
      if (value === "") {
        callback(new Error("起订量不能为空！"));
      } else if (value === "0") {
        callback(new Error("起订量不能为零！"));
      } else if (parseInt(value) > activity_stock) {
        callback(new Error("起订量不能大于活动库存数！"));
      } else {
        callback();
      }
    };
    const validate_cost_price = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("成本单价不能为空！"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      showModelThis: this.showModel,
      ruleForm: {
        skuCode: "",
        good_name: "",
        activity_stock: "",
        moq_num: "",
        cost_price: "",
      },
      rulesThis: this.rules,
      rules: {
        moq_num: [
          { required: true, validator: validate_moq_num, trigger: "blur" },
        ],
        cost_price: [
          {
            required: true,
            validator: validate_cost_price,
            trigger: "blur",
          },
        ],
      },
    };
  },
  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.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = this.sitem
            ? JSON.parse(JSON.stringify(this.sitem))
            : {
                skuCode: "",
                good_name: "",
                activity_stock: "",
                moq_num: "",
                cost_price: "",
              };
        }
      });
    },
    moq_num_change(e) {
      this.ruleForm.moq_num = e + "";
      this.$refs.ruleForm.validateField("moq_num");
    },
    cost_price_change(e) {
      this.ruleForm.cost_price = e + "";
      this.$refs.ruleForm.validateField("cost_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));
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh", model);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

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