<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" inline @submit.native.prevent label-width="90px">
      <el-form-item label="分公司" prop="impUnitName" >
        <el-input
          v-model="queryParams.impUnitName"
          placeholder="请输入分公司"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <el-form-item label="项目部" prop="departName">
        <el-input
          v-model="queryParams.departName"
          placeholder="请输入项目部"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <el-form-item label="工程" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入工程"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <el-form-item label="分包单位" prop="subUnitName">
        <el-input
          v-model="queryParams.subUnitName"
          placeholder="请输入分包单位"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <el-form-item label="物资状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 220px"
        >
          <el-option label="正常" value="0" />
          <el-option label="1个月检测到期" value="2" />
          <el-option label="3个月检测到期" value="1" />
          <el-option label="已过期" value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="检验时间">
        <el-date-picker
          v-model="queryParams.time"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 220px"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="班组名称" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入班组名称"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <el-form-item label="物资类型" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物资类型"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 220px"
        />
      </el-form-item>

      <!-- 表单按钮 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 添加统计行 -->
    <el-row type="flex" justify="end" :gutter="3" class="mb8 stat-row">
      <el-col :span="3">
        <div class="stat-item">已过期数量: <span class="status-expired">{{ expiredCount }}</span></div>
      </el-col>
      <el-col :span="3">
        <div class="stat-item">临期1个月数量: <span class="status-one-month">{{ oneMonthCount }}</span></div>
      </el-col>
      <el-col :span="3">
        <div class="stat-item">临期3个月数量: <span class="status-three-months">{{ threeMonthCount }}</span></div>
      </el-col>
    </el-row>

    <el-table :data="tableList" fit highlight-current-row style="width: 100%" :max-height="600">
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="index => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
      />
      <el-table-column label="分公司" align="center" prop="impUnitName"  :show-overflow-tooltip="true" />
      <el-table-column label="项目部" align="center" prop="departName"  :show-overflow-tooltip="true" />
      <el-table-column label="工程名称" align="center" prop="proName" width="160"  />
      <el-table-column label="分包单位" align="center" prop="subUnitName"  :show-overflow-tooltip="true" />
      <el-table-column label="班组名称" align="center" prop="teamName" width="120"  />
       <el-table-column label="物资类型" align="center" prop="materialName"  :show-overflow-tooltip="true" />
      <el-table-column label="物资名称" align="center" prop="typeName"  :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="typeModelName"  :show-overflow-tooltip="true" />
      <el-table-column label="设备编号" align="center" prop="maCode"  :show-overflow-tooltip="true" />
      <el-table-column label="本次检验时间" align="center" prop="thisCheckTime"  :show-overflow-tooltip="true" />
      <el-table-column label="下次检验时间" align="center" prop="nextCheckTime"  :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span :class="getStatusClass(scope.row.status)">{{ getStatusText(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检验报告" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
    <span
      style="color:#409EFF;cursor:pointer;"
      @click="openReport(scope.row)"
    >
      {{ scope.row.reportCode }}
    </span>
        </template>
      </el-table-column>


    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import TreeSelect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {getSafetyListApi,getSafeNumListApi,getAgreementIdApi  } from '@/api/materialsStation'

export default {
  components: { TreeSelect },
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '', // 关键字
        status: '', // 规格型号
        impUnitName: '', // 分公司
        departName: '', // 项目部
        proName: '', // 工程
        subUnitName: '', // 分包单位
        time: null, // 检验时间
        teamName: '', // 班组名称
        materialName: '', // 物资类型
        startTime: '', // 开始时间
        endTime: '' // 结束时间
      },
      total: 0, // 总条数
      // 表格数据
      tableList: [],
      // 添加统计数据变量
      expiredCount: 0,
      oneMonthCount: 0,
      threeMonthCount: 0
    }
  },
  created() {
    this.getList()
    this.getSafetyNum()
  },
  methods: {
    getStatusText(status) {
      switch (status) {
        case '0':
          return '正常';
        case '1':
          return '3个月检测到期';
        case '2':
          return '1个月检测到期';
        case '3':
          return '已过期';
        default:
          return '';
      }
    },
    // 根据状态值返回对应的 CSS 类名
    getStatusClass(status) {
      switch (status) {
        case '0':
          return 'status-normal';
        case '1':
          return 'status-three-months';
        case '2':
          return 'status-one-month';
        case '3':
          return 'status-expired';
        default:
          return '';
      }
    },

    // 查询
   async handleQuery() {
      this.queryParams.pageNum = 1
     await this.getList()
     await this.getSafetyNum()

    },
    // 重置
    async handleReset() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.$refs.queryForm.resetFields()
      this.queryParams.keyWord = ''
      this.queryParams.status = ''
      this.queryParams.time = null
      this.queryParams.startTime= ''
      this.queryParams.endTime= ''
      await this.getList()
      await this.getSafetyNum()
    },
    // 获取列表
    async getList() {
      console.log('列表-查询', this.queryParams)
      const loading = this.$loading({ text: '加载中...' })
      this.queryParams.startTime = this.queryParams.time ? this.queryParams.time[0] : '';
      this.queryParams.endTime = this.queryParams.time? this.queryParams.time[1] : '';
      try {
        const resTemp = await getAgreementIdApi()
        console.log("xxxxxhhhhhhhhhh",resTemp)
        const params = { ...this.queryParams,agreementIdList:resTemp.data}
        const res = await getSafetyListApi(params)
        console.log('🚀 ~ 获取列表 ~ res:', res)
        this.tableList = res.data.rows
        this.total = res.data.total
        loading.close()
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)
        this.tableList = []
        this.total = 0
        loading.close()
      }
    },
    // 获取列表
    async getSafetyNum() {

      try {
        const resTemp = await getAgreementIdApi()
        const params = { ...this.queryParams,agreementIdList:resTemp.data}
        const res = await getSafeNumListApi(params)
        console.log("yyyyyyy",res.data)
        this.expiredCount = res.data.expiredNum
        this.oneMonthCount = res.data.oneMonthNum
        this.threeMonthCount = res.data.threeMonthNum
        console.log("xxxxxxxx",res.data.expiredNum,this.expiredCount)
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)

      }
    },
    openReport(row) {
      let reportUrl = row.reportUrl;
      let pdfUrl = "";
      if (/https?/i.test(reportUrl)) {
        console.log("URL包含http/https");
        pdfUrl = reportUrl;
      } else {
        console.log("URL不包含http/https");
        pdfUrl = "http://sgwpdm.ah.sgcc.com.cn/iws/smw/filePath/" + reportUrl;
      }
      // 打开新窗口
      window.open(pdfUrl, "_blank");
    },
  }
}
</script>

<style lang="scss" scoped>
// 定义不同状态的颜色样式
.status-normal {
  color: #67C23A; // 绿色
}
.status-three-months {
  color: #E6A23C; // 橙色
}
.status-one-month {
  color: #F56C6C; // 红色
}
.status-expired {
  color: #909399; // 灰色
}
// .stat-item {
//   padding: 8px 16px;
//   background-color: #f5f7fa;
//   border-radius: 4px;
//   display: inline-block;
//   margin-right: 10px;
// }

.stat-row {
  margin-bottom: 10px;

  .stat-item {
    background: #f5f7fa;
    padding: 8px 16px;
    border-radius: 4px;
    text-align: center;
    font-size: 16px;
    white-space: nowrap;
  }

}
</style>
