<template>
  <div>
    <div class="selfClass" :style="{height}">
      <el-card style="width:800px;margin:50px auto auto auto">
        <div>
          <el-row>
            <el-col :span="24" class="self-avatar">
              <!--上传图片 start-->
              <div style="width:80px;height:80px;margin: auto;">
                <el-upload
                  :action="action"
                  :on-success="_handelsuccess"
                  :before-upload="_beupload"
                  :data="dataObj"
                  :with-credentials="true"
                  :show-file-list="false"
                  :accept="accept"
                >
                  <el-tooltip content="头像" placement="right">
                    <img :src="loginUser">
                  </el-tooltip>
                </el-upload>
              </div>
            </el-col>
            <el-col :span="24" style="margin-bottom: 10px">
              <Form
                id="edit-personal-form"
                ref="formObj"
                :model="formObj"
                :rules="ruleValidate"
                :label-width="80"
                inline
              >
                <Form-item label="所属部门：" class="width-48">
                  {{ formObj.orgNames }}
                </Form-item>
                <Form-item label="所属角色：" class="width-48">
                  {{ formObj.roleNames }}
                </Form-item>
                <Form-item label="用户名：" class="width-48">
                  <div v-if="isViewUser">
                    <span style="display:inline-block;width: 65%">
                      {{ formObj.username }}
                    </span>
                    <span class="edit-btn blue-btn" @click="_isViewUserChange">
                      更改
                    </span>
                  </div>
                  <div v-else>
                    <el-input v-model="editUsername" name="username" placeholder="请输入用户名" style="width: 65%" />
                    <span class="edit-btn blue-btn" @click="_save">
                      确定
                    </span>
                    <span class="edit-btn red-btn" @click="_cancel">
                      取消
                    </span>
                  </div>
                </Form-item>
                <Form-item label="真实姓名：" class="width-48">
                  <a class="blue-color" @click="_viewSign(formObj.id)">
                    {{ formObj.realname }}
                  </a>
                  <a class="blue-color" style="margin-left: 100px" @click="_sign(formObj.id)">
                    电子签名
                  </a>
                </Form-item>
                <Form-item label="性别：" class="width-48">
                  <div v-if="isSex">
                    <span
                      style="display:inline-block;width: 65%"
                    >
                      {{ formObj.sex === undefined?'/':(formObj.sex === 0?'男' : '女') }}
                    </span>
                    <span class="edit-btn blue-btn" @click="isSex = false">
                      更改
                    </span>
                  </div>
                  <div v-else>
                    <span style="display:inline-block;width: 65%">
                      <el-select
                        clearable
                        size="small"
                        style="width: 100%"
                        @change="_sexChange"
                      >
                        <el-option
                          v-for="item in sexList"
                          :key="item.value"
                          :label="item.name"
                          :value="item.value"
                        />
                      </el-select>
                    </span>
                    <span class="edit-btn blue-btn" @click="isSex = true">
                      取消
                    </span>
                  </div>
                </Form-item>
                <input name="sex" type="hidden">
                <Form-item label="手机号码：" prop="mobile" class="width-48">
                  <span style="display:inline-block;width: 60%">
                    {{ formObj.mobile }}
                  </span>
                  <span class="edit-btn blue-btn" @click="_edit('手机号码')">
                    更改
                  </span>
                  <span v-show="formObj.mobile">
                    <span v-if="formObj.verifyMobile" class="edit-btn blue-btn btn-disabled">
                      已验证
                    </span>
                    <span v-else class="edit-btn blue-btn" @click="_validate('手机号码')">
                      验证
                    </span>
                  </span>
                </Form-item>
                <Form-item label="邮箱：" prop="email" class="width-48">
                  <span style="display:inline-block;width: 60%">
                    {{ formObj.email }}
                  </span>
                  <span class="edit-btn blue-btn" @click="_edit('邮箱')">
                    更改
                  </span>
                  <span v-show="formObj.email">
                    <span v-if="formObj.verifyEmail" class="edit-btn blue-btn btn-disabled">
                      已验证
                    </span>
                    <span v-else class="edit-btn blue-btn" @click="_validate('邮箱')">
                      验证
                    </span>
                  </span>
                </Form-item>
                <Form-item label="QQ：" class="width-48">
                  <div v-if="isQQ">
                    <span style="display:inline-block;width: 65%">
                      {{ formObj.qq }}
                    </span>
                    <span class="edit-btn blue-btn" @click="_isQQChange">
                      更改
                    </span>
                  </div>
                  <div v-else>
                    <el-input v-model="qqVal" name="qq" placeholder="请输入您的QQ" style="width: 65%" />
                    <span class="edit-btn blue-btn" @click="_saveQQ">
                      确定
                    </span>
                    <span class="edit-btn red-btn" @click="isQQ = true">
                      取消
                    </span>
                  </div>
                </Form-item>
              </Form>
            </el-col>
          </el-row>
        </div>
      </el-card>
    </div>
    <validateMobile ref="mobileModal" @on-result-change="_getUserInfo" />
    <validateEmail ref="emailModal" @on-result-change="_getUserInfo" />
    <!--图片预览-->
    <Modal v-model="visible" title="电子签名">
      <div style="margin-top: 20px;text-align: center;border: 1px solid #eee;padding: 10px">
        <img :src="imgUrl" alt="电子签名" style="width:120px;height:60px">
      </div>
    </Modal>
    <!--电子签名-->
    <signature ref="signatureModal" />
  </div>
