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

        <el-col :span="10">
          <el-form-item class="query-form-item" label="货位:">
            <el-input v-model="setData.invSpaceName" style="width: 90%" :disabled="true"></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="setData.productName" style="width: 90%" :disabled="true"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="规格型号:">
            <el-input v-model="setData.ggxh" style="width: 90%" :disabled="true"></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="setData.lowStockNum" disabled></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="库存积压预警数量：">
            <el-input v-model="setData.overStockNum" 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="setData.recentDateTime" disabled></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item class="query-form-item" label="近效期预警时间单位：">
            <el-input v-show="setData.isDateBy == 1" value="天" disabled></el-input>
            <el-input v-show="setData.isDateBy == 2" value="小时" 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-select v-model="setData.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="setData.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="setData.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="setData.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="setData.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="formSubmit()" :loading="formLoading"
      >提交
      </el-button>
    </div>
  </div>
</template>

<script>
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {filterProductNames} from "@/api/inventory/invPorduct";
import {isBlank} from "@/utils/strUtil";
import {saveInvRemindSet} from "@/api/system/invRemindSet";

export default {
  props: {
    invRemindSetData: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    },
    formName: {
      type: String,
      required: true
    }
  },
  name: "invRemindSetModify",
  data() {
    return {
      setData: {
        id: null,
        relId: null,
        ggxh: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        lowStock: false,
        lackStock: false,
        overStock: false,
        expireDate: false,
        recentDate: false,
        remark: null
      },
      invList: [],
      spaceList: [],
      productList: [],
      productName: null,
      formLoading: false
    };
  },
  methods: {
    invChange() {
      this.spaceList = [];
      this.setData.invSpaceCode = null;
      this.setData.relId = null;
      this.setData.ggxh = null;
      this.getProductList();
      this.getSpaceList();
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getSpaceList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {invWarehouseCode: this.setData.invCode, status: 1};
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
      })
    },
    invSpaceChange() {
      this.setData.relId = null;
      this.setData.ggxh = null;
      this.getProductList();
    },
    getProductList() {
      this.productList = [];
      let params = {
        cpmctymc: null,
        invCode: this.setData.invCode,
        invSpaceCode: this.setData.invSpaceCode
      };
      filterProductNames(params).then((res) => {
        this.productList = res.data.list || [];
      })
    },
    formSubmit() {
      let result = this.verifyFormData();
      if (result !== "success") {
        this.$message.warning(result);
        return;
      }
      this.formLoading = true;
      saveInvRemindSet(this.setData, this.formName)
        .then((response) => {
          this.formLoading = false;
          if (response.code !== 20000) {
            this.$message.error(response.message);
            return false;
          }
          this.$message.success("操作成功");
          this.closeDialog();
        })
        .catch((error) => {
          this.$message.error(error.message);
          this.formLoading = false;
        });
    },
    verifyFormData() {
      if (isBlank(this.setData.invCode)) {
        return "请选择预警仓库";
      }
      if (this.setData.lowStock === false && this.setData.lackStock === false && this.setData.overStock === false && this.setData.expireDate === false && this.setData.recentDate === false) {
        return "至少开启一项预警";
      }
      return "success";
    },
    handleSelectProduct(relId) {
      this.productList.forEach((item) => {
        if (item.relIdFk === relId) {
          this.setData.ggxh = item.ggxh;
        }
      })
    },
  },
  created() {
    if (!isBlank(this.invRemindSetData.id)) {
      this.setData = this.invRemindSetData;
    }
    this.getInvList();
    this.getProductList();
  },
};
</script>
<style type="text/scss" lang="scss">
.el-radio {
  width: 20%;
}
</style>
