<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="物资名称:">
              <el-autocomplete
                class="inline-input"
                style="width: 90%"
                v-model="queryName"
                :fetch-suggestions="queryProductName"
                placeholder="情请输入物资名称"
                clearable
                @select="handleSelect"
                @clear="clearQueryProduct"
              ></el-autocomplete>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item class="query-form-item" label="规格型号:">
              <el-select v-model="filterQuery.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-col :span="8">
            <el-form-item class="query-form-item" label="预警类型:">
              <el-select v-model="filterQuery.type"
                         placeholder="请选择消息状态"
                         clearable
                         style="width: 90%"
              >
                <el-option label="库存不足预警" :value="1"></el-option>
                <el-option label="负库存预警" :value="2"></el-option>
                <el-option label="库存积压预警" :value="3"></el-option>
                <el-option label="库存过期预警" :value="4"></el-option>
                <el-option label="近效期预警" :value="5"></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.batchNo" placeholder="批次号"-->
<!--                        style="width: 90%"-->
<!--                        clearable="true"></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"
                         placeholder="请选择消息状态"
                         clearable
                         style="width: 90%"
              >
                <el-option label="未确认" :value="1"></el-option>
                <el-option label="已确认" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!--          <el-col :span="6">-->
          <!--            <el-form-item class="query-form-item" label="忽略状态:">-->
          <!--              <el-select v-model="filterQuery.ignoreStatus"-->
          <!--                         placeholder="请选择消息忽略状态"-->
          <!--                         clearable-->
          <!--                         style="width: 90%"-->
          <!--              >-->
          <!--                <el-option label="不忽略" :value="0"></el-option>-->
          <!--                <el-option label="忽略7天" :value="1"></el-option>-->
          <!--                <el-option label="忽略15天" :value="2"></el-option>-->
          <!--                <el-option label="忽略30天" :value="3"></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-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="物资名称" prop="productName" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="180" show-overflow-tooltip></el-table-column>
<!--        <el-table-column label="批次号" prop="batchNo" width="120" show-overflow-tooltip></el-table-column>-->
        <el-table-column label="预警次数" prop="batchNo" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="预警类型" prop="productionDate" width="120">
          <template slot-scope="scope">
            <el-tag type="danger">{{ typeMap[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="预警详情" prop="msg" width="250" show-overflow-tooltip></el-table-column>
        <el-table-column label="状态" prop="status" width="120">
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.status)">{{
                statusMap[scope.row.status]
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理结果" prop="handleMsg" width="200" show-overflow-tooltip></el-table-column>
        <!--        <el-table-column label="忽略预警" prop="ignoreStatus" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-tag>{{ ignoreStatusMap[scope.row.ignoreStatus] }}</el-tag>-->
        <!--          </template>-->
        <!--        </el-table-column>-->

<!--        <el-table-column label="入库数量" prop="inCount" width="100"></el-table-column>-->
<!--        <el-table-column label="出库数量" prop="outCount" width="100"></el-table-column>-->
        <el-table-column label="库存数量" prop="reCount" width="100"></el-table-column>
<!--        <el-table-column label="生产日期" prop="productionDate" width="120"></el-table-column>-->
<!--        <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>-->
<!--        <el-table-column label="供应商" prop="supName" width="150" show-overflow-tooltip></el-table-column>-->
        <el-table-column label="生产企业" prop="manufactory" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册备案证号" prop="zczbhhzbapzbh" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              v-if="scope.row.status === 1"
              @click.native="confirmMsg(scope.row)"
            >确认
            </el-button>
            <!--            <el-button-->
            <!--              type="text"-->
            <!--              size="small"-->
            <!--              v-if="scope.row.ignoreStatus === 0"-->
            <!--              @click.native="ignoreMsg(scope.row)"-->
            <!--            >忽略-->
            <!--            </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-card>


    <!--  忽略预警弹窗  -->
    <el-dialog
      v-if="ignoreVisible"
      title="忽略预警"
      :visible.sync="ignoreVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="30%"
      top="5vh"
    >
      <el-row>
        <el-button type="primary" size="medium" style="margin-left: 20%; margin-right: 20px;" @click="ignore(1)">
          忽略7天
        </el-button>
        <el-button type="success" size="medium" style="margin-right: 20px;" @click="ignore(2)">忽略15天</el-button>
        <el-button type="warning" size="medium" style="margin-right: 20px;" @click="ignore(3)">忽略30天</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {getInvRemindMsgList, confirmMsg, ignoreMsg} from "@/api/system/invRemindMsg";
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";

export default {
  name: "invRemindMsg",
  data() {
    return {
      showSearch: true,
      filterQuery: {
        relId: null,
        batchNo: null,
        ggxh: null,
        status: 1,
        ignoreStatus: 0,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        type: null,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      loading: true,
      index: null,
      statusMap: {
        1: "未处理",
        2: "已处理"
      },
      typeMap: {
        1: "库存不足预警",
        2: "负库存预警",
        3: "库存积压预警",
        4: "库存过期预警",
        5: "近效期预警"
      },
      ignoreStatusMap: {
        0: "不忽略",
        1: "忽略7天",
        2: "忽略15天",
        3: "忽略30天"
      },
      queryName: null,
      invList: [],
      spaceList: [],
      ggxhList: [],
      ignoreVisible: false,
      currentId: null
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        paramStatus: 1,
        page: 1,
        limit: 20,
      };
      this.getList();
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getInvRemindMsgList(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;
        });
    },
    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() {
      this.clearProduct();
      let params = {
        cpmctymc: this.filterQuery.cpmctymc,
        invCode: this.filterQuery.invCode,
        invSpaceCode: this.filterQuery.invSpaceCode
      };
      filterProductNames(params).then((res) => {
        this.productList = res.data.list || [];
      })
    },
    clearProduct() {
      this.filterQuery.relId = null;
      this.queryName = null;
      this.filterQuery.ggxh = null;
      this.ggxhList = [];
      this.productList = null;
    },
    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;
      let params = {relId: item.relId};
      getGgxhList(params).then((res) => {
        this.ggxhList = res.data || [];
      });
    },
    clearQueryProduct() {
      this.queryName = null;
      this.filterQuery.relId = null;
    },
    confirmMsg(row) {
      this.$prompt('请输入处理方式', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        let params = {
          id: row.id,
          handleMsg: value
        };
        confirmMsg(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("已确认");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        })
      }).catch(() => {
      });
    },
    ignoreMsg(row) {
      this.ignoreVisible = true;
      this.currentId = row.id;
    },
    ignore(type) {
      this.ignoreVisible = false;
      let params = {
        id: this.currentId,
        ignoreStatus: type
      };
      ignoreMsg(params).then((res) => {
        this.currentId = null;
        if (res.code === 20000) {
          this.$message.success("已忽略预警");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch(() => {
        this.currentId = null;
      })
    },


    statusFilterType(status) {
      const statusMap = {
        1: "warning",
        2: "success",
      };
      return statusMap[status];
    },
  },
  created() {
    this.getInvList();
    // 加载表格数据
    this.getList();
  },
};
</script>

