<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-__partymembers}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataList()">
        <el-form-item label="所属机构">
          <el-cascader v-model="deptIdList"
                       :options="options"
                       :props="{ checkStrictly: true }"
                       clearable>
          </el-cascader>
        </el-form-item>
        <!-- <el-form-item label="街道">
          <el-select v-model="dataForm.streetId"
                     clearable
                     placeholder="请选择"
                     @change="getCommunityList">
            <el-option v-for="item in streetOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="社区">
          <el-select v-model="dataForm.communityId"
                     clearable
                     placeholder="请选择"
                     @change="getGridList">
            <el-option v-for="item in communityOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="网格">
          <el-select v-model="dataForm.gridId"
                     clearable
                     placeholder="请选择">
            <el-option v-for="item in gridOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="注册状态">
          <el-select v-model="dataForm.registFlag"
                     clearable
                     placeholder="请选择">
            <el-option v-for="item in registOptions"
                       :key="item.dictValue"
                       :label="item.dictName"
                       :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item> -->
        <br>
        <el-form-item label="姓名">
          <el-input v-model="dataForm.realName"
                    placeholder=""
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="dataForm.mobile"
                    placeholder=""
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="dataForm.identityNo"
                    placeholder=""
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="职务">
          <el-select v-model="dataForm.postId"
                     placeholder="请选择">
            <el-option v-for="item in postOptions"
                       :key="item.dictValue"
                       :label="item.dictName"
                       :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <br>
        <el-form-item label="注册时间">
          <el-date-picker v-model="timeRange"
                          type="daterange"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          value-format="yyyy-MM-dd"
                          :default-time="['00:00:00', '23:59:59']"
                          @change="selectedTime">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <br>
        <el-form-item>
          <el-button v-if="$hasPermission('partymembers:save')"
                     type="primary"
                     @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('partymembers:delete')"
                     type="danger"
                     @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info"
                     @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info"
                     @click="exportMoudleHandle()">导出模板</el-button>
        </el-form-item>
        <el-form-item>
          <el-upload class="upload-demo"
                     ref="upload"
                     :action="uploadUrl"
                     :on-preview="handlePreview"
                     multiple
                     :limit="1"
                     :on-success='uploadSuccess'
                     :on-error='errorExceed'>
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </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="realName"
                         label="姓名"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="mobile"
                         label="手机号"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="identityNo"
                         label="身份证号"
                         header-align="center"
                         align="center"
                         width="180"></el-table-column>
        <el-table-column prop="post"
                         label="职务"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="cadreFlag"
                         label="干部下沉"
                         :formatter="formatCadre"
                         header-align="center"
                         align="center"></el-table-column>
        <!-- <el-table-column prop="tagName"
                         label="角色"
                         header-align="center"
                         align="center"></el-table-column> -->
        <el-table-column prop="partyOrganization"
                         label="所在党委"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="registTime"
                         label="认证时间"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="allDeptNames"
                         label="街道-社区-网格"> </el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('partymembers:update')"
                       type="text"
                       size="small"
                       @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('partymembers:delete')"
                       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 './partymembers-add-or-update'
import Cookies from 'js-cookie'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/app-user/partymembers/page',
        getDataListIsPage: true,
        deleteURL: '/app-user/partymembers',
        deleteIsBatch: true,
        exportURL: '/app-user/partymembers/export',
        exportMoudle: '/app-user/partymembers/exportMoudle'
      },
      dataForm: {
        id: '',
        realName: '',
        mobile: '',
        streetId: '',
        identityNo: '',
        communityId: '',
        gridId: '',
        registFlag: '',
        postId: '',
        startTime: '',
        endTime: ''
      },
      // streetOptions: [],
      communityOptions: [],
      gridOptions: [],
      registOptions: [],
      // postOptions: [],
      timeRange: [],
      upLoadUrl: '',
      faultDataVisible: false,
      errordataList: [],
      options: [],
      deptIdList: []
    }
  },
  components: {
    AddOrUpdate
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 1) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 2) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = ''
      }
      if (val.length === 3) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = this.deptIdList[2]
      }
    }
  },
  created: function () {
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUser`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => { })
    // this.getStreetList()
    this.getRegistOptions()
    // this.getPostOptions()
    this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/app-user/partymembers/importExcel?token=${Cookies.get('token')}`
  },
  methods: {
    selectedTime () {
      this.dataForm.startTime = this.timeRange[0]
      this.dataForm.endTime = this.timeRange[1]
    },
    // getStreetList () {
    //   this.$http.get(`/sys/dept/sublist/` + (localStorage.getItem('street') === null ? '1169154711480528897' : localStorage.getItem('street'))).then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }
    //     this.streetOptions = res.data
    //   }).catch(() => { })
    // },
    getCommunityList () {
      if (this.dataForm.streetId === '') {
        this.communityOptions = []
        this.gridOptions = []
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
        return
      }
      this.dataForm.communityId = ''
      this.dataForm.gridId = ''
      this.$http.get(`/sys/dept/sublist/` + this.dataForm.streetId).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.communityOptions = res.data
      }).catch(() => { })
    },
    getGridList () {
      if (this.dataForm.communityId === '') {
        this.gridOptions = []
        this.dataForm.gridId = ''
        return
      }
      this.dataForm.gridId = ''
      this.$http.get(`/sys/dept/sublist/` + this.dataForm.communityId).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.gridOptions = res.data
      }).catch(() => { })
    },
    getRegistOptions () {
      this.$http.get(`/sys/dict/sublist/1168769645576179713`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.registOptions = res.data
      }).catch(() => { })
    },
    // getPostOptions () {
    //   this.$http.get(`/sys/dict/sublist/1168770417290366977`).then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }
    //     this.postOptions = res.data
    //   }).catch(() => { })
    // },
    formatCadre: function (row, column) {
      return row.cadreFlag === '1' ? '是' : row.cadreFlag === '0' ? '否' : '未知'
    },
    formatRegist: function (row, column) {
      return row.registFlag === '1' ? '是' : row.registFlag === '0' ? '否' : '未知'
    },
    importData () {
    },
    // 获取数据列表
    getDataList () {
      if (this.timeRange === null) {
        this.dataForm.startTime = ''
        this.dataForm.endTime = ''
      }
      this.dataListLoading = true
      this.$http.get(
        this.mixinViewModuleOptions.getDataListURL,
        {
          params: {
            order: this.order,
            orderField: this.orderField,
            page: this.mixinViewModuleOptions.getDataListIsPage ? this.page : null,
            limit: this.mixinViewModuleOptions.getDataListIsPage ? this.limit : null,
            ...this.dataForm
          }
        }
      ).then(({ data: res }) => {
        this.dataListLoading = false
        if (res.code !== 0) {
          this.dataList = []
          this.total = 0
          return this.$message.error(res.msg)
        }
        this.dataList = this.mixinViewModuleOptions.getDataListIsPage ? res.data.list : res.data
        this.total = this.mixinViewModuleOptions.getDataListIsPage ? res.data.total : 0
      }).catch(() => {
        this.dataListLoading = false
      })
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    errorExceed (file, fileList) {
      this.$message.error('上传失败请重试')
    },
    uploadSuccess (response, file, fileList) {
      this.$refs.upload.clearFiles()
      console.log(response)
      if (response.data !== null && response.data.length > 0) {
        this.faultDataVisible = true
        this.errordataList = response.data
        console.log(this.faultDataVisible)
        return
      }
      this.$message({
        message: this.$t('prompt.success'),
        type: 'success',
        duration: 500,
        onClose: () => {
          this.getDataList()
        }
      })
    }
  }
}
</script>