</template>

<script>
/**
 * 个人信息
 */
import { sysUser } from '../../../api'
import global from '../../../api/config'
import signature from '../../sys-config/sys-user/signature'
import validateEmail from './validateEmail'
import validateMobile from './validateMobile'

export default {
  components: {
    validateEmail,
    validateMobile,
    signature
  },
  data() {
    return {
      formObj: {
        orgNames: '',
        roleNames: '',
        username: '',
        mobile: '',
        email: '',
        verifyMobile: '',
        verifyEmail: '',
        sex: ''
      },
      action: global.baseURL + '/base/v1/user/avatar',
      dataObj: {
        file: ''
      },
      loginUser: global.staticURL + '/img/loginUser.jpg',
      showEditModal: false,
      ruleValidate: {},
      isViewUser: true,
      editUsername: '',
      visible: false,
      imgUrl: '',
      isSex: true,
      isQQ: true,
      sexList: [{ name: '男', value: 0 }, { name: '女', value: 1 }],
      qqVal: '',
      accept: '.jpg,.jpeg,.png,.JPEG,.PNG,.JPG'
    }
  },
  computed: {
    height: function() {
      return this.$tableHeight('', 120) + 'px'
    }
  },
  mounted() {
    this._getUserInfo()
  },
  methods: {
    // 修改性别
    _sexChange(data) {
      $('input[name=sex]').val(data)
      this._save()
      this.isSex = true
    },
    _isQQChange() {
      this.qqVal = this.formObj.qq
      this.isQQ = false
    },
    // 保存QQ
    _saveQQ() {
      this._save()
      this.isQQ = true
    },
    // 预览电子签名
    _viewSign(id) {
      this.visible = true
      this.imgUrl =
        global.baseURL +
        '/v1/file_signature/download/' +
        id +
        '?' +
        Math.random()
    },
    // 电子签名
    _sign(id) {
      this.$refs.signatureModal._open(id)
    },
    // 修改用户名
    _isViewUserChange() {
      this.editUsername = this.formObj.username
      this.isViewUser = false
    },
    // 获取用户资料
    _getUserInfo: async function() {
      const result = await sysUser.getUserInfo()
      this.formObj = result
      global.setUserInfo(result)
      if (this.formObj.avatar === undefined) {
        this.loginUser = global.staticURL + '/img/loginUser.jpg'
      } else {
        this.loginUser =
          global.baseURL + '/base/v1/company/logo?key=' + this.formObj.avatar
      }
    },
    _beupload(file) {
      const type = !!(file.type === 'image/png' || file.type === 'image/jpeg')
      if (!type) {
        this.$message.error('上传头像图片只能是 jpg或png 格式!')
      }
      this.dataObj.file = file.name
      return type
    },
    // 上传成功
    _handelsuccess(response, file, fileList) {
      if (response.success) {
        this.$message.success('上传成功！')
        this._getUserInfo()
        this.$emit('on-result-change')
      } else {
        this.$message.error(response.msg)
      }
    },
    _save: async function() {
      const data = this.$serialize('edit-personal-form')
      const result = await sysUser.edit({ id: this.formObj.id, obj: data })
      if (result) {
        this.$message.success('编辑成功')
        this._cancel()
        this._getUserInfo()
      }
    },
    _cancel() {
      this.isViewUser = true
    },
    _edit(name) {
      switch (name) {
        case '用户名':
          this._cancel()
          this.editUsername = this.formObj.username
          this.isViewUser = false
          break
        case '手机号码':
          this.$refs.mobileModal._open({ isEdit: true })
          break
        case '邮箱':
          this.$refs.emailModal._open({ isEdit: true, id: this.formObj.id })
          break
      }
    },
    _validate(name) {
      switch (name) {
        case '手机号码':
          this.$refs.mobileModal._open({ mobile: this.formObj.mobile })
          break
        case '邮箱':
          this.$refs.emailModal._open({ email: this.formObj.email })
          break
      }
    }
  }
}
</script>

<style scoped>
.selfClass {
  background: #eee;
  padding: 20px;
}

.self-avatar {
  margin-bottom: 20px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
  text-align: center;
}

.self-avatar img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.self-avatar img:hover {
  cursor: pointer;
}

.edit-btn {
  margin-left: 10px;
}

.blue-btn {
  color: #00a0e9;
}

.blue-btn:hover {
  color: #00b5ec;
  cursor: pointer;
}

.red-btn {
  color: #eb6877;
}

.red-btn:hover {
  color: #ff4c5c;
  cursor: pointer;
}

.btn-disabled {
  cursor: not-allowed !important;
}
</style>
