<template>
  <div class="app-container">
    <el-row class="container-main" :gutter="20">
      <el-col class="container-left" :xs="24" :sm="24" :md="8" :lg="6" :xl="5">
        <span class="right-top-line" />
        <span class="left-bottom-line" />
        <div class="user-content">
          <h4 class="info-title">个人信息</h4>
          <div class="el-upload">
            <img :src="user.avatarName ? baseApi + '/avatar/' + user.avatarName : Avatar" title="点击上传头像" class="avatar" @click="toggleShow">
            <myUpload
              v-model="show"
              :headers="headers"
              :url="updateAvatarApi"
              @crop-upload-success="cropUploadSuccess"
            />
          </div>
          <ul class="user-info">
            <li><div style="height: 100%"><i class="iconfont icon-dengluzhanghao-fanbai" /> 登录账号<div class="user-right">{{ user.username }}</div></div></li>
            <li><i class="iconfont icon-yonghunicheng-fanbai" /> 用户昵称 <div class="user-right">{{ user.nickName }}</div></li>
            <li><i class="iconfont icon-suoshubumen-fanbai" /> 所属部门 <div class="user-right"> {{ user.dept.name }}</div></li>
            <li><i class="iconfont icon-shoujihaoma-fanbai" /> 手机号码 <div class="user-right">{{ user.phone }}</div></li>
            <li><i class="iconfont icon-yonghuyouxiang-fanbai" /> 用户邮箱 <div class="user-right">{{ user.email }}</div></li>
            <li>
              <i class="iconfont icon-anquanshezhi-fanbai" /> 安全设置
              <div class="user-right">
                <a @click="$refs.pass.dialog = true">修改密码</a>
                <a @click="$refs.email.dialog = true">修改邮箱</a>
              </div>
            </li>
          </ul>
        </div>
      </el-col>
      <el-col class="container-right tab-content" :xs="24" :sm="24" :md="16" :lg="18" :xl="19">
        <span class="right-top-line" />
        <span class="left-bottom-line" />
        <div class="user-right-content">
          <ul class="tab-nav">
            <li :class="{'active-tab-nav': activeIndex == 0}" @click="handleClick(0)">用户资料<i /></li>
            <li :class="{'active-tab-nav': activeIndex == 1}" @click="handleClick(1)">操作日志<i /></li>
            <!-- 最右侧装饰img -->
            <span class="tab-right-img" />
          </ul>
          <div v-if="activeIndex == 0" class="tab-item">
            <el-form ref="form" :model="form" :rules="rules" size="small" label-width="65px">
              <el-form-item label="昵称" prop="nickName">
                <el-input v-model="form.nickName" style="width: 35%" />
                <span style="color:#999;margin-left: 22px;">用户昵称不作为登录使用</span>
              </el-form-item>
              <el-form-item label="手机号" prop="phone">
                <el-input v-model="form.phone" style="width: 35%;" />
                <span style="color: #999;margin-left: 22px;">手机号码不能重复</span>
              </el-form-item>
              <el-form-item label="性别">
                <el-radio-group v-model="form.gender" style="width: 178px">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="">
                <el-button :loading="saveLoading" size="mini" type="primary" @click="doSubmit">保存配置</el-button>
              </el-form-item>
            </el-form>
          </div>
          <!-- 操作日志 -->
          <div v-if="activeIndex == 1" class="tab-item">
            <el-table v-loading="loading" :data="data" style="width: 100%;">
              <el-table-column prop="description" label="行为" />
              <el-table-column prop="requestIp" label="IP" />
              <el-table-column :show-overflow-tooltip="true" prop="address" label="IP来源" />
              <el-table-column prop="browser" label="浏览器" />
              <el-table-column prop="time" label="请求耗时" align="center">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.time <= 300">{{ scope.row.time }}ms</el-tag>
                  <el-tag v-else-if="scope.row.time <= 1000" type="warning">{{ scope.row.time }}ms</el-tag>
                  <el-tag v-else type="danger">{{ scope.row.time }}ms</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="right">
                <template slot="header">
                  <div style="display:inline-block;float: right;cursor: pointer" @click="init">创建日期<i class="el-icon-refresh" style="margin-left: 40px" /></div>
                </template>
                <template slot-scope="scope">
                  <span>{{ scope.row.createTime }}</span>
                </template>
              </el-table-column>
            </el-table>
            <!--分页组件-->
            <el-pagination
              :total="total"
              :current-page="page + 1"
              style="margin-top: 8px;"
              layout="total, prev, pager, next, sizes"
              @size-change="sizeChange"
              @current-change="pageChange"
            />
          </div>
        </div>
      </el-col>
    </el-row>
    <updateEmail ref="email" :email="user.email" />
    <updatePass ref="pass" />
  </div>
</template>

