<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="0">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :size="'small'"
            :rules="rulesThis"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="分类名称" prop="cat_name">
              <el-input
                v-model="ruleForm.cat_name"
                disabled
                placeholder="分类名称"
                maxlength="100"
              />
            </el-form-item>
            <el-form-item label="财务核算码" prop="fund_code">
              <el-input
                v-model="ruleForm.fund_code"
                disabled
                placeholder="财务核算码"
                maxlength="100"
              />
            </el-form-item>
            <el-form-item label="预算→成本" prop="order_rate">
              <digital-input
                :values="ruleForm.order_rate"
                :placeholder="'预算→成本'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                disabled
                :append="'%'"
                @reschange="order_rate_change"
              />
            </el-form-item>
            <el-form-item label="成本→售价" prop="sale_rate">
              <digital-input
                :values="ruleForm.sale_rate"
                :placeholder="'成本→售价'"
                :min="-100"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                disabled
                :controls="false"
                :append="'%'"
                @reschange="sale_rate_change"
              />
            </el-form-item>
            <el-form-item label="最低毛利" prop="lower_rate">
              <digital-input
                :values="ruleForm.lower_rate"
                :placeholder="'最低毛利'"
                :min="-100"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                disabled
                :append="'%'"
                @reschange="lower_rate_change"
              />
            </el-form-item>
            <el-form-item label="单品修改下限" prop="rate">
              <digital-input
                :values="ruleForm.rate"
                :placeholder="'单品修改下限'"
                :min="-100"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                disabled
                :append="'%'"
                @reschange="rate_change"
              />
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/goodStore/sort";
import resToken from "@/mixins/resToken";
export default {
  name: "sort",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    const validate_order_rate = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("预算→成本毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_sale_rate = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("成本→售价毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_lower_rate = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("最低毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_rate = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("单品修改下限毛利率不能为空！"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      showModelThis: this.showModel,
      ruleForm: {
        id: "",
        cat_name: "",
        order_rate: "0",
        sale_rate: "0",
        lower_rate: "0",
        rate: "0",
      },
      rulesThis: this.rules,
      rules: {
        fund_code: "",
        order_rate: [
          { required: true, validator: validate_order_rate, trigger: "blur" },
        ],
        sale_rate: [
          { required: true, validator: validate_sale_rate, trigger: "blur" },
        ],
        lower_rate: [
          { required: true, validator: validate_lower_rate, trigger: "blur" },
        ],
        rate: [{ required: true, validator: validate_rate, 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();
      await this.initData();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const {
            pid,
            id,
            cat_name,
            order_rate,
            sale_rate,
            lower_rate,
            rate,
            fund_code,
          } = this.sitem;
          this.ruleForm = {
            pid: pid || "",
            id: id || "",
            cat_name: cat_name || "",
            fund_code: fund_code || "",
            order_rate: order_rate || "0",
            sale_rate: sale_rate || "0",
            lower_rate: lower_rate || "0",
            rate: rate || "0",
          };
        }
      });
    },
    order_rate_change(e) {
      this.ruleForm.order_rate = e + "";
      this.$refs.ruleForm.validateField("order_rate");
    },
    sale_rate_change(e) {
      this.ruleForm.sale_rate = e + "";
      this.$refs.ruleForm.validateField("sale_rate");
    },
    lower_rate_change(e) {
      this.ruleForm.lower_rate = e + "";
      this.$refs.ruleForm.validateField("lower_rate");
    },
    rate_change(e) {
      this.ruleForm.rate = e + "";
      this.$refs.ruleForm.validateField("rate");
    },

    async initData() {
      const res = await asyncRequest.detail({ id: this.sitem.id });
      if (res && res.code === 0 && res.data) {
        console.log(res.data);
        const { pid, id, cat_name, order_rate, sale_rate, lower_rate, rate } =
          res.data;
        this.ruleForm = {
          pid: pid || "",
          id: id || "",
          cat_name: cat_name || "",
          order_rate: order_rate || "0",
          sale_rate: sale_rate || "0",
          lower_rate: lower_rate || "0",
          rate: rate || "0",
        };
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>

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