<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="600px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            ref="ruleForm"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item label="业务公司名称" prop="companyName">
              <el-input
                v-model="ruleForm.companyName"
                disabled
                maxlength="1000"
                placeholder="请选择业务公司"
              />
            </el-form-item>
            <el-form-item label="供应商名称" prop="wsm_supplier">
              <el-input
                v-model="ruleForm.wsm_supplier"
                disabled
                maxlength="1000"
                placeholder="请选择供应商"
              />
            </el-form-item>
            <el-form-item label="仓库名称" prop="wsm_name">
              <el-input
                v-model="ruleForm.wsm_name"
                disabled
                maxlength="1000"
                placeholder="请选择备库仓库"
              />
            </el-form-item>
            <el-form-item label="商品名称" prop="good_name">
              <el-input v-model="ruleForm.good_name" maxlength="1000" disabled></el-input>
            </el-form-item>
            <el-form-item label="预警数量" prop="warn_stock">
              <el-input v-model="ruleForm.warn_stock" maxlength="1000"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button type="primary" @click="submitForm" v-if="!isDetail"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/stock/stockWarning";
import resToken from "@/mixins/resToken";
import { isnumber } from "@/utils/validate";
export default {
  name: "stockWarning",
  props: ["showModel", "row", "isDetail", "sitem"],
  mixins: [resToken],
  data() {
    const validateWarn_stock = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("预计数量不能为空！"));
      } else {
        if (isnumber(value)) {
          callback();
        } else {
          callback(new Error("预计数量仅支持数字！"));
        }
      }
    };
    return {
      title: "库存预警修改",
      companyCode: "",
      loading: false,
      showModelThis: this.showModel,
      ruleForm: {
        type_code: "",
        wsm_code: "",
        wsm_name: "",
        good_name: "",
        warn_stock: "",
        companyName: "",
      },
      rulesThis: this.rules,
      rules: {
        warn_stock: [
          {
            required: true,
            validator: validateWarn_stock,
            trigger: "blur",
          },
        ],
        companyName: [
          {
            message: "业务公司名称不能为空",
            required: true,
            trigger: "blur",
          },
        ],
        good_name: [
          {
            message: "产品名称不能为空",
            required: true,
            trigger: "blur",
          },
        ],
        wsm_supplier: [
          {
            required: true,
            message: "请选择供应商",
            trigger: "blue",
          },
        ],
        wsm_name: [
          {
            required: true,
            message: "请选择仓库",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
    sitem(val) {
      const {
        good_name,
        warn_stock,
        wsm_name,
        wsm_supplier,
        wsm_code,
        spuCode,
        companyName,
      } = val;
      this.ruleForm = {
        wsm_supplier,
        wsm_name,
        good_name,
        warn_stock,
        wsm_code,
        companyName,
        type_code: spuCode,
      };
      console.log(this.ruleForm);
    },
  },
  methods: {
    // 关闭弹窗

    async initForm() {
      this.loading = true;
      this.title = "库存预警修改";
      this.rulesThis = this.rules;
      this.loading = false;
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const { warn_stock, type_code, wsm_code } = this.ruleForm;
          const model = {
            warn_stock,
            type_code,
            wsm_code,
          };
          const { code, data, message } = await asyncRequest.update(model);
          this.loading = false;
          if (code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            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>
