<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-kpi__kpiresultgrid}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <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 @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('kpi:gridResult:export')" type="primary" @click="exportHandle()">{{ $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="rownum" label="排名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="allDeptNames" label="所属机构" header-align="center" align="center" width="440"></el-table-column>
        <el-table-column prop="gridLeader" label="网格长" header-align="center" align="center"></el-table-column>
        <!-- 手动打分 -->
        <el-table-column prop="manualScore" label="抓基层党建工作职责得分" header-align="center" align="center"></el-table-column>
        <el-table-column prop="workScore" label="平时工作得分" header-align="center" align="center"></el-table-column>
        <el-table-column prop="finalScore" label="总分" header-align="center" align="center"></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 './resultGrid-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/kpi/gridResult/page',
        getDataListIsPage: true,
        deleteURL: '/kpi/gridResult',
        deleteIsBatch: true,
        exportURL: '/kpi/gridResult/export'
      },
      dataForm: {
        id: '',
        gridId: '',
        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: {
    detailQuery (id) {
      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>
