<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-news__topic}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="街道">
          <el-select v-model="dataForm.streetId" placeholder="请选择" clearable @change="getCommunityList">
            <el-option v-for="item in streetOptions" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="社区">
          <el-select v-model="dataForm.communityId"
                     placeholder="请选择"
                     clearable
                     @change="getGridList">
            <el-option v-for="item in communityOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="网格">
          <el-select v-model="dataForm.gridId" clearable
                     placeholder="请选择">
            <el-option v-for="item in gridOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间"
                      prop="startTime">
          <el-date-picker v-model="dataForm.startTime"
                          type="date"
                          :picker-options="pickerBeginDateBefore"
                          value-format="yyyy-MM-dd"
                          format="yyyy-MM-dd"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="至"
                      label-width="25px"
                      prop="endTime">
          <el-date-picker v-model="dataForm.endTime"
                          type="date"
                          :picker-options="pickerBeginDateAfter"
                          value-format="yyyy-MM-dd"
                          format="yyyy-MM-dd"
                          placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排名">
          <el-select v-model="this.orderType" clearable
                     placeholder="请选择">
            <el-option v-for="item in orderTypeOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-if="this.orderType === '0'" v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column label="排名" type="index" show-overflow-tooltip align="center" width="50"></el-table-column>
        <el-table-column prop="groupName" label="群名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="ownGrid" label="所属网格" header-align="center" align="center"></el-table-column>
      </el-table>
      <el-table v-if="this.orderType === '1'" v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column label="排名" type="index" show-overflow-tooltip align="center" width="50"></el-table-column>
        <el-table-column prop="groupName" label="群名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="memberNum" label="群成员数量" header-align="center" align="center"></el-table-column>
        <el-table-column prop="ownGrid" label="所属网格" header-align="center" align="center"></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>
      <close v-if="closeVisible" ref="close" @refreshDataList="getDataList"></close>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import Close from './topic-close'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/group/group/activity',
        getDataListIsPage: true
      },
      closeVisible: false,
      detailVisible: false,
      dataForm: {
        id: '',
        streetId: '',
        communityId: '',
        gridId: '',
        startTime: '',
        endTime: ''
      },
      pickerBeginDateBefore: {
        disabledDate: (time) => {
          let beginDateVal = this.dataForm.startTime
          if (beginDateVal) {
            return time.getTime() > new Date(beginDateVal).getTime()
          }
        }
      },
      pickerBeginDateAfter: {
        disabledDate: (time) => {
          let EndDateVal = this.dataForm.endTime
          if (EndDateVal) {
            return time.getTime() < new Date(EndDateVal).getTime()
          }
        }
      },
      orderType: '0',
      streetOptions: [],
      communityOptions: [],
      gridOptions: [],
      orderTypeOptions: [{
        id: '0',
        name: '按活跃度排名'
      }, {
        id: '1',
        name: '按群成员排名'
      }]
    }
  },
  watch: {
    'orderType': function (val) {
      if (val === '0') {
        this.mixinViewModuleOptions.getDataListURL = '/group/group/activity'
      } else {
        this.mixinViewModuleOptions.getDataListURL = '/group/group/member'
      }
      this.getDataList()
    }
  },
  components: {
    Close
  },
  created: function () {
    this.getStreetList()
  },
  methods: {
    getStreetList () {
      this.$http.get(`/sys/dept/sublist/` + (localStorage.getItem('street') === null ? '1169154711480528897' : localStorage.getItem('street'))).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.streetOptions = res.data
      }).catch(() => { })
    },
    getCommunityList () {
      if (this.dataForm.streetId === '') {
        this.communityOptions = []
        this.gridOptions = []
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
        return
      }
      this.dataForm.communityId = ''
      this.dataForm.gridId = ''
      this.$http.get(`/sys/dept/sublist/` + this.dataForm.streetId).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.communityOptions = res.data
      }).catch(() => { })
    },
    getGridList () {
      if (this.dataForm.communityId === '') {
        this.gridOptions = []
        this.dataForm.gridId = ''
        return
      }
      this.dataForm.gridId = ''
      this.$http.get(`/sys/dept/sublist/` + this.dataForm.communityId).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.gridOptions = res.data
      }).catch(() => { })
    }
  }
}
</script>
