<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>
    <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>
          <dept-access-list :list="list" :query="query" class="assess-table"/>
        </el-tab-pane>
        <el-tab-pane ref="chartTab" name="chart">
          <span slot="label"><i class="el-icon-s-data"/> 图表模式</span>
          <dept-access-chart :list="list.slice(0,list.length-1)"/>
        </el-tab-pane>
      </el-tabs>
      <el-button type="text" class="rule-button" @click="dialogVisible = true">考核规则</el-button>
      <el-button type="text" class="rule-button" style="right: 85px" @click="exportFile">导出报表</el-button>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      :append-to-body="true"
      :close-on-click-modal="false"
      title="考核规则"
      width="65%"
      @close="fetchData"
    >
      <access-rule/>
    </el-dialog>
  </app-container>
</template>

<script>
import { getDayTime } from '@/utils/dateutils'
import DeptAccessList from './deptAccessList'
import DeptAccessChart from './deptAccessChart'
import AccessRule from './accessRule'
import { departmentAssess, exportAssessDept } from '@/api/assess/assessDept'

export default {
  name: 'DeptAccess',
  components: { DeptAccessList, DeptAccessChart, AccessRule },
  data() {
    return {
      activeName: 'list', // 默认tab
      dialogVisible: false,
      timeRange: [],
      query: {
        begTime: '',
        endTime: ''
      },
      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])
          }
        }]
      },
      list: []
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.query.begTime = val[0]
        this.query.endTime = val[1]
      } else {
        this.query.begTime = ''
        this.query.endTime = ''
      }
    }
  },
  activated() {
    this.fetchData()
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      if (this.query.begTime === '' || this.query.endTime === '') {
        // this.query.begTime = moment().subtract(1, 'months').startOf('month').format('YYYY-MM-DD')
        // this.query.endTime = moment().subtract(1, 'months').endOf('month').format('YYYY-MM-DD')
        this.query.begTime = getDayTime(new Date().getTime() - 24 * 7 * 60 * 60 * 1000).Format('yyyy-MM-dd')
        this.query.endTime =  new Date().Format('yyyy-MM-dd')
        this.timeRange = [this.query.begTime, this.query.endTime]
      }
      departmentAssess(this.query).then(res => {
        this.list = res.data
      })
    },
    handleClick(tab, event) {
      this.activeName = tab.name
      if (tab.name === 'chart') {
        console.log(tab.$children[0])
        setTimeout(() => {
          tab.$children[0].initChart()
        }, 100)
      }
    },
    search() {
      this.fetchData()
    },
    exportFile() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      exportAssessDept(this.query).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)
      })
    }
    // updateRule() {
    //   this.dialogVisible = false
    //   this.fetchData()
    // }
  }
}
</script>

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


/*.assess-container{*/
  /*margin: 20px;*/
/*}*/
/*.assess-table{*/
  /*margin-bottom: 30px;*/
/*}*/

/*.assess-container /deep/.cell{*/
  /*padding-left: 3px !important;*/
  /*padding-right: 3px;*/
/*}*/
/*.assess-container /deep/ td{*/
  /*padding-top: 12px;*/
  /*padding-bottom: 12px;*/
/*}*/
</style>
