<template>
  <div class="userDetail">
    <p>
      <img :src="userData.photo" style="width: 50px;height: 50px;border-radius: 50%"/>     {{userData.userName}}
    </p>

    <el-tabs type="border-card" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="0">

        <el-form ref="userData" :model="userData" status-icon label-position="right" label-width="140px"
                 style='width: 100%; margin-left:20px;'>
          <div style="float:left;height: 400px;width: 50%">
            <el-form-item label="用户姓名：" prop="userName">
              <span>{{ userData.userName }}</span>
            </el-form-item>
            <template v-if="userData.auditStatus == '3' ">
              <el-form-item label="用户角色："  prop="roleType">
                <span>{{ userData.roleType }}</span>
              </el-form-item>
            </template>
            <template v-else>
              <el-form-item label="申请角色："  prop="temRoleType">
                <span>{{ userData.temRoleType }}</span>
              </el-form-item>
            </template>

            <el-form-item label="性别：">
              <el-col :span="11">
                <el-form-item prop="gender">
                  <span>{{ userData.gender }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-tooltip :content="'是否显示: ' + userData.hiddenGender" placement="top">
                  <el-switch style="display: block" v-model="userData.hiddenGender" disabled>
                  </el-switch>
                </el-tooltip>

              </el-col>
            </el-form-item>

            <el-form-item label="邮箱：">
              <el-col :span="11">
                <el-form-item prop="email">
                  <span>{{ userData.email }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-tooltip :content="'是否显示: ' + userData.hiddenEmail" placement="top">
                  <el-switch style="display: block" v-model="userData.hiddenEmail" disabled>
                  </el-switch>
                </el-tooltip>
              </el-col>
            </el-form-item>

            <el-form-item label="注册时间：" prop="createDate">
              <span>{{ userData.createDate }}</span>
            </el-form-item>

            <el-form-item label="最后一次登录时间：" prop="lastLoginTime">
              <span>{{ userData.lastLoginTime }}</span>
            </el-form-item>

          </div>
          <div style="float:right;height: 400px;width: 50%">

          </div>

          <div v-if="userData.auditStatus == 1" style="text-align: center;">
            <el-button type="primary" @click="updateUserData">审核</el-button>
          </div>
        </el-form>

      </el-tab-pane>
      <template v-if="userData.auditStatus == '3' ">
        <el-tab-pane label="我的关注" name="1" >

          <el-table size="small" :data="concernList" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
            <el-table-column align="center" width="100px" label="用户ID" prop="id" sortable>
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="头像" prop="photo">
              <template slot-scope="scope">
                <img :src="scope.row.photo" style="width: 50px;height: 50px;border-radius: 50%"/>
              </template>
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="用户名" prop="user_name">
            </el-table-column>

            <el-table-column align="center" min-width="50px" label="性别" prop="gender">
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="角色" prop="role_type">
            </el-table-column>
          </el-table>

        </el-tab-pane>
        <el-tab-pane label="我的圈子" name="2" >
          <el-table size="small" :data="circleList" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

            <el-table-column align="center" width="100px" label="ID" prop="id" >
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="组织图片" prop="url">
              <template slot-scope="scope">
                <img :src="scope.row.url" style="width: 50px;height: 50px;border-radius: 50%"/>
              </template>
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="组织名称" prop="organize_name">
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="组织说明" prop="organize_memo">
            </el-table-column>

            <el-table-column align="center" min-width="50px" label="组织类型" prop="organize_type">
              <template slot-scope="scope">
                <span v-if="scope.row.organize_type === '1'">地方专委会</span>
                <span v-else-if="scope.row.organize_type === '2'">行业专委会</span>
                <span v-else>兴趣小组</span>
              </template>
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="组织人数" prop="organize_user_count">
            </el-table-column>

          </el-table>

        </el-tab-pane>

        <el-tab-pane label="我的消息" name="3" >
          <el-table size="small" :data="messageList" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

            <el-table-column align="center" width="100px" label="ID" prop="id" sortable>
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="消息类型" prop="message_type">
              <template slot-scope="scope">
                <span v-if="scope.row.message_type === 'system'">系统消息</span>
                <span v-else>用户消息</span>
              </template>
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="发送人" prop="fromUserName">
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="发送人头像" prop="fromPhoto">
              <template slot-scope="scope">
                <span v-if="scope.row.fromPhoto">
                  <img :src="scope.row.fromPhoto" style="width: 50px;height: 50px;border-radius: 50%"/>
                </span>
              </template>
            </el-table-column>

            <el-table-column align="center" min-width="150px" label="内容" prop="content">
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="发送时间" prop="create_time">
            </el-table-column>


            <el-table-column align="center" min-width="100px" label="接收人" prop="fkUserName">
            </el-table-column>

            <el-table-column align="center" min-width="100px" label="接收人头像" prop="fkPhoto">
              <template slot-scope="scope">
                <img :src="scope.row.fkPhoto" style="width: 50px;height: 50px;border-radius: 50%"/>
              </template>
            </el-table-column>


          </el-table>

        </el-tab-pane>
      </template>

      <!-- 分页 -->

      <div v-if="visible">
        <div class="pagination-container">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
                         :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </el-tabs>

    <!-- 审核用户对话框 -->
    <el-dialog title="审核" :visible.sync="dialogFormVisible">
      <el-form ref="listQuery" :model="listQuery" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>

        <el-form-item :rules="rules"  label="审核：" prop="auditStatus">
          <div>
            <el-radio v-model="listQuery.auditStatus" label="3" border>通过</el-radio>
            <el-radio v-model="listQuery.auditStatus" label="2" border>驳回</el-radio>
          </div>
        </el-form-item>
        <template v-if="listQuery.auditStatus=='2'">
          <el-form-item label="驳回原因：" prop="reason">
            <el-input type="textarea"
                      :rows="2"
                      placeholder="请输入内容"
                      v-model="listQuery.reason"></el-input>
          </el-form-item>
        </template>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateData">确定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import { readUser, auditUser, getConcernedList, getMessageUser, getOrganizeList } from '@/api/user'
export default {
  name: 'userDetail',
  data() {
    return {
      id: undefined,
      userData: {
        id: undefined,
        userName: '',
        phoen: '',
        gender: '',
        photo: ''

      },
      concernList: null,
      circleList: null,
      messageList: null,
      dialogFormVisible: false,
      rules: {
        auditStatus: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      total: null,
      listLoading: false,
      queryType: 'baseInfo',
      activeTab: undefined,
      visible: undefined,
      listQuery: {
        id: undefined,
        auditStatus:'',
        reason:'',
        page: 1,
        limit: 20
      }
    }
  },
  watch: {
    '$route'(to, from) {
      var id = this.$route.query.id
      if (id) {
        this.id = id
        this.listQuery.id = id
        this.getList()
        readUser({ 'id': id }).then(response => {
          this.userData = response.data.data.items
          this.$nextTick(() => {
            this.$refs['userData'].clearValidate()
          })
        })
      }
    }
  },
  created() {
    var id = this.$route.query.id
    if (id) {
      this.id = id
      this.listQuery.id = id
      this.getList()
    }
  },
  methods: {
    getList() {
      console.log('---')
      console.log(this.queryType)
      console.log('---')
      if (this.queryType === 'baseInfo') {
        var id = this.id
        readUser({ 'id': id }).then(response => {
          this.userData = response.data.data.items
          this.$nextTick(() => {
            this.$refs['userData'].clearValidate()
          })
        })
      } else if (this.queryType === 'concerned') {
        this.listLoading = true
        getConcernedList(this.listQuery).then(response => {
          this.concernList = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(() => {
          this.concernList = []
          this.total = 0
          this.listLoading = false
        })
      } else if (this.queryType === 'circle') {
        this.listLoading = true
        getOrganizeList(this.listQuery).then(response => {
          this.circleList = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(() => {
          this.circleList = []
          this.total = 0
          this.listLoading = false
        })
      } else if (this.queryType === 'message') {
        this.listLoading = true
        getMessageUser(this.listQuery).then(response => {
          this.messageList = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(() => {
          this.messageList = []
          this.total = 0
          this.listLoading = false
        })
      }
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getWaterDetails() {
      if (this.activeTab === '0') {
        this.visible = false
        this.queryType = 'baseInfo'
        this.getList()
      } else if (this.activeTab === '1') {
        this.visible = true
        this.queryType = 'concerned'
        this.getList()
      } else if (this.activeTab === '2') {
        this.visible = true
        this.queryType = 'circle'
        this.getList()
      } else if (this.activeTab === '3') {
        this.visible = true
        this.queryType = 'message'
        this.getList()
      } else {
        console.log('this.activeTab !!!')
      }
    },
    updateUserData() {
      this.dialogFormVisible = true
    },
    updateData() {
      debugger
      this.$refs['listQuery'].validate((valid) => {
        if (valid) {
          auditUser(this.listQuery).then(response => {
            this.dialogFormVisible = false
            var state = response.data.data.state
            if (state === 'success') {
              this.getList()
              this.$notify({
                title: '认证审核',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })

    },
    //点击选项卡
    handleClick(tab, e) {
      this.activeTab = tab.index
      this.getWaterDetails()
    }
  }
}
</script>

<style scoped>
  .userDetail{
    padding: 20px 30px;
  }
</style>
