<template>
  <el-dialog
    v-loading="loading"
    :title="'申请议价'"
    :center="true"
    align="left"
    top="10vh"
    width="750px"
    :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="80px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品名称:">
              <el-input v-model="sitem.good_name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="图片:" style="margin: 0" label-width="44px">
              <img
                class="hover"
                v-viewer
                :src="sitem.good_img"
                alt=""
                v-if="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-model="sitem.unit" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="当前售价:">
              <el-input v-model="sitem.sale_price" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item
              label="期望售价:"
              prop="bargain_price"
              label-width="95px"
            >
              <digital-input
                :values="ruleForm.bargain_price"
                :name="'ruleForm.bargain_price'"
                :placeholder="'期望售价'"
                :min="0"
                :disabled="false"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="'元'"
                @reschange="number_change($event, 'bargain_price')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="型号:" style="margin: 0">
              <span v-for="(si, sii) in sitem.specinfo" :key="sii + 'spec'"
                ><span v-if="sii !== 0">--</span>
                <span>{{ si.spec_name }}[{{ si.spec_value_name }}]</span>
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="text-align: right">
            <el-button
              v-if="!isDetail"
              type="primary"
              :size="'mini'"
              @click="submitForm"
              >保 存
            </el-button>
            <el-button @click="showModelThis = false" :size="'mini'">{{
              isDetail ? "关 闭" : "取 消"
            }}</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";
export default {
  name: "Account",
  props: ["showModel", "sitem"],
  mixins: [resToken],
  data() {
    const validate_num = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else if (
        value === "0" ||
        value === "0." ||
        value === "0.0" ||
        value === "0.00"
      ) {
        callback(new Error("期望售价不能为零!"));
      } else {
        callback();
      }
    };

    return {
      roleList: [],
      loading: false,
      title: "添加账号",
      organizeList: [],
      showModelThis: this.showModel,
      ruleForm: {
        bargain_price: "0",
      },
      rulesThis: this.rules,
      rules: {
        bargain_price: [
          {
            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");
      }
    },
  },
  mounted() {},
  methods: {
    itemidChange(e) {
      this.ruleForm.itemid = e;
      this.$refs.ruleForm.validateField("itemid");
    },
    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(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            bargain_price: "0",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { bargain_price } = JSON.parse(JSON.stringify(this.ruleForm));
          const { bidNo, sale_price } = this.sitem;
          if (sale_price * 100 === bargain_price * 100) {
            this.$message.warning("期望售价不能等于当前售价!");
            this.loading = false;
            return;
          }
          const model = {
            bidNo: bidNo,
            bargain_price: bargain_price, // 账号
          };
          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>