<template>
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" inline>
      <el-form-item label="工程名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入工程名称"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程负责人" prop="createUserName">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="请输入工程负责人"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </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">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="tableList" fit highlight-current-row style="width: 100%">
      <!-- 多选 -->
      <el-table-column type="selection" width="55" align="center" @selection-change="selectionChange" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="(column, index) in tableColumns"
        show-overflow-tooltip
        :key="column.prop"
        :label="column.label"
        :prop="column.prop"
        align="center"
      ></el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDetail(scope.row)">查看详情</el-button>
        </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 { getHiddenDangerStatisticsList, exportHiddenDangerStatisticsList } from '@/api/hiddenDangerManagement'

export default {
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: '', // 工程名称
        createUserName: '', // 工程负责人
      },
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '工程名称', prop: 'proName' },
        { label: '工程负责人', prop: 'createUserName' },
        { label: '隐患问题总数', prop: 'hiddenDangerCount' },
        { label: '已完成整改数', prop: 'rectificationCount' },
        { label: '未完成整改数', prop: 'unRectificationCount' },
      ],
      // 表格数据
      tableList: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查询
    handleQuery() {
      console.log('查询', this.queryParams)
      this.getList()
    },
    // 重置
    handleReset() {
      console.log('重置')
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      try {
        const params = { ...this.queryParams }
        console.log('🚀 ~ getList ~ params:', params)
        const res = await getHiddenDangerStatisticsList(params)
        console.log('🚀 ~ 获取列表 ~ res:', res)
        this.tableList = res.rows
        this.total = res.total
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)
      }
    },
    // 多选
    selectionChange(val) {
      console.log('selectionChange', val)
    },
    // 详情
    handleDetail(row) {
      console.log('详情', row)
      this.$router.push({
        path: '/hiddenDangerManagement/hazardStatistics/details',
        query: {
          ...row,
          type: 1,
        },
      })
    },
    // 导出数据
    handleExport() {
      try {
        let fileName = `工程隐患统计_${new Date().getTime()}.xLsx`
        let url = exportHiddenDangerStatisticsList()
        const params = { ...this.queryParams }
        console.log('🚀 ~ 导出 ~ params:', params)
        this.derive(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    },
  },
}
</script>

<style lang="scss" scoped></style>
