<template>
  <el-dialog
    v-loading="loading"
    :title="'申请议价'"
    :center="true"
    align="left"
    top="10vh"
    width="900px"
    :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-top: -20px">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :size="'mini'"
        :rules="rulesThis"
        label-width="102px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="20">
            <el-form-item label="商品名称">
              <el-input
                v-if="show_sitem"
                v-model="show_sitem.good_name"
                maxlength="100"
                disabled
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="4">
            <el-form-item label="图片" style="margin: 0" label-width="44px">
              <img
                class="hover"
                v-viewer
                :src="show_sitem.good_img"
                alt=""
                v-if="show_sitem && show_sitem.good_img"
                style="width: 35px; height: 35px"
              />
            </el-form-item>
          </el-col> -->
          <el-col :span="4">
            <el-form-item label="单位" label-width="50px">
              <el-input
                v-if="show_sitem"
                maxlength="10"
                v-model="show_sitem.unit"
                disabled
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="议价前售价">
              <el-input
                v-if="show_sitem"
                v-model="show_sitem.origin_price"
                maxlength="20"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="当前售价">
              <el-input
                v-if="show_sitem"
                v-model="show_sitem.sale_price"
                maxlength="20"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="期望售价" prop="bargain_price">
              <digital-input
                :values="ruleForm.bargain_price"
                :name="'ruleForm.bargain_price'"
                :placeholder="'期望售价'"
                :min="0"
                :disabled="sitem.is_gold_price == 1 && sitem.can[0].id == 6"
                :max="
                  show_sitem && show_sitem.origin_price ? show_sitem.origin_price : '0'
                "
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="'元'"
                @reschange="number_change($event, 'bargain_price')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="议价前工艺费">
              <el-input
                v-if="show_sitem"
                v-model="show_sitem.cost_fee"
                maxlength="20"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="议价前工艺费">
              <el-input
                v-if="show_sitem"
                v-model="show_sitem.sale_cost_fee"
                maxlength="20"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="期望工艺费" prop="cost_price">
              <digital-input
                :values="ruleForm.cost_price"
                :name="'ruleForm.cost_price'"
                :placeholder="'期望工艺费'"
                :min="0"
                :disabled="!(sitem.is_gold_price == 1 && sitem.can[0].id == 6)"
                :max="show_sitem && show_sitem.cost_fee ? show_sitem.cost_fee : '0'"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="'元'"
                @reschange="number_change($event, 'cost_price')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="议价原因" prop="result_info_id">
              <abnormal-cause
                :value="ruleForm.result_info_id"
                :placeholder="'议价原因'"
                :type="'7'"
                :disabled="false"
                :size="'mini'"
                @searchChange="select_change"
              />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="议价备注:" prop="bargain_remark" label-width="88px">
              <el-input
                type="textarea"
                :rows="2"
                maxlength="250"
                show-word-limit
                placeholder="议价备注"
                v-model="ruleForm.bargain_remark"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" style="text-align: right">
            <el-button @click="showModelThis = false" :size="'mini'">{{
              isDetail ? "关 闭" : "取 消"
            }}</el-button>
            <el-button v-if="!isDetail" type="primary" :size="'mini'" @click="submitForm"
              >保 存
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/bargainList";
import resToken from "@/mixins/resToken";
import { add_sum } from "@/utils/validate";
export default {
  name: "Account",
  props: ["showModel", "sitem"],
  mixins: [resToken],
  data() {
    return {
      roleList: [],
      loading: false,
      title: "添加账号",
      organizeList: [],
      showModelThis: this.showModel,
      ruleForm: {
        result_info_id: "",
        bargain_price: "0",
        result_info_id_name: "",
        bargain_remark: "",
        cost_price: "0",
      },
      show_sitem: null,
      rulesThis: this.rules,
      rules: {
        result_info_id: [
          {
            required: true,
            message: "请选择议价原因！",
            trigger: "change",
          },
        ],
        bargain_price: [
          {
            required: true,
            message: "期望售价不能为空！",
            trigger: "blur",
          },
        ],
        cost_price: [
          {
            required: true,
            message: "期望工艺费不能为空！",
            trigger: "blur",
          },
        ],
        bargain_remark: [
          {
            required: true,
            message: "议价备注不能为空！",
            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;
    },

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

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        this.show_sitem = JSON.parse(JSON.stringify(this.sitem));
        const { origin_price, specinfo, cost_fee, bidNo } = this.sitem;
        if (specinfo && specinfo.length > 0) {
          specinfo.forEach((a, ai) => {
            this.show_sitem.good_name += `${ai === 0 ? "_" : "-"}${a.spec_name}[${
              a.spec_value_name
            }]`;
          });
        }
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            bargain_price: origin_price || "0",
            bidNo: bidNo || "",
            bargain_remark: "",
            cost_price: cost_fee || "0",
            result_info_id: "",
            result_info_id_name: "",
          };
        }
      });
    },
    select_change(e) {
      const { code, label } = e;
      this.ruleForm.result_info_id = code || "";
      this.ruleForm.result_info_id_name = label || "";
      this.$refs.ruleForm.validateField("result_info_id");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const { bidNo, origin_price, is_gold_price, can, cost_fee } = this.sitem;
          const { bargain_price, cost_price } = this.ruleForm;
          let a = add_sum(origin_price + "", "0");
          let b = add_sum(bargain_price + "", "0");
          if (b > a) {
            this.$message.warning("期望售价不能高于当前售价");
            this.loading = false;
            return;
          }

          let c = add_sum(cost_fee + "", "0");
          let d = add_sum(cost_price + "", "0");
          if (d > c) {
            this.$message.warning("期望工艺费不能高于当前工艺费");
            this.loading = false;
            return;
          }

          let model = JSON.parse(JSON.stringify(this.ruleForm));
          console.log(model);
          delete model["result_info_id_name"];

          //根据实时金价对接口进行不同传参
          if (is_gold_price == "1" && can && can.length > 0 && can[0].id + "" == "6") {
            if (c === d) {
              this.$message.warning("期望工艺费不能等于当前工艺费!");
              this.loading = false;
              return;
            }
            model.bargain_price = "0";
          } else {
            if (a === b) {
              this.$message.warning("期望售价不能等于当前售价!");
              this.loading = false;
              return;
            }
            model.cost_price = "0";
          }

          let res = await asyncRequest.bargain_add(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "议价流程创建成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

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