<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="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.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-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-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="300"></el-table-column>
        <el-table-column label="证书名称" prop="certName" width="300"></el-table-column>
        <el-table-column label="有效期" prop="vailDate" width="200"></el-table-column>
        <el-table-column label="失效期" prop="expireDate" width="300" show-overflow-tooltip></el-table-column>
        <el-table-column label="消息状态" prop="status" width="100">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </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="type" width="150">
          <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></el-table-column>
        <el-table-column label="处理方式" prop="handleMsg" 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 {getSupCertRemindMsgList, confirmMsg, ignoreMsg} from "@/api/purchase/supCertRedmindMsg";

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: "产品名称"
      }
    };
  },
  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.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(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;
      })
    },
    typeChange(val) {
      this.msgName = this.msgNameMap[val];
    }
  },
  created() {
    // 加载表格数据
    this.getList();
  },
};
</script>

