<template>
  <div>
    <el-form :model="setData" label-width="200px" style="margin-bottom: -15px">
      <el-row>
        <el-col :span="10">
          <el-form-item class="query-form-item" label="仓库:">
            <el-select v-model="setData.invCode" placeholder="请选择所属仓库"
                       @change="invChange"
                       style="width: 90%"
            >
              <el-option
                v-for="item in invList"
                :key="item.name"
                :label="item.name"
                :value="item.code">
                <span style="float: left">{{ item.name }}</span>
              </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.invSpaceCode" placeholder="请选择货位"
                       @change="invSpaceChange"
                       style="width: 90%"
            >
              <el-option
                v-for="item in spaceList"
                :key="item.name"
                :label="item.name"
                :value="item.code">
                <span style="float: left">{{ item.name }}</span>
              </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.relId" placeholder="请选择货位"
                       style="width: 90%"
                       @change="handleSelectProduct"
            >
              <el-option
                v-for="item in productList"
                :key="item.cpmctymc"
                :label="item.cpmctymc"
                :value="item.relIdFk"
              >
                <span style="float: left">{{ item.cpmctymc }}</span>
                <span style="float: right">{{ item.ggxh }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

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

      <el-row>
        <el-col :span="20">
          <el-form-item class="query-form-item" label="是否开启低库存预警">
            <el-radio v-model="setData.lowStock" :label="true" border>开启</el-radio>
            <el-radio v-model="setData.lowStock" :label="false" border>关闭</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item class="query-form-item" label="是否开启负库存预警">
            <el-radio v-model="setData.lackStock" :label="true" border>开启</el-radio>
            <el-radio v-model="setData.lackStock" :label="false" border>关闭</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item class="query-form-item" label="是否开启库存积压预警">
            <el-radio v-model="setData.overStock" :label="true" border>开启</el-radio>
            <el-radio v-model="setData.overStock" :label="false" border>关闭</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item class="query-form-item" label="是否开启产品过期预警">
            <el-radio v-model="setData.expireDate" :label="true" border>开启</el-radio>
            <el-radio v-model="setData.expireDate" :label="false" border>关闭</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item class="query-form-item" label="是否开启近效期预警">
            <el-radio v-model="setData.recentDate" :label="true" border>开启</el-radio>
            <el-radio v-model="setData.recentDate" :label="false" border>关闭</el-radio>
          </el-form-item>
        </el-col>
      </el-row>

      </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 {getGgxhList} from "@/api/basic/product/udiRelevance";
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: 10%;
}
</style>
