<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-__mastertopic}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataListSearch()">
        <el-form-item label="话题内容" prop="topicContent">
          <el-input v-model="dataForm.topicContent" placeholder="话题内容" clearable  maxlength="50" @keyup.native="btKeyUpTopicContent"></el-input>
        </el-form-item>
        <el-form-item label="发言人" prop="nickName" label-width="70px">
          <el-input v-model="dataForm.nickName" placeholder="发言人" clearable @keyup.native="btKeyUpNickName"></el-input>
        </el-form-item>
        <el-form-item label="排行榜" prop="orderBy">
          <el-select v-model="dataForm.orderBy" placeholder="类别" clearable>
            <el-option v-for="item in orderByArr"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <br>
        <el-form-item label="状态" prop="state" label-width="68px">
          <el-select v-model="dataForm.state" placeholder="状态" clearable>
            <el-option v-for="item in stateArr"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <div class="block">-->
          <el-form-item label="发布时间"
                        prop="startTime" label-width="70px">
            <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 type="primary" v-if="$hasPermission('cloudAnalysis:mastertopic:export')"
                       @click="exportHandle()">{{ $t('export') }}</el-button>
          </el-form-item>
<!--        </div>-->
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column label="序号" header-align="center" align="center" width="50px">
          <template slot-scope="scope">
            {{scope.$index+1}}
          </template>
        </el-table-column>
        <el-table-column prop="topicContent" label="话题内容" header-align="center" align="left" width="300" :show-overflow-tooltip='true' ></el-table-column>
        <el-table-column prop="nickName" label="发言人" header-align="center" align="center"></el-table-column>
        <el-table-column prop="browseNum" label="浏览数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="commentNum" label="评论数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="approveNum" label="点赞数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="opposeNum" label="点踩数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="totalParticipation" label="总参与度" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createdTime" label="发布时间" header-align="center" align="center" width="200"></el-table-column>
        <el-table-column prop="state" label="状态" header-align="center" align="center" :formatter="stateFormat"></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" v-if="$hasPermission('cloudAnalysis:mastertopic:toTop')  && scope.row.state == 1"  @click="openToTopDialog($event, scope.row)">置顶</el-button>
            <el-button type="text" size="small" v-if="$hasPermission('cloudAnalysis:mastertopic:toTop')  && scope.row.state == 2" @click="openCancelToTopDialog($event, scope.row)">取消置顶</el-button>
            <el-button type="text" size="small" v-if="$hasPermission('cloudAnalysis:mastertopic:close')" @click="openCloseTopicDialog($event, scope.row)" :disabled="scope.row.state == 0">关闭</el-button>
            <el-button type="text" size="small" :disabled="btnAble" @click="manageHandle(scope.row.id, scope.row.browseNum)">查看</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>
      <el-dialog
          title="确定要本条内容置顶吗?"
          :visible.sync="toTopDialog"
          width="30%">
        <span>置顶后，话题会被群成员优先看到，查看内容</span>
        <span slot="footer" class="dialog-footer">
        <el-button @click="toTopDialog = false">取 消</el-button>
        <el-button type="primary" @click="toTopOrCancelToTop" :disabled="toGrey">确 定</el-button>
       </span>
      </el-dialog>
      <el-dialog
          title="确定要本条内容取消置顶吗?"
          :visible.sync="cancelToTopDialog"
          width="30%">
        <span>取消置顶后，话题会被按照发布时间排序，需要滑动查找内容</span>
        <span slot="footer" class="dialog-footer">
        <el-button @click="cancelToTopDialog = false">取 消</el-button>
        <el-button type="primary" @click="toTopOrCancelToTop" :disabled="toGrey">确 定</el-button>
       </span>
      </el-dialog>
      <el-dialog
          title="确定要将本条内容关闭吗?"
          :visible.sync="closeDialog"
          width="30%">
        <span>关闭后，在移动端不显示话题内容，管理后台将保留数据，可以查看</span>
        <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog = false">取 消</el-button>
        <el-button type="primary" @click="toTopOrCancelToTop" :disabled="toGrey">确 定</el-button>
       </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './mastertopic-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/cloudAnalysis/mastertopic/page',
        getDataListIsPage: true,
        deleteURL: '/cloudAnalysis/mastertopic',
        deleteIsBatch: true,
        exportURL: '/cloudAnalysis/mastertopic/export'
      },
      dataForm: {
        id: '',
        nickName: '',
        topicContent: ''
      },
      optionForm: {
        id: '',
        state: ''
      },
      toTopDialog: false,
      cancelToTopDialog: false,
      closeDialog: false,
      isTop: true,
      toGrey: false,
      btnAble: false,
      orderByArr: [
        {
          label: '总参与度',
          value: '0'
        },
        {
          label: '浏览最多',
          value: '1'
        },
        {
          label: '评论最多',
          value: '2'
        },
        {
          label: '点赞最多',
          value: '3'
        },
        {
          label: '点踩最多',
          value: '4'
        }
      ],
      stateArr: [
        {
          label: '全部',
          value: '-1'
        },
        {
          label: '进行中',
          value: '1'
        },
        {
          label: '已关闭',
          value: '0'
        }
      ],
      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()
          }
        }
      }
    }
  },
  components: {
    AddOrUpdate
  },
  mounted () {
    this.getDataList()
  },
  methods: {
    stateFormat: function (row, column) {
      let state = row.state
      if (state === '0') {
        return '已关闭'
      } else if (state === '1' || state === '2') {
        return '进行中'
      }
    },
    manageHandle (topicId, browseNum) {
      this.btnAble = true
      // 将当前登录人改为已读
      this.$http.post(`/cloudAnalysis/mastertopic/addBrowNum`, { id: topicId, browseNum: browseNum }).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.btnAble = false
        this.$parent.selectComponent = 'Mastertopicdetail'
        this.$router.push({ path: '/master-mastertopicroute', query: { topicId: topicId } })
      }).catch(() => {})
    },
    openToTopDialog (value, row) {
      this.toTopDialog = true
      this.optionForm.id = row.id
      this.optionForm.state = 2
    },
    openCancelToTopDialog (value, row) {
      this.cancelToTopDialog = true
      this.optionForm.id = row.id
      this.optionForm.state = 1
    },
    toTopOrCancelToTop () {
      this.toGrey = true
      this.$http.put(`/cloudAnalysis/mastertopic`, this.optionForm).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.toTopDialog = false
        this.cancelToTopDialog = false
        this.closeDialog = false
        this.toGrey = false
        this.getDataList()
        this.$message({
          type: 'success',
          message: '修改成功!',
          onClose: () => {

          }
        })
      })
    },
    openCloseTopicDialog (value, row) {
      this.closeDialog = true
      this.optionForm.id = row.id
      this.optionForm.state = 0
    },
    btKeyUpTopicContent(e){
      e.target.value = e.target.value.replace(/[`~!@#$%^&*()_+=<>?:"{}|·~！@#￥%……&*（）——+={}|《》？：“”【】、；‘’，。、]/g, '')
      this.dataForm.topicContent = e.target.value
    },
    btKeyUpNickName(e){
      e.target.value = e.target.value.replace(/[`~!@#$%^&*()_+=<>?:"{}|·~！@#￥%……&*（）——+={}|《》？：“”【】、；‘’，。、]/g, '')
      this.dataForm.nickName = e.target.value
    }
  }
}
</script>
