<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-news__reportissue}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.content" placeholder="举报问题描述" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否匿名" prop="anonymousFlag">
          <el-select style="width: 100px" v-model="dataForm.anonymousFlag" placeholder="请选择" clearable>
            <el-option
                    v-for="item in anonymous"
                    :key="item.optionValue"
                    :label="item.optionName"
                    :value="item.optionValue">
            </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>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('news:reportissue:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('news:reportissue:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border style="width: 100%;">
        <el-table-column prop="content" label="举报问题描述" show-overflow-tooltip header-align="center" align="center"></el-table-column>
        <el-table-column prop="createdTime" label="提交时间" width="200px" header-align="center" align="center"></el-table-column>
        <el-table-column prop="anonymousFlag" label="是否匿名" width="120px" header-align="center" align="center" :formatter="anonymousFlagFormat"></el-table-column>
        <el-table-column prop="reportUser" label="举报人" width="150px" header-align="center" align="center"></el-table-column>
        <el-table-column prop="reportUserMobile" label="举报人联系电话" width="150px" 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="addOrUpdateHandle(scope.row.id)">{{ $t('look') }}</el-button>
            <el-button v-if="scope.row.shieldFlag === '0'"
                       type="text"
                       size="small"
                       @click="shield(scope.row.id)">屏蔽</el-button>
            <el-button v-if="scope.row.shieldFlag === '1'"
                       type="text"
                       size="small" style="color: darkgrey">已屏蔽</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 './reportissue-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/reportissue/page',
        getDataListIsPage: true,
        deleteURL: '/custom/reportissue',
        deleteIsBatch: true
      },
      dataForm: {
        content: '',
        anonymousFlag: '',
        startTime: '',
        endTime: ''
      },
      anonymous: [ { 'optionValue': '0', 'optionName': '否' }, { 'optionValue': '1', 'optionName': '是' } ],
      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()
          }
        }
      }
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    anonymousFlagFormat: function (row, column) {
      let anonymousFlag = row.anonymousFlag
      if (anonymousFlag === '0') {
        return '否'
      } else if (anonymousFlag === '1') {
        return '是'
      }
    },
    shield (val) {
      this.$prompt('', '请输入屏蔽原因', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        closeOnPressEscape: false,
        inputPlaceholder: '100字以内',
        inputPattern: /^[\S\s]{1,100}$/,
        inputErrorMessage: '屏蔽原因不能为空且在100个字以内'
      }).then(({ value }) => {
        this.$http['post']('/custom/reportissue/shield', { id: val, shieldReason: value }).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.getDataList()
            }
          })
        }).catch(() => { })
      }).catch(() => {
      })
    }
  }
}
</script>
