<template>
  <el-card class="box-card" shadow="always">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="时间">
        <el-date-picker
          v-model="queryParams.checkDate"
          type="date"
          :clearable="false"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOption"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row type="flex" justify="space-between">
      <el-col :span="12">
      </el-col>
      <el-col :span="12">
        <div class="right-toolbar">
          <el-tooltip content="密度" effect="dark" placement="top">
            <el-dropdown trigger="click" @command="handleCommand">
              <el-button circle size="mini">
                <i class="el-icon-s-grid"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="medium">正常</el-dropdown-item>
                <el-dropdown-item command="small">中等</el-dropdown-item>
                <el-dropdown-item command="mini">紧凑</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-tooltip>
          <el-tooltip content="刷新" effect="dark" placement="top">
            <el-button circle size="mini" @click="handleRefresh">
              <i class="el-icon-refresh"></i>
            </el-button>
          </el-tooltip>
          <el-tooltip content="列设置" effect="dark" placement="top">
            <el-popover placement="bottom" width="100" trigger="click">
              <el-checkbox-group v-model="checkedTableColumns" @change="handleCheckedColsChange">
                <el-checkbox
                  v-for="(item, index) in tableColumns"
                  :key="index"
                  :label="item.prop"
                >{{ item.label }}</el-checkbox>
              </el-checkbox-group>
              <span slot="reference">
                <el-button circle size="mini">
                  <i class="el-icon-setting"></i>
                </el-button>
              </span>
            </el-popover>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableDataList"
      border
      tooltip-effect="dark"
      :height="tableHeight"
      :size="tableSize"
      style="width: 100%;margin: 15px 0;"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 }}</span>
        </template>
      </el-table-column>
      <template v-for="(item, index) in tableColumns">
        <el-table-column
          v-if="item.show"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          :formatter="item.formatter"
          align="center"
          show-overflow-tooltip
        />
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleReport(scope.row)"
          >查看报告</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-bottom: 50px"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="queryParams.pageNum"
      :page-size.sync="queryParams.pageSize"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
  import { pageCheckReport } from '@/api/quality/checkreport'
  import moment from 'moment'
  export default {
    name: 'CheckReportList',
    data() {
      return {
        tableHeight: document.body.offsetHeight - 330 + 'px',
        // 展示切换
        showOptions: {
          data: {},
          showList: true
        },
        // 遮罩层
        loading: true,
        // 表格头
        tableColumns: [
          {
            prop: 'checkReportType',
            label: '报告类型',
            show: true,
            formatter: this.checkReportTypeFormatter
          },
          { prop: 'checkDate', label: '检测时间', show: true },
          {
            prop: 'checkResult',
            label: '检测结果',
            show: true,
            formatter: this.checkResultFormatter
          },
          { prop: 'checkTotalCount', label: '检测数量', show: true },
          { prop: 'checkErrorCount', label: '不合规数量', show: true }
        ],
        // 默认选择中表格头
        checkedTableColumns: [],
        tableSize: 'medium',
        // 状态数据字典
        statusOptions: [],
        // 数据集表格数据
        tableDataList: [],
        // 总数据条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          checkDate: moment(moment().add(0, 'days').startOf('day').valueOf()).format('YYYY-MM-DD')
        },
        pickerOption: {
          disabledDate(date) {
            return date.getTime() > Date.now()
          }
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      initCols() {
        this.checkedTableColumns = this.tableColumns.map(col => col.prop)
      },
      handleCheckedColsChange(val) {
        this.tableColumns.forEach(col => {
          if (!this.checkedTableColumns.includes(col.prop)) {
            col.show = false
          } else {
            col.show = true
          }
        })
      },
      handleCommand(command) {
        this.tableSize = command
      },
      getList() {
        this.loading = true
        pageCheckReport(this.queryParams).then(response => {
          this.loading = false
          if (response.success) {
            const { data } = response
            this.tableDataList = data.data
            this.total = data.total
          }
        })
      },
      handleReport(row) {
        this.showOptions.data = row
        this.showOptions.showList = false
        this.showOptions.showDetail = true
        this.$emit('showCard', this.showOptions)
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.queryParams.pageNum = 1
        this.queryParams.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.queryParams.pageNum = val
        this.getList()
      },
      checkReportTypeFormatter(row, column, cellValue, index) {
        if (cellValue === 'structure') {
          return '结构符合性检测报告'
        } else {
          return '内容符合性检测报告'
        }
      },
      checkResultFormatter(row, column, cellValue, index) {
        if (cellValue === '1') {
          return <el-tag type='success'> 成功</el-tag>
        } else {
          return <el-tag type='warning'> 失败</el-tag>
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 20,
          sourceName: ''
        }
        this.handleQuery()
      },
      /** 刷新列表 */
      handleRefresh() {
        this.getList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .right-toolbar {
    float: right;
  }
  .el-card ::v-deep .el-card__body {
    height: calc(100vh - 170px);
  }
</style>
