<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-kpi__kpiresultsuperior}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataListSearch()">
        <el-form-item label="所属机构">
          <el-cascader
            v-model="deptIdList"
            :options="options"
            :props="{ checkStrictly: true }"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="考核周期起始月">
          <el-date-picker v-model="dataForm.scoreStartDate"
              type="month" clearable placeholder="选择月"
              value-format="yyyy-MM"  format="yyyy-MM">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getDataListSearch()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('kpi:gridResult:export')" type="success" @click="tableExport()">{{ $t('export') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column prop="allDeptNames" label="所属机构" header-align="center" align="center" min-width="150" show-overflow-tooltip></el-table-column>
        <el-table-column prop="finalScore" label="最终得分" header-align="center" align="center" width="120"></el-table-column>
        <el-table-column prop="startDate" label="考核周期开始日" header-align="center" :formatter="startDateFormat" align="center" width="180"></el-table-column>
        <el-table-column prop="endDate" label="考核周期结束日" header-align="center" :formatter="endDateFormat" align="center" width="180"></el-table-column>
        <el-table-column prop="kpiCycle" label="考核周期" header-align="center" :formatter="kpiCycleFormat" align="center" width="120"></el-table-column>
        <el-table-column prop="deptTypeKey" label="机构类别" header-align="center" :formatter="deptTypeKeyFormat" align="center" width="120"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="detailQuery(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './resultSuperior-add-or-update'
import Cookies from 'js-cookie'
import qs from 'qs'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/kpi/kpiresultsuperior/page',
        getDataListIsPage: true,
        deleteURL: '/kpi/kpiresultsuperior',
        deleteIsBatch: true,
        exportURL: '/kpi/kpiresultsuperior/export'
      },
      dataForm: {
        id: '',
        scoreStartDate: ''
      },
      // 所属机构配置
      deptIdList: [],
      options: [],
      streetList: [],
      communityList: [],
      gridList: []
    }
  },
  created () {
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUser`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => {})
  },
  methods: {
    // Excel导出
    tableExport () {
      let id = this.dataForm.id
      let params = qs.stringify({
        'token': Cookies.get('token'),
        'id': id,
        'scoreStartDate': this.dataForm.scoreStartDate,
        'streetId': this.deptIdList[0],
        'communityId': this.deptIdList[1],
        'gridId': this.deptIdList[2]
      })
      window.location.href = `${window.SITE_CONFIG['apiURL']}${this.mixinViewModuleOptions.exportURL}?${params}`
    },
    // 考核周期开始日字段,格式化
    startDateFormat: function (row) {
      return row.startDate.substring(0, 10)
    },
    // 考核周期结束日字段,格式化
    endDateFormat: function (row) {
      return row.endDate.substring(0, 10)
    },
    // 考核周期,格式化
    kpiCycleFormat: function (row) {
      if (row.kpiCycle === 'month') {
        return '月'
      } else if (row.kpiCycle === 'quarter') {
        return '季'
      } else if (row.kpiCycle === 'year') {
        return '年'
      }
    },
    // 机构类别,格式化
    deptTypeKeyFormat: function (row) {
      if (row.deptTypeKey === 'district_party') {
        return '区党委'
      } else if (row.deptTypeKey === 'district_dept') {
        return '区直部门'
      } else if (row.deptTypeKey === 'street_party') {
        return '街道党工委'
      } else if (row.deptTypeKey === 'street_dept') {
        return '街道部门'
      } else if (row.deptTypeKey === 'community_party') {
        return '社区党工委'
      } else if (row.deptTypeKey === 'grid_party') {
        return '网格党支部'
      }
    },
    // 跳转到详情页
    detailQuery (id, startDate, endDate, deptId) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id
        this.$refs.addOrUpdate.init()
      })
    }
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 1) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 2) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = ''
      }
      if (val.length === 3) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = this.deptIdList[2]
      }
    }
  },
  components: {
    AddOrUpdate
  }
}
</script>
