<template>
  <div>
    <el-form :model="formData" label-width="200px">
      <el-card>
        <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-card>

      <el-divider></el-divider>

      <el-form v-model="productQuery" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="仓库名称:">
              <el-select v-model="productQuery.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="8">
            <el-form-item class="query-form-item" label="货位名称:">
              <el-select v-model="productQuery.invSpaceCode" placeholder="请选择货位"
                         @change="invSpaceChange"
                         clearable
                         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-col :span="8">
            <el-form-item class="query-form-item" label="物资名称:">
              <el-input v-model="productQuery.cpmctymc" clearable style="width: 90%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="产品DI:">
              <el-input v-model="productQuery.nameCode" clearable style="width: 90%"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item class="query-form-item" label="供应商名称:">
              <el-input v-model="productQuery.supName" clearable style="width: 90%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" @click.native="queryProductList" icon="el-icon-search">搜索</el-button>
          <el-button type="primary" @click.native="formSubmit" icon="el-icon-check">选入</el-button>
        </el-button-group>
      </div>

      <el-table v-loading="loading" :data="productList" style="width: 100%" border ref="multipleTable"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="物资名称" prop="cpmctymc" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="仓库名称" prop="invName" width="200"></el-table-column>
        <el-table-column label="供应商名称" prop="supName" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="生产企业" prop="ylqxzcrbarmc" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册备案号" prop="zczbhhzbapzbh" show-overflow-tooltip></el-table-column>
      </el-table>

      <pagination
        v-show="productTotal>0"
        :total="productTotal"
        :page.sync="productQuery.page"
        :limit.sync="productQuery.limit"
        @pagination="getInvProductList"
      />
    </el-form>

  </div>
</template>

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

export default {
  props: {
    closeDialog: {
      type: Function,
      required: true
    }
  },
  name: "addInvRemindSetDialog",
  data() {
    return {
      formData: {
        deptCode: this.$store.getters.locDeptCode,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        lowStock: false,
        lackStock: false,
        overStock: false,
        expireDate: false,
        recentDate: false
      },
      invList: [],
      spaceList: [],
      productList: [],
      formLoading: false,
      productVisible: false,
      loading: false,
      productQuery: {
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        cpmctymc: null,
        nameCode: null,
        page: 1,
        limit: 10
      },
      productTotal: 0,
      multipleSelection: [],
      showSearch: true
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    invChange() {
      this.multipleSelection = [];
      this.spaceList = [];
      this.formData.invSpaceCode = null;
      this.formData.relId = null;
      this.formData.ggxh = null;
      this.getInvProductList();
      this.getSpaceList();
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getSpaceList();
          this.getInvProductList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {invWarehouseCode: this.formData.invCode, status: 1};
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
      })
    },
    invSpaceChange() {
      this.multipleSelection = [];
    },
    formSubmit() {
      let message = this.verifyFormData();
      if (message !== "success") {
        this.$message.error(message);
        return;
      }


      let relIdList = [];
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item) => {
          relIdList.push(item.relIdFk);
        });
      } else {
        this.$message.error("请选择需要预警的产品");
        return;
      }

      //添加预警设置
      let params = {
        lowStock: this.formData.lowStock,
        lackStock: this.formData.lackStock,
        overStock: this.formData.overStock,
        expireDate: this.formData.expireDate,
        recentDate: this.formData.recentDate,
        deptCode: this.formData.deptCode,
        invCode: this.formData.invCode,
        invSpaceCode: this.formData.invSpaceCode,
        relIdList: relIdList
      };
      batchAddInvRemindSet(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("添加成功");
          this.closeDialog();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
        this.closeDialog();
      })
    },
    verifyFormData() {
      if (isBlank(this.formData.invCode)) {
        return "请选择预警仓库";
      }
      if (this.formData.lowStock === false && this.formData.lackStock === false && this.formData.overStock === false && this.formData.expireDate === false && this.formData.recentDate === false) {
        return "至少开启一项预警";
      }
      return "success";
    },
    selectProduct() {
      this.productVisible = true;
      this.getInvProductList();
      this.productQuery = {
        invCode: this.formData.invCode,
        invSpaceCode: this.formData.invSpaceCode,
        page: 1,
        limit: 10
      };
      this.productTotal = 0;
    },
    getInvProductList() {
      getInvProduct(this.productQuery).then((res) => {
        if (res.code === 20000) {
          this.productList = res.data.list || [];
          this.productTotal = res.data.total || 0;
        } else {
          this.productList = [];
          this.productTotal = 0;
        }
      }).catch((error) => {
        this.$message.error(error.message);
        this.productList = [];
        this.productTotal = 0;
      })
    },
    onReset() {
      this.productQuery = {
        invCode: this.formData.invCode,
        invSpaceCode: this.formData.invSpaceCode,
        cpmctymc: null,
        nameCode: null,
        page: 1,
        limit: 10
      };
      this.productTotal = 0;
      this.productList = [];
      this.getInvProductList()
    },
    queryProductList() {
      this.getInvProductList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
  created() {
    this.getInvList();
  },
};
</script>
<style type="text/scss" lang="scss">
.el-radio {
  width: 20%;
}
</style>
