<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="700px"
    :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
            :size="'small'"
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="起订量" prop="min_num">
                  <digital-input
                    :values="ruleForm.min_num"
                    :placeholder="'起订量'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="0"
                    :size="'mini'"
                    :controls="false"
                    :append="''"
                    @reschange="number_change($event, 'min_num')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="成本单价" prop="nake_fee">
                  <digital-input
                    :values="ruleForm.nake_fee"
                    :placeholder="'成本单价'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'nake_fee')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="工艺费" prop="cost_fee">
                  <digital-input
                    :values="ruleForm.cost_fee"
                    :placeholder="'工艺费'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'cost_fee')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="物流费" prop="delivery_fee">
                  <digital-input
                    :values="ruleForm.delivery_fee"
                    :placeholder="'物流费'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'delivery_fee')"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="证书费" prop="cert_fee">
                  <digital-input
                    :values="ruleForm.cert_fee"
                    :placeholder="'证书费'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'cert_fee')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="加标费" prop="mark_fee">
                  <digital-input
                    :values="ruleForm.mark_fee"
                    :placeholder="'加标费'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'mark_fee')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="包装费" prop="package_fee">
                  <digital-input
                    :values="ruleForm.package_fee"
                    :placeholder="'包装费'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'package_fee')"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="其他费用" prop="other_fee">
                  <digital-input
                    :values="ruleForm.other_fee"
                    :placeholder="'其他费用'"
                    :min="0"
                    :disabled="false"
                    :max="100000000000"
                    :position="'right'"
                    :precision="2"
                    :size="'mini'"
                    :controls="false"
                    :append="'元'"
                    @reschange="number_change($event, 'other_fee')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24" style="text-align: right">
                <el-button :size="'small'" type="primary" @click="submitForm"
                  >保 存
                </el-button>
                <el-button @click="showModelThis = false" :size="'small'">{{
                  "取 消"
                }}</el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import resToken from "@/mixins/resToken";
export default {
  name: "brand",
  props: ["showModel", "index", "sitem"],
  mixins: [resToken],
  data() {
    const validate_num_0 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("起订量不能为空！"));
      } else if (value === "0") {
        callback(new Error("起订量不能为零！"));
      } else {
        callback();
      }
    };
    const validate_num = (rule, value, callback) => {
      const { required } = rule;
      if (required && value === "") {
        callback(new Error("不能为空！"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      title: "",
      showModelThis: this.showModel,
      ruleForm: {},
      rulesThis: this.rules,
      rules: {
        min_num: [
          { required: true, validator: validate_num_0, trigger: "blur" },
        ],
        nake_price: [
          {
            required: true,
            validator: validate_num,
            trigger: "blur",
          },
        ],
        //固定成本
        cert_fee: [
          {
            required: true,
            validator: validate_num,
            trigger: "blur",
          },
        ],
        package_fee: [
          {
            required: true,
            validator: validate_num,
            trigger: "blur",
          },
        ],

        cost_fee: [
          {
            required: true,
            validator: validate_num,
            trigger: "blur",
          },
        ],

        mark_fee: [
          {
            required: true,
            validator: validate_num,
            trigger: "blur",
          },
        ],
        nake_fee: [
          { required: true, validator: validate_num, trigger: "blur" },
        ],
        delivery_fee: [
          { required: true, validator: validate_num, 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;
      // await this.resetFormData();

      console.log(this.index);
      if (this.index + "" === "-1") {
        this.title = "添加成本阶梯";
      } else {
        this.title = "修改成本阶梯";
      }
      this.rulesThis = this.rules;
      await this.resetForm();

      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(async () => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          await this.resetFormData();
        }
      });
    },
    async resetFormData() {
      if (this.index + "" === "-1") {
        this.ruleForm = {
          id: "",
          index: "-1",
          min_num: "0",
          nake_fee: "0",
          cost_fee: "0",
          delivery_fee: "0",
          cert_fee: "0",
          mark_fee: "0",
          package_fee: "0",
          other_fee: "0",
        };
      } else {
        const {
          id,
          min_num,
          nake_fee,
          cost_fee,
          delivery_fee,
          cert_fee,
          mark_fee,
          package_fee,
          other_fee,
        } = this.sitem;
        this.ruleForm = {
          id: id || "",
          index: this.index,
          min_num: min_num || "0",
          cost_fee: cost_fee || "0",
          nake_fee: nake_fee || "0",
          delivery_fee: delivery_fee || "0",
          cert_fee: cert_fee || "0",
          mark_fee: mark_fee || "0",
          package_fee: package_fee || "0",
          other_fee: other_fee || "0",
        };
      }
    },

    number_change(e, key) {
      this.ruleForm[key] = e + "" || "0";
      this.$refs.ruleForm.validateField(key);
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.showModelThis = false;
          // 刷新
          this.$emit("refresh", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

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