<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-custom__specialuser}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="所属村庄">
          <el-input v-model="dataForm.suVillage" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="dataForm.suName" placeholder="请输入" clearable></el-input>
      </el-form-item>

        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info"
                     @click="exportMoudleHandle()">导出模板</el-button>
        </el-form-item>
        <el-button type="primary" @click="showPrompt">点击上传</el-button>
        <el-form-item>
          <el-upload class="upload-demo"
                     ref="upload"
                     :action="uploadUrl"
                     :limit="1"
                     :on-error='uploadError'
                     :on-success='uploadSuccess'>
            <el-button type="primary" v-show="false" id="handleClick">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button 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="suVillage" label="所属村庄" header-align="center" align="center" width="150"></el-table-column>
        <el-table-column prop="suName" label="姓名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="suBirthday" label="出生日期" header-align="center" align="center" :formatter="dateFormatter" width="100"></el-table-column>
        <el-table-column prop="suDisabilityNumber" label="残疾证号" header-align="center" align="center" width="200"></el-table-column>
        <el-table-column prop="suDisabilityType" label="残疾类别及等级" header-align="center" align="center"  width="100"></el-table-column>
        <el-table-column prop="suIsLow" label="是否低保" header-align="center" align="center" :formatter="isFormatter"></el-table-column>
        <el-table-column prop="suLowType" label="低保类型" header-align="center" align="center" width="100"></el-table-column>
        <el-table-column prop="suGuaranteedPopulation" label="保障人口" header-align="center" align="center"></el-table-column>
        <el-table-column prop="suRelationship" label="关系" header-align="center" align="center"></el-table-column>
        <el-table-column prop="suLowMoney" label="低保金额" header-align="center" align="center"></el-table-column>
        <el-table-column label="两项补贴"  header-align="center" align="center">
          <el-table-column prop="suLivingSubsidy" label="困难残疾人生活补贴" header-align="center" align="center" width="100"></el-table-column>
          <el-table-column prop="suCareSubsidy" label="重度残疾人护理补贴" header-align="center" align="center" width="100"></el-table-column>
        </el-table-column>
        <el-table-column prop="suElderlySubsidy" label="经济困难老年人补贴" header-align="center" align="center"></el-table-column>
        <el-table-column prop="suChildSubsidy" label="孤困儿童补贴" header-align="center" align="center"></el-table-column>
        <el-table-column prop="suFiveGuaranteesSubsidy" label="分散五保补贴" header-align="center" align="center"></el-table-column>
        <el-table-column prop="suIsPoor" label="是否贫困户" header-align="center" align="center" :formatter="isFormatter"></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>
      <el-dialog :visible.sync="faultDataVisible"
                 title="请修改后重新提交">
        <el-table :data="errordataList"
                  border
                  style="width: 100%;">
          <el-table-column prop="erroLine"
                           label="行数"
                           header-align="center"
                           align="center"></el-table-column>
          <el-table-column prop="errorInfo"
                           label="错误信息"
                           header-align="center"
                           align="center"></el-table-column>
        </el-table>
        <template slot="footer">
          <el-button type="primary"
                     @click="faultDataVisible = false">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './specialuser-add-or-update'
import Cookies from 'js-cookie'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/specialuser/page',
        getDataListIsPage: true,
        deleteURL: '/custom/specialuser',
        deleteIsBatch: true,
        exportURL: '/custom/specialuser/export',
        exportMoudle: '/custom/specialuser/exportModule'
      },
      dataForm: {
        id: ''
      },
      uploadUrl: '',
      faultDataVisible: false,
      errordataList: []
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/custom/specialuser/importExcel?token=${Cookies.get('token')}`
  },
  methods: {
    errorExceed (file, fileList) {
      this.$message.error('上传失败请重试')
    },
    uploadError (error, file, fileList) {
      this.$message.error(error)
    },
    uploadSuccess (response, file, fileList) {
      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()
        }
      })
    },
    isFormatter (row, column, cellValue, index) {
      if (cellValue) {
        if (cellValue === '1') {
          return '是'
        } else {
          return '否'
        }
      }
      return ''
    },
    dateFormatter (row, column, cellValue, index) {
      if (cellValue) {
        return cellValue.substring(0, 10)
      }
      return ''
    },
    showPrompt () {
      this.$confirm('导入数据默认新增，不会替换已有数据，是否确认导入', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        document.getElementById('handleClick').click()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消导入'
        })
      })
    }
  }
}
</script>
