<template>
  <div>
    <el-form :model="setData" label-width="200px" style="margin-bottom: -15px">
      <el-row>
        <el-col :span="10">
          <el-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-col :span="10">
            <el-form-item class="query-form-item" label="产品名称:">
              <el-autocomplete
                class="inline-input"
                style="width: 90%"
                v-model="productName"
                :fetch-suggestions="queryProductName"
                placeholder="请输入内容"

                @select="handleSelectProduct"
                @clear="clearQueryProduct"
              ></el-autocomplete>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="规格型号:">
              <el-select v-model="setData.ggxh" placeholder="请选择" style="width: 90%">
                <el-option
                  v-for="item in ggxhList"
                  :key="item"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </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: [],
      productName: null,
      ggxhList: [],
      formLoading: false
    };
  },
  methods: {
    invChange() {
      this.spaceList = [];
      this.setData.invSpaceCode = null;
      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() {
      let params = {
        cpmctymc: null,
        invCode: this.setData.invCode,
        invSpaceCode: this.setData.invSpaceCode
      };
      filterProductNames(params).then((res) => {
        this.productList = res.data.list || [];
      })
    },
    queryProductName(productName, cb) {
      let params = {
        cpmctymc: productName,
        invCode: this.setData.invCode,
        invSpaceCode: this.setData.invSpaceCode
      };
      filterProductNames(params).then((res) => {
        let productList = res.data.list || [];
        let results = productName ? productList.filter(this.createFilter(productName)) : productList;
        let rs = [];
        results.forEach(item => {
          let data = {value: item.cpmctymc, relId: item.relIdFk}
          rs.push(data);
        });
        cb(rs);
      })
    },
    createFilter(queryString) {
      return (productList) => {
        return productList.cpmctymc.toLowerCase().indexOf(queryString.toLowerCase()) === 0;
      }
    },
    handleSelectProduct(item) {
      this.queryName = item.value;
      this.setData.relId = item.relId;
      this.setData.ggxh = null;
      let params = {relId: item.relId};
      getGgxhList(params).then((res) => {
        this.ggxhList = res.data || [];
      });
    },
    clearQueryProduct() {
      this.queryName = null;
      this.setData.relId = null;
    },

    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 (!isBlank(this.setData.relId)) {
        if (isBlank(this.setData.ggxh)) {
          return "请选择规格型号";
        }
      }

      if (this.setData.lowStock === false && this.setData.lackStock === false && this.setData.overStock === false && this.setData.expireDate === false && this.setData.recentDate === false) {
        return "至少开启一项预警";
      }

    },
  },
  created() {
    if (!isBlank(this.invRemindSetData.id)) {
      this.setData = this.invRemindSetData;
    }
    this.getInvList();
  },
};
</script>
<style type="text/scss" lang="scss">
.el-radio {
  width: 10%;
}
</style>
