<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-custom__evaluateofficer}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <!-- <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item> -->
        <div>
        <el-form-item>
          <el-button type="primary" @click="backToDeptList">返回</el-button>
        </el-form-item>
        </div>
        <el-form-item label="干部名称" prop="fullName">
          <el-input v-model="dataForm.fullName" placeholder="干部名称" clearable></el-input>
        </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 type="primary" @click="evaluateofficerAddHandle(dataForm.deptId)">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('epdc:topic:export')" type="primary" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-upload class="upload-demo"
                     ref="upload"
                     v-loading="dataListLoading"
                     :action="uploadUrl"
                     :on-preview="handlePreview"
                     multiple
                     :limit="1"
                     :before-upload='beforeUpload'
                     :on-success='uploadSuccess'
                     :on-error='errorExceed'>
            <el-button type="primary">导入</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('custom:evaluateofficer:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="fullName" label="干部姓名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="sex" label="性别" header-align="center" align="center" :formatter="showFlagFormat"></el-table-column>
        <el-table-column prop="position" label="单位职务" header-align="center" align="center"></el-table-column>
        <el-table-column prop="likesCount" label="点赞次数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="opposeCount" label="被踩次数" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createdTime" label="创建时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="sort" 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="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</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>
      <evaluateofficer-add v-if="evaluateofficerAddVisible" ref="evaluateofficerAdd" @refreshDataList="getDataList"></evaluateofficer-add>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './evaluateofficer-add-or-update'
import evaluateofficerAdd from './evaluateofficer-add'
import Cookies from 'js-cookie'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/evaluateofficer/page',
        getDataListIsPage: true,
        deleteURL: '/custom/evaluateofficer',
        deleteIsBatch: true,
        exportURL: '/custom/evaluateofficer/export'
      },
      dataForm: {
        id: '',
        deptId: ''
      },
      evaluateofficerAddVisible: false,
      upLoadUrl: ''
    }
  },
  mounted () {
    // this.dataForm.id = this.$route.query.id
    this.dataForm.deptId = this.$route.query.deptId
    this.getDataList()
  },
  components: {
    AddOrUpdate,
    evaluateofficerAdd
  },
  created: function () {
    this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/custom/evaluateofficer/importExcel/${this.$route.query.deptId}?token=${Cookies.get('token')}`
  },
  methods: {
    showFlagFormat (row, column) {
      if (row.sex === '0') {
        return '女'
      } else {
        return '男'
      }
    },
    backToDeptList () {
      this.$emit('refreshDataList')
      this.$parent.selectComponent = 'Evaluatedept'
      this.$router.push({ path: '/custom-evaluatedeptroute'})
    },
    evaluateofficerAddHandle (deptId) {
      this.evaluateofficerAddVisible = true
      this.$nextTick(() => {
        this.$refs.evaluateofficerAdd.dataForm.deptId = deptId
        this.$refs.evaluateofficerAdd.init()
      })
    },
    beforeUpload (file) {
        if(confirm('上传会覆盖之前数据，是否继续？')==true){
          return true
        }else{
          return false
        }
    },
    errorExceed (file, fileList) {
      this.$message.error('上传失败请重试')
    },
    uploadSuccess (response, file, fileList) {
      this.getDataList()// 刷新列表
      this.dataListLoading = false
      this.$refs.upload.clearFiles()
      if (response.data !== null && response.data.length > 0) {
        this.faultDataVisible = true
        this.errordataList = response.data
        return
      }
      this.$message({
        message: this.$t('prompt.success'),
        type: 'success',
        duration: 500,
        onClose: () => {
          this.getDataList()
        }
      })
    }
  }
}
</script>
