<template>
  <div>
    <el-card class="el-card">
      <el-form v-if="queryList && queryList.length > 0 && showSearch" :model="filterQuery" class="query-form" label-width="100px">
       <!-- <el-row>
          <el-col :span="6">
            <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="6">
            <el-form-item class="query-form-item" label="货位:">
              <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择货位" clearable="true"
                         @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-col :span="6">
            <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="6">
            <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-row>

        <el-row>
          <el-col :span="6">
            <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-col :span="6">
            <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.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-row> -->
        <el-row style=" display:flex;  flex-wrap: wrap; ">
          <template v-for="(item, index) in queryList" >
            <el-form-item v-if="item.columnType == 'input' && item.columnName !='queryName' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`" :key="item.id">
              <el-input
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :disabled="executeEval(null,item.disabledFuc,false)"
                @keyup.enter.native="executeFuc($event,'5',item.clickFuc)"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'input' && item.columnName =='queryName' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`" :key="item.id">
              <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-form-item v-if="item.columnType == 'select' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-select v-model="filterQuery[item.columnName]"
                         :placeholder="item.columnDesc"
                         @change="executeFuc($event,'5',item.checkRules)"
                         :disabled="executeEval(null,item.disabledFuc,false)"
                         clearable>
                <el-option
                  v-for="dict in item.lableRuleObj"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
                <span style="float: left">{{ item.name }}</span>
                <span v-if="item.isShowXx == '1'" style="float: right; color: #8492a6; font-size: 13px">{{ item.code }}</span>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'selectServer' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-select
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :disabled="executeEval(null,item.disabledFuc,false)"
                filterable
                remote
                @change="executeFuc($event,'5',item.checkRules)"
                :remote-method="(query) => executeFuc(query,'5',item.clickFuc)"
                clearable>
                <el-option
                  v-for="item in options[item.clickFuc]"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
                <span style="float: left">{{ item.name }}</span>
                <span v-if="item.isShowXx == '1'" style="float: right; color: #8492a6; font-size: 13px">{{ item.code }}</span>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'datePicker' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'date' && executeEval(row,item.expression,true)" :label="item.columnDesc+`:`">
              <el-date-picker
                v-model="filterQuery[item.columnName]"
                :style="`width:${item.width+'px'}`"
                value-format="yyyy-MM-dd"
                :disabled="executeEval(null,item.disabledFuc,false)"
                type="date"
                :placeholder="item.columnDesc"
              ></el-date-picker>
            </el-form-item>
          </template>
        </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="invName" width="120"></el-table-column>
        <el-table-column label="货位名称" prop="invSpaceName" width="120"></el-table-column>
        <el-table-column label="物资名称" prop="productName" 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="ggxh" width="180" 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="type" 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="handleMsg" width="200" show-overflow-tooltip></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>
          </template>
        </el-table-column> -->
        <template v-for="(item, index) in tableHeader">
          <el-table-column
            v-if="item.columnType == 'id' && executeEval(row,item.expression,true)"
            type="index" :label="item.columnDesc"></el-table-column>
          <el-table-column
            v-if="item.columnType == 'selection'"
            type="selection"
            :width="item.width"
            :selectable="(row,number) => executeFuc(row,'3',item.clickFuc)"
          ></el-table-column>

          <el-table-column
            v-if="item.columnType == 'radio' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template  slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="radioCheck"><span></span></el-radio>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'laber' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template   slot-scope="scope">
              <span :style="{color: executeFuc(scope.row,'4',item.lableRuleObj[scope.row[item.columnName]])}">{{ item.lableRuleObj[scope.row[item.columnName]] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'eltag' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template   slot-scope="scope">
              <el-tag :type="executeFuc(scope.row,'4',item,item.lableRuleObj?item.lableRuleObj[scope.row[item.columnName]]:scope.row[item.columnName])">
                <span>{{ item.lableRuleObj?item.lableRuleObj[scope.row[item.columnName]]:scope.row[item.columnName] }}</span>
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'button' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :width="item.width"
            :key="item.columnName"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button v-for="(buttonItem, buttonIndex) in item.buttonRulObj"
                         :type="buttonItem.type"
                         :size="buttonItem.size"
                         :style="buttonItem.style"
                         :key="buttonItem"
                         v-if="executeEval(scope.row,buttonItem.hasPermi,true)"
                         :disabled="executeEval(scope.row,buttonItem.disabledFuc,false)"
                         @click.native.stop="executeFuc(scope.row,'1',buttonItem.clickFuc)"
              >{{ buttonItem.name }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'text' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template slot-scope="scope">
              <span :style="{color: executeFuc(scope.row,'4',item,scope.row[item.columnName])}">{{ scope.row[item.columnName] }}</span>
            </template>
          </el-table-column>
        </template>
      </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="confirmMsgVisible"
      title="确认预警信息"
      :visible.sync="confirmMsgVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="60%"
      top="5vh"
    >
      <el-form :model="rowData" label-width="200px" style="margin-bottom: -15px">
        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="仓库：">
              <el-input v-model="rowData.invName" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="货位：">
              <el-input v-model="rowData.invSpaceName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="产品名称：">
              <el-input v-model="rowData.productName" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="规格型号：">
              <el-input v-model="rowData.ggxh" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-eow>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="预警类型：">
              <el-input v-model="rowData.msg" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="预警类型：">
              <el-input v-model="rowData.typeName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-eow>

        <el-row >
          <el-col :span="20">
            <el-form-item class="query-form-item" label="备注:">
              <el-input v-model="handleMsg" auto-complete="off"
                        type="textarea"
                        placeholder="请输入备注内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="closeConfirmDialog">取消</el-button>
        <el-button type="primary" @click.native="updateHandleMsg()" :loading="updateLoading"
        >提交
        </el-button>
      </div>

    </el-dialog>

    <!--  忽略预警弹窗  -->
    <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 {getInvListByUserOptimize} from "@/api/system/invWarehouse";
import {getInvSpaceListOptimize} from "@/api/inventory/invSpace";
import {filterProductNames} from "@/api/inventory/invPorduct";
import {getGgxhListOptimize} from "@/api/basic/product/udiRelevance";
import {
  getHead,executeFuc
} from "@/utils/customConfig";

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,
      tableHeader:[],
      queryList:[],
      fromList:[],

      options: {
        getInvList:[],
        getSpaceList:[],
        getGgxhList:[],
      },
      invList: [],
      invCodeList:[],
      spaceList: [],
      ggxhList: [],
      ignoreVisible: false,
      currentId: null,
      confirmMsgVisible: false,
      handleMsg: null,
      rowData: null,
      updateLoading: false,

    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        paramStatus: 1,
        page: 1,
        limit: 20,
        listInv:this.invCodeList,
      };
      this.queryName = null;
      this.getList();
      this.options.getInvSpaceList = [];
      this.options.getGgxhList = [];
    },
    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) {
      _this.spaceList = [];
      _this.filterQuery.invSpaceCode = null;
      _this.getSpaceList(_this);
      _this.getList();
    },
    getInvList(_this) {
      getInvListByUserOptimize()
        .then((response) => {
          _this.options.getInvList = response.data || [];
          for(var i=0;i<response.data.length;i++){
            _this.invCodeList.push(response.data[i].code)
          }
          _this.getSpaceList(_this);
          _this.getList();
        })
        .catch(() => {
        });

    },
    getSpaceList(_this) {
      let params = {invWarehouseCode: _this.filterQuery.invCode, status: 1};
      getInvSpaceListOptimize(params).then((res) => {
        _this.options.getSpaceList = 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.options.getGgxhList = [];
      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;
      this.getGgxhList(this,item.relId);
    },
    getGgxhList(_this,relId) {
      let params = {relId: relId};
      getGgxhListOptimize(params).then((res) => {
       _this.options.getGgxhList = res.data || [];
      });
    },

    clearQueryProduct() {
      this.queryName = null;
      this.filterQuery.relId = null;
    },
    confirmMsg(_this,row) {
      _this.rowData = row;
      let typeName = _this.typeMap[row.type];
      _this.rowData.typeName = typeName;
      _this.handleMsg = null;
      _this.confirmMsgVisible = true;
    },
    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;
      })
    },
    closeConfirmDialog() {
      this.confirmMsgVisible = false;
      this.rowData = {}
    },
    updateHandleMsg() {
      let params = {
        id: this.rowData.id,
        handleMsg: this.handleMsg
      };
      this.updateLoading = true
      confirmMsg(params).then((res) => {
        this.updateLoading = false
        if (res.code === 20000) {
          this.rowData = {};
          this.handleMsg = null;
          this.confirmMsgVisible = false;
          this.$message.success("已确认");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.updateLoading = false
        this.rowData = {};
        this.handleMsg = null;
        this.confirmMsgVisible = false;
        this.$message.error(error.message);
      })
    },

    statusFilterType(status) {
      const statusMap = {
        1: "warning",
        2: "success",
      };
      return statusMap[status];
    },
    executeFuc(row,type,clickFuc,value){
      return executeFuc(this,row,type,clickFuc,value);
    },
    executeEval(row,expression,defaultRet){
      if(expression){
        return eval(expression);
      }
      return defaultRet;
    }
  },
  created() {
    this.getInvList(this);
    // 加载表格数据
    getHead("invRemindMsg","1").then((re) => {
      // 处理返回的数据
      this.tableObj = re.data;
      this.tableHeader = re.data.tableList;
      this.queryList = re.data.queryList;
      this.fromList = re.data.fromList;
      this.getList();
    });
  },
};
</script>

