<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-epdc__user}">
      <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="街道"
                      v-if="false">
          <el-select v-model="dataForm.streetId"
                     clearable
                     placeholder="请选择"
                     @change="getCommunityList">
            <el-option v-for="(item,index) in streetOptions"
                       :key="index"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="社区"
                      v-if="false">
          <el-select v-model="dataForm.communityId"
                     clearable
                     placeholder="请选择"
                     @change="getGridList">
            <el-option v-for="(item,index) in communityOptions"
                       :key="index"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支部"
                      v-if="false">
          <el-select v-model="dataForm.gridId"
                     clearable
                     placeholder="请选择">
            <el-option v-for="(item,index) in gridOptions"
                       :key="index"
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="dataForm.nickname"
                    placeholder="请输入"
                    clearable></el-input>
        </el-form-item>
<!--        <el-form-item label="手机号">-->
<!--          <el-input v-model="dataForm.mobile"-->
<!--                    placeholder="请输入"-->
<!--                    clearable></el-input>-->
<!--        </el-form-item>-->
        <!-- <div class="block"> -->
        <span class="demonstration">注册时间</span>&nbsp;&nbsp;
        <el-date-picker @change='setRegistTime'
                        v-model="time"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"></el-date-picker>&nbsp;
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('epdc:user:export')"
                     type="primary"
                     @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 type="selection"
                         header-align="center"
                         align="center"
                         width="50"></el-table-column>
        <el-table-column label="序号"
                         width="70px" align="center"><template slot-scope="scope">{{scope.$index+1}}</template></el-table-column>
        <el-table-column prop="myGridName"
                         label="所属支部（街道-社区-支部）"
                         header-align="center"
                         align="center"
                         v-if="false"></el-table-column>
        <el-table-column prop="nickname"
                         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="realName"
                         label="真实姓名"
                         header-align="center"
                         align="center"
                         v-if="false"></el-table-column>
        <el-table-column prop="identityNo"
                         label="身份证号"
                         header-align="center"
                         align="center"
                         v-if="false"></el-table-column>
        <el-table-column prop="createdTime"
                         label="加入支部时间"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="state"
                         label="注册状态"
                         :formatter="formatState"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="100">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       @click="userDetailHandle(scope.row.id)">查看</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>
      <user-detail v-if="userDetailVisible"
                   ref="userDetail"
                   @refreshDataList="getDataList"></user-detail>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './user-add-or-update'
import UserDetail from './user-detail'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: 'app-user/user/page',
        getDataListIsPage: true,
        deleteURL: '/app-use/user',
        deleteIsBatch: true,
        exportURL: '/app-user/user/unauthorizedexport'
      },
      dataForm: {
        id: '',
        streetId: '',
        communityId: '',
        gridId: '',
        deptId:'',
        realName: '',
        mobile: '',
        identityNo: '',
        startTime: '',
        endTime: '',
        partyFlag: '0', // 是否是党员(0-否，1-是)
        state: '0',
        nickname: ''
      },
      deptIdList: [],
      options: [],
      userDetailVisible: false,
      streetOptions: [],
      communityOptions: [],
      gridOptions: [],
      time: '',
      tabFlag: '0',
      state: '未认证用户',
      partyFlagOptions: [{ id: '1', name: '是' }, { id: '0', name: '否' }]
    }
  },
  created () {
    // this.getStreetList()
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUserOnlyParty`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => { })
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
        this.dataForm.deptId = ''
      }else
      if (val.length === 1) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
        this.dataForm.deptId = this.deptIdList[0]
      }else
      if (val.length === 2) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = ''
        this.dataForm.deptId = this.deptIdList[1]
      }else
      if (val.length === 3) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = this.deptIdList[2]
        this.dataForm.deptId = this.deptIdList[2]
      }
    }
  },
  methods: {
    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 () {
      this.communityOptions = this.gridOptions = []
      this.dataForm.communityId = this.dataForm.gridId = null
      if (this.dataForm.streetId) {
        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 () {
      this.gridOptions = []
      this.dataForm.gridId = null
      if (this.dataForm.communityId) {
        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(() => { })
      }
    },
    setRegistTime () {
      this.dataForm.startTime = this.time[0]
      this.dataForm.endTime = this.time[1]
    },
    formatState: function (row, column) {
      let state = row.state
      if (state === '1') {
        return '待审核'
      } else if (state === '2') {
        return '审核不通过'
      } else if (state === '3') {
        return '审核通过'
      } else if (state === '0') {
        return '已注册'
      }
    },
    formatPartyFlag: function (row, column) {
      return row.partyFlag === '1' ? '是' : row.state === '0' ? '未知' : '否'
    },
    // 获取数据列表
    getDataList () {
      if (this.time === 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
      })
    },
    userDetailHandle (userId) {
      this.userDetailVisible = true
      this.$nextTick(() => {
        this.$refs.userDetail.dataForm.id = userId
        this.$refs.userDetail.init()
      })
    }
  },
  components: {
    AddOrUpdate,
    UserDetail
  }
}
</script>
