<template>
  <div>
    <el-card class="el-card">
      <el-tabs type="border-card">
        <el-tab-pane label="产品资质预警">
          <el-form :model="productQuery" 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="productQuery.name" placeholder="配送企业名称"-->
              <!--                            style="width: 90%"-->
              <!--                            clearable="true"></el-input>-->
              <!--                </el-form-item>-->
              <!--              </el-col>-->

              <el-col :span="4">
                <el-form-item class="query-form-item" label="物资名称:">
                  <el-input v-model="productQuery.name" placeholder="物资名称"
                            style="width: 90%"
                            clearable="true"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item class="query-form-item" label="证书名称:">
                  <el-input v-model="productQuery.certName" placeholder="证书名称"
                            style="width: 90%"
                            clearable="true"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item class="query-form-item" label="状态:">
                  <el-select v-model="productQuery.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="4">
                <el-form-item class="query-form-item" label="供应商:">
                  <el-input v-model="productQuery.supName" placeholder="供应商"
                            style="width: 90%"
                            clearable="true"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item class="query-form-item" label="预警类型:">
                  <el-select v-model="productQuery.warnType"
                             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-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" @click.native="productSearch" icon="el-icon-search">搜索</el-button>
            </el-button-group>
          </div>
          <el-divider style="margin: 15px"></el-divider>
          <el-table v-loading="loading" :data="productList" style="width: 100%" border highlight-current-row>
            <el-table-column type="index" label="序号" width="50"></el-table-column>
            <el-table-column label="供应商" prop="supName" width="180" show-overflow-tooltip></el-table-column>
            <el-table-column label="物资名称" 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="warnType" width="120">
              <template slot-scope="scope">
                <el-tag :type="statusFilterType(scope.row.warnType)">{{ warnTypeMap[scope.row.warnType] }}</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>-->


                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.status === 1"
                  @click.native="linkPage(scope.row,3)"
                >处理
                </el-button>
                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.status === 1"
                  @click.native="delMsg(scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="productTotal>0"
            :total="productTotal"
            :page.sync="productQuery.page"
            :limit.sync="productQuery.limit"
            @pagination="getProductList"
          />
        </el-tab-pane>
        <el-tab-pane label="生产企业资质预警">
          <el-form :model="manuQuery" class="query-form" label-width="100px"
                   v-show="showSearch">
            <el-row>
              <el-col :span="4">
                <el-form-item class="query-form-item" label="生产企业:">
                  <el-input v-model="manuQuery.name" placeholder="生产企业"
                            style="width: 90%"
                            clearable="true"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item class="query-form-item" label="证书名称:">
                  <el-input v-model="manuQuery.certName" placeholder="证书名称"
                            style="width: 90%"
                            clearable="true"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item class="query-form-item" label="状态:">
                  <el-select v-model="manuQuery.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="4">
                <el-form-item class="query-form-item" label="配送企业:">
                  <el-input v-model="manuQuery.supName" placeholder="配送企业"
                            style="width: 90%"
                            clearable="true"></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item class="query-form-item" label="预警类型:">
                  <el-select v-model="manuQuery.warnType"
                             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-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" @click.native="manuSearch" icon="el-icon-search">搜索</el-button>
            </el-button-group>
          </div>
          <el-divider style="margin: 15px"></el-divider>

          <el-table v-loading="loading" :data="manuList" style="width: 100%" border highlight-current-row>
            <el-table-column type="index" label="序号" width="50"></el-table-column>
            <el-table-column label="配送企业" prop="supName" width="180" show-overflow-tooltip></el-table-column>
            <el-table-column label="生产企业" 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="warnType" width="120">
              <template slot-scope="scope">
                <el-tag :type="statusFilterType(scope.row.warnType)">{{ warnTypeMap[scope.row.warnType] }}</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>-->

                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.status === 1"
                  @click.native="linkPage(scope.row,2)"
                >处理
                </el-button>

                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.status === 1"
                  @click.native="delMsg(scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="manuTotal>0"
            :total="manuTotal"
            :page.sync="manuQuery.page"
            :limit.sync="manuQuery.limit"
            @pagination="getManuList"
          />

        </el-tab-pane>
        <el-tab-pane label="配送企业资质预警">

          <el-form :model="supQuery" class="query-form" label-width="100px"
                   v-show="showSearch">
            <el-row>
<!--              <el-col :span="6" v-if="this.ckeck">-->
<!--                <el-form-item class="query-form-item" label="配送企业名称:">-->
<!--                  <el-input v-model="supQuery.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-input v-model="supQuery.certName" 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="supQuery.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-input v-model="supQuery.supName" 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="supQuery.warnType"
                             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-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" @click.native="supSearch" icon="el-icon-search">搜索</el-button>
            </el-button-group>
          </div>
          <el-divider style="margin: 15px"></el-divider>

          <el-table v-loading="loading" :data="supList" style="width: 100%" border highlight-current-row>
            <el-table-column type="index" label="序号" width="50"></el-table-column>
            <el-table-column label="配送企业" 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="warnType" width="120">
              <template slot-scope="scope">
                <el-tag :type="statusFilterType(scope.row.warnType)">{{ warnTypeMap[scope.row.warnType] }}</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>-->

                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.status === 1"
                  @click.native="linkPage(scope.row,1)"
                >处理
                </el-button>
                <el-button
                  type="text"
                  size="small"
                  v-if="scope.row.status === 1"
                  @click.native="delMsg(scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="supTotal>0"
            :total="supTotal"
            :page.sync="supQuery.page"
            :limit.sync="supQuery.limit"
            @pagination="getSupList"
          />
        </el-tab-pane>

      </el-tabs>


    </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()"
        >提交
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

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

