<template>
  <div>
    <el-form :model="formData" label-width="200px">
      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="仓库：">
            <el-input v-model="formData.invName" disabled></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="货位：">
            <el-input v-model="formData.invSpaceName" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="产品名称：">
            <el-input v-model="formData.productName" disabled></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="规格型号：">
            <el-input v-model="formData.ggxh" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider></el-divider>
      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="低库存预警数量：">
            <el-input v-model="formData.lowStockNum" ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="库存积压预警数量：">
            <el-input v-model="formData.overStockNum" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="近效期预警时间：">
            <el-input v-model="formData.recentDateTime" ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="近效期预警时间单位：">
            <el-select v-model="formData.isDateBy">
              <el-option :value=1 label="天" ></el-option>
              <el-option :value=2 label="小时" ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider></el-divider>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="是否开启低库存预警">
            <el-select v-model="formData.lowStock">
              <el-option :value="true" label="开启"></el-option>
              <el-option :value="false" label="关闭"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="是否开启负库存预警">
            <el-select v-model="formData.lackStock">
              <el-option :value="true" label="开启"></el-option>
              <el-option :value="false" label="关闭"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="是否开启库存积压预警">
            <el-select v-model="formData.overStock">
              <el-option :value="true" label="开启"></el-option>
              <el-option :value="false" label="关闭"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="是否开启产品过期预警">
            <el-select v-model="formData.expireDate">
              <el-option :value="true" label="开启"></el-option>
              <el-option :value="false" label="关闭"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="是否开启近效期预警">
            <el-select v-model="formData.recentDate">
              <el-option :value="true" label="开启"></el-option>
              <el-option :value="false" label="关闭"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click.native="closeDialog">取消</el-button>
      <el-button type="primary" @click.native="addInvRemindSet()" :loading="formLoading"
      >提交
      </el-button>
    </div>
  </div>
</template>

<script>
import {getInfoByInvId, saveInvRemindSet} from "@/api/system/invRemindSet";

export default {
  name: "addInvRemindSetDialog",
  props: {
    invId: {
      type: String,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      formData: {
        relId: null,
        ggxh: null,
        deptCode: null,
        invCode: null,
        invSpaceCode: null,
        lowStock: null,
        lackStock: false,
        overStock: false,
        expireDate: false,
        recentDate: false,
        lowStockNum: null,
        overStockNum: null,
        recentDateTime: null,
        isDateBy: null,
        invName: null,
        invSpaceName: null,
        productName: null
      },
      formLoading: false
    };
  },
  methods: {
    getRemindSetInfo() {
      getInfoByInvId({invId: this.invId}).then((res) => {
        if (res.code === 20000) {
          this.formData = res.data;
          if (res.data.lowStock == null) {
            this.formData.lowStock = false;
          }
          if (res.data.lackStock == null) {
            this.formData.lackStock = false;
          }
          if (res.data.overStock == null) {
            this.formData.overStock = false;
          }
          if (res.data.expireDate == null) {
            this.formData.expireDate = false;
          }
          if (res.data.recentDate == null) {
            this.formData.recentDate = false;
          }
        } else {
          this.$message.error(res.meesage);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      })
    },
    addInvRemindSet() {
      if (!this.formData.lowStock && !this.formData.lackStock && !this.formData.overStock && !this.formData.expireDate && !this.formData.recentDate) {
        this.$message.warning("至少开启一项预警！");
        return;
      }
      this.formLoading = true
      saveInvRemindSet(this.formData, "add").then((res) => {
        this.formLoading = false
        if (res.code === 20000) {
          this.closeDialog();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.formLoading = false
        this.$message.error(error.message);
        this.closeDialog();
      })
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getRemindSetInfo();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

