<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-news__topic}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataListSearch()">
        <el-form-item label="所属机构">
          <el-cascader v-model="ids" :options="options" :props="{ checkStrictly: true }" clearable style="width:200px;">
          </el-cascader>
        </el-form-item>
        <el-form-item label="群名">
          <el-select v-model="dataForm.groupId" clearable
                     placeholder="请选择" style="width:200px;">
            <el-option v-for="item in groupOptions"
                       :key="item.groupId"
                       :label="item.groupName"
                       :value="item.groupId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键词" prop="keyword">
          <el-input v-model="dataForm.keyword" placeholder="请输入关键词" clearable style="width:200px;" @keyup.native="btKeyUpKeyWord"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="排序方式">
          <el-select v-model="dataForm.orderType" clearable style="width:200px;"
                     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 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="选择日期时间"
                          style="width:200px;"
                          @change="changeTime">
          </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="选择日期时间"
                          style="width:200px;"
                          @change="changeTime">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="getDataListSearch()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('epdc:topic:export')" type="success" @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 label="序号" type="index" show-overflow-tooltip align="center" width="50"></el-table-column>
        <el-table-column prop="nickname" label="发言人" header-align="center" align="center" width="180" show-overflow-tooltip title="nickname"></el-table-column>
        <el-table-column prop="createdTime" label="时间" header-align="center" align="center" width="180"></el-table-column>
        <el-table-column prop="topicContent" label="话题内容" header-align="center" align="left" min-width="400"  show-overflow-tooltip title="topicContent"></el-table-column>
        <el-table-column prop="commentNum" label="评论数" header-align="center" align="center" width="100"></el-table-column>
        <el-table-column prop="browseNum" label="浏览数" header-align="center" align="center" width="100"></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="look(scope.row.id)">{{ $t('look') }}</el-button>
            <el-button v-if="scope.row.state !== 20" type="text" size="small" @click="close(scope.row.id)">{{ $t('close') }}</el-button>
            <el-button type="text" size="small" v-if="scope.row.state ===20" :disabled="true">已关闭</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>
      <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/topic/page',
        getDataListIsPage: true,
        exportURL: '/group/topic/export'
      },
      closeVisible: false,
      dataForm: {
        id: '',
        streetId: '',
        communityId: '',
        deptId: '',
        startTime: '',
        endTime: '',
        groupId: '',
        keyword: '',
        orderType: ''
      },
      pickerBeginDateBefore: {
        disabledDate: (time) => {
          let beginDateVal = this.dataForm.endTime
          if (beginDateVal) {
            return time.getTime() > new Date(beginDateVal + ' 00:00:00').getTime()
          }
        }
      },
      pickerBeginDateAfter: {
        disabledDate: (time) => {
          let EndDateVal = this.dataForm.startTime
          if (EndDateVal) {
            return time.getTime() < new Date(EndDateVal + ' 00:00:00').getTime()
          }
        }
      },
      ids: [],
      options: [],
      groupOptions: [],
      orderTypeOptions: [{
        id: '0',
        name: '按发布时间排序'
      }, {
        id: '1',
        name: '按浏览数排序'
      }, {
        id: '2',
        name: '按评论数排序'
      }]
    }
  },
  components: {
    Close
  },
  created: function () {
    this.getOptions()
    this.getGroupList()
  },
  watch: {
    'ids': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.deptId = ''
      }
      if (val.length === 1) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = ''
        this.dataForm.deptId = ''
      }
      if (val.length === 2) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = this.ids[1]
        this.dataForm.deptId = ''
      }
      if (val.length === 3) {
        this.dataForm.streetId = this.ids[0]
        this.dataForm.communityId = this.ids[1]
        this.dataForm.deptId = this.ids[2]
      }
    }
  },
  methods: {
    close (id) {
      this.closeVisible = true
      this.$nextTick(() => {
        this.$refs.close.dataForm.id = id
        this.$refs.close.init()
      })
    },
    look (id) {
      this.$parent.selectComponent = 'TopicDetail'
      this.$router.push({ path: '/group-topic', query: { id: id } })
    },
    getGroupList () {
      this.$http.get('/group/group/groupList').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.groupOptions = res.data
      }).catch(() => { })
    },
    getOptions () {
      var paa = {deptType:'party'}
      this.$http.post(`/sys/user/deptOptions/getByLoginUserNew`,paa).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      }).catch(() => {})
    },
      btKeyUpKeyWord(e){
      e.target.value = e.target.value.replace(/[`~!#$%^&*()_\+=<>?:"{}|~！#￥%……&*（）={}|《》？：“”【】\\[\]、；‘’，。、\s+]/g, '')
      this.dataForm.keyword= e.target.value
    }
  }
}
</script>
