<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="仓库:">
              <el-select v-model="filterQuery.invCode" placeholder="请选择所属仓库" clearable="true"
                         @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="filterQuery.invSpaceCode" placeholder="请选择货位" clearable="true"
                         style="width: 90%"
                         @change="invSpaceChange"
              >
                <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="filterQuery.productName" style="width: 90%" placeholder="请输入产品名称" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="状态:">
              <el-select v-model="filterQuery.status" clearable style="width: 90%">
                <el-option :value="0" label="未启用"></el-option>
                <el-option :value="1" label="已启用"></el-option>
              </el-select>
            </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="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click.native="addSet" icon="el-icon-plus">新增库存预警设置</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 120%" border>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="仓库名称" prop="invName" width="200"></el-table-column>
        <el-table-column label="货位名称" prop="invSpaceName" width="200"></el-table-column>
        <el-table-column label="产品名称" prop="productName" width="300" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="300" show-overflow-tooltip></el-table-column>
        <el-table-column label="低库存预警" prop="lowStock" width="120">
          <template slot-scope="scope">
            <el-tag>{{ enableMap[scope.row.lowStock] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="负库存预警" prop="lackStock" width="120">
          <template slot-scope="scope">
            <el-tag>{{ enableMap[scope.row.lackStock] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="库存积压预警" prop="overStock" width="120">
          <template slot-scope="scope">
            <el-tag>{{ enableMap[scope.row.overStock] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="库存产品过期预警" prop="invSpaceName" width="180">
          <template slot-scope="scope">
            <el-tag>{{ enableMap[scope.row.expireDate] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="近效期预警" prop="recentDate" width="120">
          <template slot-scope="scope">
            <el-tag>{{ enableMap[scope.row.recentDate] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native="handleForm(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click.native="deleteSet(scope.row.id)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      />

      <!--表单-->
      <el-dialog
        v-if="addVisible"
        title="新增预警设置"
        :visible.sync="addVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :before-close="closeAddDialog"
        width="80%"
      >
        <addInvRemindSetDialog
          :closeDialog="closeAddDialog"
        ></addInvRemindSetDialog>
      </el-dialog>

      <!--表单-->
      <el-dialog
        v-if="formVisible"
        title="编辑预警设置"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="60%"
      >
        <invRemindSetModify
          :invRemindSetData="formData"
          :closeDialog="closeDialog"
          :formName="formName"
        ></invRemindSetModify>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  getInvRemindSetList,
  deleteInvRemindSet,
  saveInvRemindSet
} from "@/api/system/invRemindSet";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {filterProductNames} from "@/api/inventory/invPorduct";
import invRemindSetModify from "@/views/remind/invRemindSetModify.vue";
import addInvRemindSetDialog from "@/views/remind/addInvRemindSetDialog.vue";

export default {
  name: "invRemindSet",
  data() {
    return {
      filterQuery: {
        relId: null,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        status: 1,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      loading: true,
      index: null,
      formName: null,
      formMap: {
        add: "新增提醒设置",
        edit: "提醒设置-编辑",
      },
      formLoading: false,
      formVisible: false,
      formData: {
        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
      },
      enableMap: {
        true: "开启",
        false: "关闭"
      },
      deleteLoading: false,
      invList: [],
      spaceList: [],
      queryName: null,
      addVisible: false,
      showSearch: true
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        relId: null,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        status: 1,
        page: 1,
        limit: 20,
      };
      this.getList();
      this.queryName=null;
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getInvRemindSetList(this.filterQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    // 刷新表单
    resetForm() {
      if (this.$refs["dataForm"]) {
        // 清空验证信息表单
        this.$refs["dataForm"].clearValidate();
        // 刷新表单
        this.$refs["dataForm"].resetFields();
        this.getList();
      }
    },
    addSet() {
      this.formName = "add";
      this.addVisible = true;
    },

    deleteSet(id) {
      this.$confirm('是否确认删除此库存预警设置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: id};
        deleteInvRemindSet(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("删除成功");
            this.getList();
          }
        }).catch((error) => {
          this.$message.error(error.message);
        })
      }).catch(() => {
      });
    },
    invChange() {
      this.spaceList = [];
      this.filterQuery.invSpaceCode = null;
      this.getSpaceList();
      this.getList();
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getSpaceList();
          this.getList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {invWarehouseCode: this.filterQuery.invCode, status: 1};
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
      })
    },
    invSpaceChange() {
      let params = {
        cpmctymc: this.filterQuery.cpmctymc,
        invCode: this.filterQuery.invCode,
        invSpaceCode: this.filterQuery.invSpaceCode
      };
      filterProductNames(params).then((res) => {
        this.productList = res.data.list || [];
      })
    },
    queryProductName(productName, cb) {
      let params = {
        cpmctymc: productName,
        invCode: this.filterQuery.invCode,
        invSpaceCode: this.filterQuery.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;
      }
    },
    handleSelect(item) {
      this.queryName = item.value;
      this.filterQuery.relId = item.relId;
    },
    clearQueryProduct() {
      this.queryName = null;
      this.filterQuery.relId = null;
    },
    closeDialog() {
      this.formVisible = false;
      this.formData = {
        id: null,
        relId: null,
        ggxh: null,
        deptCode: null,
        invCode: null,
        invSpaceCode: null,
        lowStock: false,
        lackStock: false,
        overStock: false,
        expireDate: false,
        recentDate: false,
        remark: null
      };
      this.getList();
    },
    closeAddDialog() {
      this.addVisible = false;
      this.getList();
    },
    // 显示表单
    handleForm(index, row) {
      this.formData = row;
      this.formName = "edit";
      this.formVisible = true;
    },
  },
  components: {
    invRemindSetModify,
    addInvRemindSetDialog
  },
  created() {
    this.getInvList();
  },
};
</script>

