<template>
  <el-dialog
    v-loading="loading"
    :title="'编辑活动信息'"
    :center="true"
    align="left"
    top="12vh"
    width="600px"
    :size="'mini'"
    :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="商品编号"
                minlength="100"
                disabled
              />
            </el-form-item>
            <el-form-item label="商品名称" prop="good_name">
              <el-input
                v-model="ruleForm.good_name"
                disabled
                placeholder="商品名称"
                minlength="100"
              />
            </el-form-item>
            <el-form-item label="活动库存" prop="activity_stock">
              <el-input
                v-model="ruleForm.activity_stock"
                disabled
                placeholder="活动库存"
                minlength="100"
              />
            </el-form-item>
            <el-form-item label="起订量" prop="moq_num">
              <el-input
                v-model="ruleForm.moq_num"
                disabled
                placeholder="起订量"
                minlength="100"
              />
            </el-form-item>
            <el-form-item label="成本单价" prop="cost_price">
              <el-input
                v-model="ruleForm.cost_price"
                disabled
                placeholder="成本单价"
                minlength="100"
              >
                <template slot="append">元</template></el-input
              >
            </el-form-item>
            <el-form-item label="非活动价" prop="sale_price">
              <el-input
                v-model="ruleForm.sale_price"
                disabled
                placeholder="非活动价"
                minlength="100"
              >
                <template slot="append">元</template></el-input
              >
            </el-form-item>
            <el-form-item label="活动价" prop="activity_price">
              <digital-input
                :values="ruleForm.activity_price"
                :placeholder="'活动价'"
                :min="0"
                :max="10000000000"
                :position="'right'"
                :precision="2"
                :controls="false"
                :append="'元'"
                @reschange="activity_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_activity_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: "",
        sale_price: "",
        activity_price: "",
      },
      rulesThis: this.rules,
      rules: {
        activity_price: [
          {
            required: true,
            validator: validate_activity_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: "",
                sale_price: "",
                activity_price: "",
              };
        }
      });
    },
    activity_price_change(e) {
      this.ruleForm.activity_price = e + "";
      this.$refs.ruleForm.validateField("activity_price");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          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>
