<!--监督员考核-->
<template>
  <app-container>
    <search-area :need-search-more="false" type="default" size="small" @search="search">
      <search-item >
        <el-date-picker
          v-model="timeRange"
          :picker-options="pickerOptions"
          type="daterange"
          size="small"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </search-item>
    </search-area>
    <!--<normal-table :data="list" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" @change="changePage">
      <template slot="btns">
        <el-button size="small" class="edit_btn" @click="exportTable">导出报表</el-button>
      </template>
    </normal-table>-->
    <div class="tab-block">
      <el-tabs ref="tabs" v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane name="list">
          <span slot="label"><i class="el-icon-s-fold"/> 列表模式</span>
          <!--<normal-table :data="list" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" @change="changePage"/>-->
          <normal-table :data="list.slice((listQuery.offset-1)*listQuery.limit,listQuery.offset*listQuery.limit)" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" @change="changePage"/>
        </el-tab-pane>
        <el-tab-pane ref="chartTab" name="chart">
          <span slot="label"><i class="el-icon-s-data"/> 图表模式</span>
          <access-area-chart :list="list"/>
        </el-tab-pane>
      </el-tabs>
      <el-button type="text" class="super-button" @click="dialogVisible = true">考核规则</el-button>
      <el-button type="text" class="super-button" style="right: 85px" @click="exportTable">导出报表</el-button>
    </div>
  </app-container>
</template>

<script>
import { getDayTime } from '@/utils/dateutils'
import { assessArea, exportAssessArea } from '@/api/assess/assessRegion'
import AccessAreaChart from './accessAreaChart'
export default {
  name: 'AssessArea',
  components: { AccessAreaChart },
  data() {
    return {
      activeName: 'list', // 默认tab
      props: [], // 存储列表页传递的row
      list: [], // 数据列表
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      columns: [
        {
          text: '网格名称',
          value: 'areaName'
        },
        {
          text: '公众举报率',
          value: 'publicReportRate'
        },
        {
          text: '立案数',
          value: 'register'

        },
        {
          text: '结案数',
          value: 'close'
        },
        {
          text: '结案率',
          value: 'closeRate'
        },
        {
          text: '综合指标',
          value: 'score'
        },
        {
          text: '等级',
          value: 'grade'
        }
      ],
      listQuery: {
        beginDate: '',
        endDate: '',
        offset: 1,
        limit: 10,
        sort: '',
        order: ''
      }, // 筛选条件
      timeRange: [],
      total: 0,
      dialogVisible: false, // 考核规则的显示
      listLoading: true // 加载动画
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.beginDate = val[0]
        this.listQuery.endDate = val[1]
      } else {
        this.listQuery.beginDate = ''
        this.listQuery.endDate = ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      if (this.listQuery.beginDate === '') {
        const defaultBeginTime = getDayTime(new Date().getTime() - 24 * 7 * 60 * 60 * 1000).Format('yyyy-MM-dd')
        const defaultEndTime = new Date().Format('yyyy-MM-dd')
        if (this.timeRange.length === 0) {
          this.timeRange = [defaultBeginTime, defaultEndTime]
        }
        this.listQuery.beginDate = this.timeRange[0]
        this.listQuery.endDate = this.timeRange[1]
      }
      assessArea(this.listQuery).then(res => {
        this.list = res.data
        this.total = res.data.length
        this.listLoading = false
      })
    },
    showCaseList(row, value) {
      debugger
      const query = {
        personId: row.personId,
        beginDate: this.listQuery.beginDate,
        endDate: this.listQuery.endDate,
        type: value
      }
      this.$router.push({ path: '/assessCaseList', query: query })
    },
    // 生成报表
    exportTable() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      exportAssessArea(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        console.log('download===', res)
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `行政区域评价列表.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      }).catch((res) => {
        loading.close()
        this.$message.error(res.message)
      })
    },
    // 来自业务查询条件更改后的查询
    search() {
      this.fetchData(false)
    },
    // 点击告警规则
    setRule() {
      this.dialogVisible = true
    },
    // 计算行号
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    handleClick(tab, event) {
      this.activeName = tab.name
      if (tab.name === 'chart') {
        console.log(tab.$children[0])
        setTimeout(() => {
          tab.$children[0].initChart()
        }, 100)
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

  .super-button{
    position: absolute;
    top: 76px;
    right: 10px;
    /* color: red; */
  }
  .tab-block{
    border-top: 12px solid #ebebeb;
    padding: 10px;
  }
</style>