export default {
  name: "supCertRemindMsg",
  data() {
    return {
      showSearch: true,

      productQuery: {
        name: null,
        status: 1,
        warnType: null,
        supName: null,
        ignoreStatus: 0,
        type: 3,
        page: 1,
        limit: 20,
        certName: null
      },
      productList: [],
      productTotal: 0,

      manuQuery: {
        name: null,
        status: 1,
        ignoreStatus: 0,
        warnType: null,
        supName: null,
        type: 2,
        page: 1,
        limit: 20,
        certName: null
      },
      manuList: [],
      manuTotal: 0,


      supQuery: {
        name: null,
        warnType: null,
        supName: null,
        status: 1,
        ignoreStatus: 0,
        type: 1,
        page: 1,
        limit: 20,
        certName: null
      },
      supList: [],
      supTotal: 0,


      loading: true,
      index: null,
      ckeck: null,
      statusMap: {
        1: "未确认",
        2: "已确认"
      },
      typeMap: {
        1: "配送企业资质预警",
        2: "生产企业资质预警",
        3: "产品资质预警"
      },
      warnTypeMap: {
        1: "过期预警",
        2: "近效期预警",
      },
      ignoreStatusMap: {
        0: "不忽略",
        1: "忽略7天",
        2: "忽略15天",
        3: "忽略30天"
      },
      ignoreVisible: false,
      currentId: null,
      msgName: "配送企业名称",
      msgNameMap: {
        1: "配送企业名称",
        2: "生产企业名称",
        3: "物资名称"
      },
      confirmVisible: false,
      confirmQuery: {},
    };
  },
  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();
    },
    productSearch() {
      this.productQuery.page = 1;
      this.getProductList();
    },
    manuSearch() {
      this.manuQuery.page = 1;
      this.getManuList();
    },
    supSearch() {
      this.supQuery.page = 1;
      this.getSupList();
    },

    getProductList() {
      this.loading = true;
      getSupCertRemindMsgList(this.productQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.productList = response.data.list || [];
            this.productTotal = response.data.total || 0;
          } else {
            this.productList = [];
            this.productTotal = 0;
          }
        })
        .catch(() => {
          this.loading = false;
          this.productList = [];
          this.productTotal = 0;
        });
    },
    getManuList() {
      this.loading = true;
      getSupCertRemindMsgList(this.manuQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.manuList = response.data.list || [];
            this.manuTotal = response.data.total || 0;
          } else {
            this.manuList = [];
            this.manuTotal = 0;
          }
        })
        .catch(() => {
          this.loading = false;
          this.manuList = [];
          this.manuTotal = 0;
        });
    },
    getSupList() {
      this.loading = true;
      getSupCertRemindMsgList(this.supQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.supList = response.data.list || [];
            this.supTotal = response.data.total || 0;
          } else {
            this.supList = [];
            this.supTotal = 0;
          }
        })
        .catch(() => {
          this.loading = false;
          this.supList = [];
          this.supTotal = 0;
        });
    },


    // confirmMsg(row) {
    //   this.confirmVisible = true;
    //   this.confirmQuery = row;
    // },

    confirmMsg(row) {
      this.confirmVisible = true;
      this.confirmQuery = row;
    },


    linkPage(row, type) {
      if (type == 1) {
        this.$router.push("/purchase/purchase/cert/supCertAudit?unitIdFk=" + row.idFk);
      } else if (type == 2) {
        this.$router.push("/purchase/purchase/cert/supCertAudit?manuIdFk=" + row.idFk + "&unitIdFk=" + row.supId);
      } else if (type == 3) {
        this.$router.push("/purchase/purchase/cert/supCertAudit?productIdFk=" + row.idFk + "&unitIdFk=" + row.supId + "&manuIdFk=" + row.manufacturerIdFk);
      }

    },

    statusFilterType(status) {
      const statusMap = {
        0: "warning",
        1: "danger",
        2: "warning",
        3: "success",
      };
      return statusMap[status];
    },
    postConfirm() {
      confirmMsg(this.confirmQuery).then((res) => {
        if (res.code === 20000) {
          this.confirmVisible = false;
          this.$message.success("已确认");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      })
    },

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

    delMsg(row) {

      this.$confirm("是否确定删除该预警信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let params = {
          id: row.id,
        };
        deleteMsg(params).then((res) => {
          if (res.code === 20000) {
            if (row.type == 1)
              this.getSupList();
            else if (row.type == 2)
              this.getManuList();
            else
              this.getProductList();
          } else {
          }
        }).catch(() => {
        })
      }).catch(() => {
        this.loading = 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(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;
    // });
    // if (this.$store.getters.customerId == 110) {
    //   this.ckeck = true;
    // }


    this.getProductList();
    this.getManuList();
    this.getSupList();
  },
};
</script>

