<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="20"
                ><template slot="prepend" v-if="pid_name">{{
                  pid_name
                }}</template></el-input
              >
            </el-form-item>
            <el-form-item label="财务核算码" prop="fund_code">
              <el-input
                v-model="ruleForm.fund_code"
                disabled
                placeholder="财务核算码"
                maxlength="50"
              />
            </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"
                :append="'%'"
                @reschange="rate_change($event, 'order_rate')"
              />
            </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'"
                :controls="false"
                :append="'%'"
                @reschange="rate_change($event, 'sale_rate')"
              />
            </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"
                :append="'%'"
                @reschange="rate_change($event, 'rate')"
              />
            </el-form-item>
            <el-form-item label="财务利率" prop="money_rate">
              <digital-input
                :values="ruleForm.money_rate"
                :placeholder="'财务利率'"
                :min="-100"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="'%'"
                @reschange="rate_change($event, 'money_rate')"
              />
            </el-form-item>
            <el-form-item label="项目最低毛利" prop="low_rate">
              <digital-input
                :values="ruleForm.low_rate"
                :placeholder="'项目最低毛利'"
                :min="-100"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="'%'"
                @reschange="rate_change($event, 'low_rate')"
              />
            </el-form-item> -->
            <!-- <el-form-item label="BOSS毛利率" prop="lower_rate">
              <digital-input
                :values="ruleForm.lower_rate"
                :placeholder="'BOSS毛利率'"
                :min="-100"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="'%'"
                @reschange="rate_change($event, 'lower_rate')"
              />
            </el-form-item>-->
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="id !== '007'"
            type="primary"
            :size="'small'"
            @click="submitForm"
            >保 存
          </el-button>
          <el-button :size="'small'" @click="showModelThis = false">{{
            id == "007" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/youzan/othsearchSort";
import resToken from "@/mixins/resToken";
export default {
  name: "sort",
  props: ["showModel", "id", "sitem", "titleList"],
  mixins: [resToken],
  data() {
    const validate_rate = (rule, value, callback) => {
      const { required } = rule;
      if (value === "" && required) {
        callback(new Error("主管利率毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_order_rate = (rule, value, callback) => {
      const { required } = rule;
      if (value === "" && required) {
        callback(new Error("预算→成本毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_sale_rate = (rule, value, callback) => {
      const { required } = rule;
      if (value === "" && required) {
        callback(new Error("成本→售价毛利率不能为空！"));
      } else {
        callback();
      }
    };

    const validate_lower_rate = (rule, value, callback) => {
      const { required } = rule;
      if (value === "" && required) {
        callback(new Error("BOSS毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_low_rate = (rule, value, callback) => {
      const { required } = rule;
      if (value === "" && required) {
        callback(new Error("项目最低毛利率不能为空！"));
      } else {
        callback();
      }
    };
    const validate_money_rate = (rule, value, callback) => {
      const { required } = rule;
      if (value === "" && required) {
        callback(new Error("财务毛利率不能为空！"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      showModelThis: this.showModel,
      pid_name: "",
      ruleForm: {
        platform_id: "",
        cat_id: "",
        cat_name: "",
        order_rate: "0",
        sale_rate: "0",
        lower_rate: "0",
        rate: "0",
        low_rate: "0",
        money_rate: "0",
      },
      rulesThis: this.rules,
      rules: {
        order_rate: [{ required: true, validator: validate_order_rate, trigger: "blur" }],
        sale_rate: [{ required: true, validator: validate_sale_rate, trigger: "blur" }],
        lower_rate: [
          { required: false, validator: validate_lower_rate, trigger: "blur" },
        ],
        rate: [{ required: true, validator: validate_rate, trigger: "blur" }],

        low_rate: [{ required: true, validator: validate_low_rate, trigger: "blur" }],
        money_rate: [{ required: true, validator: validate_money_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.pid_name = "";
      this.titleList.forEach((e, i) => {
        this.pid_name += i !== 0 ? `${e.titleName}/` : "";
      });
      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();
          const {
            platform_id,
            id,
            cat_name,
            order_rate,
            sale_rate,
            lower_rate,
            rate,
            fund_code,
            low_rate,
            money_rate,
          } = this.sitem;
          this.ruleForm = {
            platform_id: platform_id + "" || "",
            cat_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",
            low_rate: low_rate || "0",
            money_rate: money_rate || "0",
          };
        }
      });
    },
    rate_change(e, key) {
      this.ruleForm[key] = e + "";
      this.$refs.ruleForm.validateField(key);
    },
    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));
            delete model["cat_name"];
            // delete model["pid"];
            // delete model["fund_code"];
            const { code, data, message } = await asyncRequest.update(model);
            this.loading = false;
            if (code === 0) {
              this.$notify.success({
                title: "毛利率设置成功！",
                message: "",
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh");
            } else if (code >= 100 && code <= 104) {
              await this.logout();
            } else if (code === 10004) {
              const { skuCode } = data;
              let resList = JSON.parse(JSON.stringify(skuCode));

              let htmlList = "<ul>";
              resList.forEach((v) => {
                htmlList += `<li><span>${v.skuCode}__</span><span>${v.good_name}</span></li>`;
              });
              htmlList += "</ul>";
              this.$notify({
                title: "新毛利已影响以下线上商品售价，请下线后再修改！",
                dangerouslyUseHTMLString: true,
                message: htmlList,
              });
            } else {
              this.$message.warning(message);
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

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