<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="1040px"
    :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
        :rules="rulesThis"
        label-width="90px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="24">
            <show-data-table
              style="margin: 0 0 10px 0; padding: 0"
              :sitem="sitem"
              border
              :columns="showColumns"
            />
          </el-col>
          <el-col :span="6">
            <el-form-item label="当前库存" prop="activity_stock">
              <el-input
                v-model="ruleForm.activity_stock"
                maxlength="100"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="新增库存" prop="act_stock">
              <digital-input
                :values="ruleForm.act_stock"
                :placeholder="'新增库存'"
                :min="0"
                :max="100000000000"
                :position="'right'"
                :precision="0"
                :controls="false"
                :append="''"
                @reschange="num_change"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="提交后库存" prop="total_stock">
              <el-input
                v-model="ruleForm.total_stock"
                :placeholder="'提交后库存'"
                maxlength="11"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" class="tr">
            <el-button v-if="!isDetail" type="primary" @click="submitForm" :size="'mini'"
              >保 存
            </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/goodStore/active";
import resToken from "@/mixins/resToken";
import { isnumber } from "@/utils/validate";
import { add_sum } from "@/utils/validate";
import { showColumns } from "../columns";
export default {
  name: "Account",
  props: ["showModel", "id", "sitem"],
  mixins: [resToken],
  data() {
    const validateNum = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("新增库存不能为空！"));
      } else {
        const num = parseInt(value + "");
        if (!isnumber(value)) {
          callback(new Error("新增库存必须是整数！"));
        } else if (num === NaN) {
          callback(new Error("新增库存必须是整数！"));
        } else if (num === 0) {
          callback(new Error("新增库存不能为零！"));
        } else {
          callback();
        }
      }
    };

    return {
      loading: false,
      title: "添加商品活动库存",
      showModelThis: this.showModel,
      ruleForm: {
        activity_stock: "",
        skuCode: "", // 账号
        actCode: "", // 真实姓名
        act_stock: "",
        total_stock: "",
      },
      showColumns,
      rulesThis: this.rules,
      rules: {
        act_stock: [
          {
            required: true,
            validator: validateNum,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {},
  methods: {
    async initForm() {
      this.loading = true;
      await this.resetForm();
      this.rulesThis = this.rules;
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          for (let key in this.ruleForm) {
            this.ruleForm[key] = key === "act_stock" ? "0" : this.sitem[key] ?? "";
          }
          this.get_total_stock();
        }
      });
    },
    get_total_stock() {
      const { activity_stock, act_stock } = this.ruleForm;
      this.ruleForm.total_stock = add_sum(activity_stock, act_stock);
    },
    num_change(e) {
      this.ruleForm.act_stock = e + "";
      this.$refs.ruleForm.validateField("act_stock");
      this.get_total_stock();
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            const model = JSON.parse(JSON.stringify(this.ruleForm));
            const { code, data, message } = await asyncRequest.actstock(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 {
              this.$message.warning(message);
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.account {
  .bottom-btn {
    position: absolute;
    bottom: 0px;
    right: 0;
    text-align: right;
    z-index: 2;
  }
}
</style>
