<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-input v-model="filterQuery.name" 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%"
                         @change="typeChange"
              >
                <el-option label="配送企业资质预警" :value="1"></el-option>
                <el-option label="生产企业资质预警" :value="2"></el-option>
                <el-option label="产品资质预警" :value="3"></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"
                       :disabled="executeEval(null,item.disabledFuc,false)"
                       @change="executeFuc($event,'5',item.checkRules)"
                       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)"
              @change="executeFuc($event,'5',item.checkRules)"
              filterable
              remote
              :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="msgName" prop="name" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="证书名称" prop="certName" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="有效期" prop="vailDate" width="120"></el-table-column>
        <el-table-column label="失效期" prop="expireDate" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="消息状态" prop="status" width="120">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>


        <el-table-column label="预警次数" prop="remindCount" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column label="预警类型" prop="type" width="120">
          <template slot-scope="scope">
            <el-tag>{{ typeMap[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="预警内容" prop="msg" show-overflow-tooltip width="250"></el-table-column>
        <el-table-column label="处理结果" prop="handleMsg" show-overflow-tooltip width="200"></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"
      ></pagination>
    </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>


    <el-dialog
      v-if="confirmVisible"
      title="预警消息处理"
      :visible.sync="confirmVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="65%"
      top="5vh"
    >
      <el-form :model="confirmQuery" label-width="100px" style="margin-bottom: -15px">
        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" :label="msgName">
              <el-input v-model="confirmQuery.name" style="width: 90%" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="证书名称:">
              <el-input v-model="confirmQuery.certName" style="width: 90%" :disabled="true"></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="confirmQuery.remindCount" style="width: 90%" :disabled="true"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="预警类型:">
              <el-select v-model="confirmQuery.type" style="width: 90%" placeholder="预警类型" disabled>
                <el-option label="配送企业资质预警" :value="1"></el-option>
                <el-option label="生产企业资质预警" :value="2"></el-option>
                <el-option label="产品资质预警" :value="3"></el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="预警详情:" prop="msg">
            <el-input v-model="confirmQuery.msg" auto-complete="off" type="textarea" disabled autosize></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="处理方式:" prop="handleMsg">
            <el-input v-model="confirmQuery.handleMsg" auto-complete="off" type="textarea" autosize></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="closeDialog">取消</el-button>
        <el-button type="primary" @click.native="postConfirm()" :loading="confirmLoading"
        >提交
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getSupCertRemindMsgList, confirmMsg, ignoreMsg} from "@/api/purchase/supCertRedmindMsg";
import {
  getHead,executeFuc
} from "@/utils/customConfig";

export default {
  name: "supCertRemindMsg",
  data() {
    return {
      showSearch: true,
      filterQuery: {
        name: null,
        status: "1",
        ignoreStatus: 0,
        type: "1",
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      loading: true,
      index: null,
      statusMap: {
        1: "未确认",
        2: "已确认"
      },
      typeMap: {
        1: "配送企业资质预警",
        2: "生产企业资质预警",
        3: "产品资质预警"
      },
      ignoreStatusMap: {
        0: "不忽略",
        1: "忽略7天",
        2: "忽略15天",
        3: "忽略30天"
      },
      ignoreVisible: false,
      currentId: null,
      msgName: "配送企业名称",
      msgNameMap: {
        1: "配送企业名称",
        2: "生产企业名称",
        3: "物资名称"
      },
      confirmVisible: false,
      confirmQuery: {},
      confirmLoading: false,
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        name: null,
        status: 1,
        ignoreStatus: 0,
        type: 1,
        page: 1,
        limit: 20,
      };
      this.typeChange(this,this.filterQuery.type);
      this.getList();
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getSupCertRemindMsgList(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;
        });
    },


    confirmMsg(_this,row) {
      _this.confirmVisible = true;
      _this.confirmQuery = row;
    },
    postConfirm() {
      this.confirmLoading = true
      confirmMsg(this.confirmQuery).then((res) => {
        this.confirmLoading = false
        if (res.code === 20000) {
          this.confirmVisible = false;
          this.$message.success("已确认");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      })
    },

    closeDialog() {
      this.confirmVisible = false;
    },

    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;
      })
    },
    typeChange(_this,val) {
      _this.msgName = this.msgNameMap[val];
    },
    executeFuc(row,type,clickFuc,value){
      return executeFuc(this,row,type,clickFuc,value);
    },
    executeEval(row,expression,defaultRet){
      if(expression){
        return eval(expression);
      }
      return defaultRet;
    }
  },
  created() {
    // 加载表格数据
    getHead("supCertRemindMsg","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>

