<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="15vh"
    width="500px"
    :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="85px"
            class="demo-ruleForm"
          >
            <el-form-item label="起订量" prop="min_num">
              <digital-input
                :values="ruleForm.min_num"
                :placeholder="'起订量'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="0"
                :controls="false"
                :size="'small'"
                :disabled="id === '007'"
                :append="''"
                @reschange="min_num_change"
              />
            </el-form-item>
            <el-form-item label="系统售价" prop="new_sale_price">
              <el-input
                placeholder="系统售价"
                v-loading="search_loading"
                :disabled="true"
                maxlength="20"
                v-model="ruleForm.new_sale_price"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="售价" prop="sale_price">
              <digital-input
                :values="ruleForm.sale_price"
                :placeholder="'售价'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :controls="false"
                :size="'small'"
                :disabled="id === '007'"
                :append="'元'"
                @reschange="sale_price_change"
              />
            </el-form-item>
            <el-form-item label="市场价" prop="market_price">
              <digital-input
                :values="ruleForm.market_price"
                :placeholder="'市场价'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :controls="false"
                :size="'small'"
                :disabled="id === '007'"
                :append="'元'"
                @reschange="market_price_change"
              />
            </el-form-item>

            <el-form-item label="对比平台" prop="market_platform">
              <el-input
                placeholder="对比平台"
                v-model="ruleForm.market_platform"
                maxlength="50"
              />
            </el-form-item>
            <el-row :gutter="10">
              <el-col :span="16">
                <el-form-item label="状态" prop="status">
                  <el-radio-group v-model="ruleForm.status">
                    <el-radio :label="'1'">启用</el-radio>
                    <el-radio :label="'0'">禁用</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8" 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";
import asyncRequest from "@/apis/service/goodStore/goodsOnline";
export default {
  name: "brand",
  props: ["showModel", "index", "sitem", "spuCode"],
  mixins: [resToken],
  data() {
    const validate_min_num = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("起订量不能为空！"));
      } else if (value === "0") {
        callback(new Error("起订量不能为零！"));
      } else {
        callback();
      }
    };
    const validate_sale_price = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("售价不能为空！"));
      } else {
        callback();
      }
    };
    const validate_market_price = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("市场价不能为空！"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      title: "",
      showModelThis: this.showModel,
      ruleForm: {
        id: "",
        index: "-1",
        min_num: "0",
        sale_price: "0",
        market_price: "0",
        market_platform: "",
        new_sale_price: "",
        status: "1",
        is_del: "0",
      },
      rulesThis: this.rules,
      search_loading: false,
      rules: {
        min_num: [
          {
            required: true,
            validator: validate_min_num,
            trigger: "blur",
          },
        ],
        sale_price: [
          {
            required: true,
            validator: validate_sale_price,
            trigger: "blur",
          },
        ],
        market_price: [
          {
            required: true,
            type: "number",
            validator: validate_market_price,
            trigger: "blur",
          },
        ],
        market_platform: [
          { required: true, message: "对比平台不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择使用状态", trigger: "change" },
        ],
      },
    };
  },
  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.resetForm();
      this.rulesThis = this.rules;
      if (this.index + "" === "-1") {
        this.title = "添加销售成本阶梯";
      } else {
        this.title = "修改销售成本阶梯";
      }

      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() {
      const {
        id,
        min_num,
        sale_price,
        market_price,
        market_platform,
        status,
        is_del,
      } = this.sitem;
      this.ruleForm = {
        id: id || "",
        index: this.index,
        min_num: min_num || "0",
        sale_price: sale_price || "0",
        market_price: market_price || "0",
        market_platform: market_platform || "",
        new_sale_price: "",
        status: status || "1",
        is_del: is_del || "0",
      };
      await this.get_new(this.ruleForm.min_num);
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.showModelThis = false;
          // 刷新
          this.$emit("refresh", this.ruleForm);
        } else {
          console.log(this.ruleForm);
          console.log("error submit!!");
          return false;
        }
      });
    },
    async min_num_change(e) {
      this.ruleForm.min_num = e + "";
      this.$refs.ruleForm.validateField("min_num");
      await this.get_new(this.ruleForm.min_num);
    },
    async get_new(min_num) {
      if (!this.search_loading) {
        if (min_num === "" || min_num === "0") {
          this.ruleForm.new_sale_price = "0";
        } else {
          this.search_loading = true;
          let { code, data, message } = await asyncRequest.goodupprice({
            min_num: min_num,
            spuCode: this.spuCode,
          });
          this.search_loading = false;
          if (code === 0) {
            const { sale_price } = data;
            this.ruleForm.new_sale_price = sale_price;
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else if (code == 1010) {
            this.ruleForm.new_sale_price = "0";
          } else {
            this.$message.warning(message);
          }
        }
      }
    },
    sale_price_change(e) {
      this.ruleForm.sale_price = e;
      this.$refs.ruleForm.validateField("sale_price");
    },
    market_price_change(e) {
      this.ruleForm.market_price = e;
      this.$refs.ruleForm.validateField("market_price");
    },
    reschange(e) {
      this.ruleForm.price = e;
      this.$refs.ruleForm.validateField("price");
    },
  },
};
</script>

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