<script>
import myUpload from 'vue-image-crop-upload'
import { mapGetters } from 'vuex'
import updatePass from './center/updatePass'
import updateEmail from './center/updateEmail'
import { getToken } from '@/utils/auth'
import store from '@/store'
import { isvalidPhone } from '@/utils/validate'
import crud from '@/mixins/crud'
import { editUser } from '@/api/system/user'
import Avatar from '@/assets/images/avatar.png'
export default {
  name: 'Center',
  components: { updatePass, updateEmail, myUpload },
  mixins: [crud],
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      show: false,
      Avatar: Avatar,
      activeIndex: 0,
      saveLoading: false,
      headers: {
        'Authorization': getToken()
      },
      form: {},
      rules: {
        nickName: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, trigger: 'blur', validator: validPhone }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'updateAvatarApi',
      'baseApi'
    ])
  },
  created() {
    this.form = { id: this.user.id, nickName: this.user.nickName, gender: this.user.gender, phone: this.user.phone }
    store.dispatch('GetInfo').then(() => {})
  },
  methods: {
    toggleShow() {
      this.show = !this.show
    },
    handleClick(index) {
      this.activeIndex = index
      if (this.activeIndex === 1) {
        this.init()
      }
    },
    beforeInit() {
      this.url = 'api/logs/user'
      return true
    },
    cropUploadSuccess(jsonData, field) {
      store.dispatch('GetInfo').then(() => {})
    },
    doSubmit() {
      if (this.$refs['form']) {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.saveLoading = true
            editUser(this.form).then(() => {
              this.editSuccessNotify()
              store.dispatch('GetInfo').then(() => {})
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .user-content{
    color: #fff;
    .iconfont{
      margin-right: 12px;
    }
  }
  .info-title{
    margin: 0;
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    font-weight: bold;
    color: #359AFC;
    text-align: center;
    letter-spacing: 2px;
    background-color: #02255F;
  }
  .el-upload{
    display: block;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    margin: 40px auto 60px auto;
    overflow: hidden;
    .avatar {
      width: 100%;
      height: 100%;
    }
  }
  .user-info {
    padding: 0 22px;
    list-style: none;
    li{
      border-bottom: 1px dashed #113D72;
      font-size: 14px;
      height: 41px;
      line-height: 40px;
    }
    .user-right {
      float: right;
      a{
        margin-left: 20px;
        color: #3A99FD;
      }
    }
  }

  .container-left,
  .container-right{
    min-height: calc(100vh - 230px);
  }

  .tab-content{
    margin-top: 40px;
    border-top: none;
    &::before{
      display: none;
    }
    .tab-nav{
      display: flex;
      justify-content: flex-start;
      height: 41px;
      line-height: 41px;
      margin: -41px 0 30px -1px;
      font-size: 16px;
      font-weight: bold;
      color: #359AFC;
      border-bottom: 1px solid #113D72;
      li{
        position: relative;
        padding: 0 24px;
        margin: -1px 30px 0 0;
        letter-spacing: 2px;
        border-top: 1px solid #113D72;
        cursor: pointer;
        &::before,
        &::after{
          content: "";
          position: absolute;
          top: 0;
          width: 33px;
          height: 41px;
        }
        &::before{
          left: -15px;
          background: url(~@/assets/images/tab_left.png) no-repeat;
          background-size: contain;
        }
        &::after{
          right: -32px;
          background: url(~@/assets/images/tab_right.png) no-repeat;
          background-size: contain;
        }
        &:first-child{
          border-left: 1px solid #113d72;;
          &::before{
            width: 17px;
            height: 17px;
            left: -1px;
            top: -1px;
            border-left: 1px solid #339cff;
            border-top: 1px solid #339cff;
          }
        }
        &.active-tab-nav{
          color: #fff;
          &::before{
            background: url(~@/assets/images/tab_left_active.png) no-repeat;
            background-size: contain;
          }
          &::after{
            background: url(~@/assets/images/tab_right_active.png) no-repeat;
            background-size: contain;
          }
          i{
            display: block;
            position: absolute;
            bottom: -1px;
            left: 0;
            width: 100%;
            height: 1px;
            border-bottom: 1px solid #021941;
          }
        }
      }
      .tab-right-img{
        display: block;
        width: 93px;
        margin: 4px 0 0 -14px;
        background: url(~@/assets/images/tab_img.png) no-repeat;
        background-size: contain;
      }
    }
    .el-form{
      padding: 0 20px;
      .el-form-item__label{
        color: #339CFF;
      }
      .el-input__inner{
        height: 36px;
        line-height: 36px;
        color: #fff;
        border: 1px solid #339CFF;
        background: transparent;
      }
      .el-radio{
        color: #fff;
      }
      .el-button{
        font-size: 16px;
        padding: 7px 20px;
        color: #fff;
        background: #3A99FD;
      }
    }
  }
  input:-webkit-autofill,
  textarea:-webkit-autofill,
  select:-webkit-autofill {
    -webkit-text-fill-color: #fff !important;
    background-color: transparent;
    transition: background-color 50000s ease-in-out 0s;
  }
  input {
    background-color: transparent;
  }
</style>
