<template>
  <div class="app-container personal">
    <el-row :gutter="24" type="flex" justify="center">
      <el-col :md="24">
        <h1 class="title">基本资料</h1>
        <div class="clearfix">
          <el-col :md="4">
            <img :src="!avatar ? '/static/img/user.png' : avatar" class="avatar" alt="头像">
            <div>
              <el-tooltip class="item" effect="dark" content="修改头像" placement="top-start">
                <el-button type="text" class="btn_avat" @click="toggleShow">修改头像</el-button>
              </el-tooltip>
              <avatar-upload
                v-model="uploadAvatar.show"
                :url="uploadUrl"
                :params="uploadAvatar.params"
                :width="200"
                :height="200"
                :no-rotate="false"
                img-format="png"
                :headers = "uploadData.uploadHeader"
                @crop-success="cropSuccess"
                @crop-upload-success="cropUploadSuccess"/>
            </div>
          </el-col>
          <el-col :md="20">
            <p class="head_con">登录账号：{{ usercode }}</p>
            <p class="head_con">姓名：{{ username }}</p>
            <p class="head_con">用户角色：{{ rolename }}</p>
            <p v-if="seatflag" class="head_con">分机号：{{ extension }}</p>
          </el-col>
        </div>
        <hr class="division">
        <ul class="lists">
          <li class="list_item clearfix">
            <el-col :md="3">
              <label class="item_label">密码修改</label>
            </el-col>
            <el-col :md="21">
              <span class="item_span">***********</span>
              <el-button type="text" class="btn_pass" @click="btn_modifyPass(userid)">修改</el-button>
            </el-col>
          </li>
          <li class="list_item clearfix">
            <el-col :md="3">
              <label class="item_label">手机号码</label>
            </el-col>
            <el-col :md="21">
              <span class="item_span">您已绑定了手机 <b>{{ mobile }}</b></span>
              <!-- <el-tooltip class="item" effect="dark" content="暂未处理" placement="top-start">
                <el-button type="text" class="btn_tel">修改</el-button>
              </el-tooltip> -->
            </el-col>
          </li>
          <!-- <li class="list_item clearfix">
            <el-col :md="3">
              <label class="item_label">转接设置</label>
            </el-col>
            <el-col :md="21">
              <span class="item_span">您已经将转接号码绑设置为：<b class="red">152-3836-8612</b>【当您开启转到手机接听时，电话会转到此号码】</span>
              <el-tooltip class="item" effect="dark" content="暂未处理" placement="top-start">
                <el-button type="text" class="btn_trans">修改</el-button>
              </el-tooltip>
            </el-col>
          </li> -->
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { updateAvatar } from '@/api/systemSetup/roleSetting/userManage'
import modifyPass from './components/modifyPass'
import avatarUpload from 'vue-image-crop-upload'
import { mapGetters } from 'vuex'

export default {
  name: 'PersonalInfo',
  components: {
    'avatar-upload': avatarUpload
  },
  data() {
    return {
      uploadUrl: this.$store.getters.serverConfig.BASE_API + 'Api/Upload',
      uploadData: {
        uploadHeader: {
          Authorization: '',
        },
      },
      uploadAvatar: {
        show: false,
        params: {
          uploadtype: 'avatar'
        },
        imgDataUrl: '' // 头像剪切后生成的 datebase64格式的url
      }

    }
  },
  computed: {
    ...mapGetters([
      'token',
      'userid',
      'avatar',
      'username',
      'usercode',
      'userRole',
      'seatflag',
      'extension',
      'mobile',
      'rolename',
    ])
  },
  created() {
    this.uploadData.uploadHeader.Authorization = this.token
  },

  methods: {
    // 修改头像
    toggleShow() {
      this.uploadAvatar.show = !this.uploadAvatar.show
    },
    cropSuccess(imgDataUrl, field) {
      // this.uploadAvatar.imgDataUrl = imgDataUrl;
    },
    cropUploadSuccess(jsonData, field) {
      console.log("jsonData",jsonData)
      if (jsonData.state.toLowerCase() === 'success') {
        const avatarData = {
          usercode: this.usercode,
          headimg: jsonData.data[0].fileurl, //头像
          // head_img: jsonData.data[0].head_img, // 头像大图
          // head_small_img: jsonData.data[0].head_small_img // 头像缩略图
        }
        updateAvatar(avatarData).then(response => {
          if (response.state.toLowerCase() === 'success') {
            // 更新头像
            this.$store.dispatch('SetAvatar', jsonData.data[0].head_img)
            this.$message.success(`恭喜你，用户(${this.usercode}-${this.username})的头像修改成功!`)
          }
        })
      }
    },
    // 修改密码
    btn_modifyPass(editId) {
      this.$layer.iframe({
        content: {
          content: modifyPass, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': editId, 'ispersonal': true }// props
        },
        area: ['40%', '360px'],
        title: '修改密码'
      })
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.personal{
		height: 100%;
		.title {
			font-size: 18px;
			padding-left: 10px;
			border-left: 2px solid #069BFF;
		}
		.avatar{
			width: 110px;
			height: 110px;
			background-color: #0086B3;
			border-radius: 50%;
		}
		.btn_avat{
			margin-left: 30px;
		}
		.head_con{
			font-size: 12px;
			color: #4c4c4c;
			margin: 18px 0;
		}
		.division{
			background-color: #d9e1eb;
			height: 1px;
			border: none;
		}
		.lists{
			margin: 0;
			padding: 0;
			list-style: none;
			.list_item{
				border-bottom: 1px dashed #d9e1eb;
				line-height: 70px;
				.item_label{
					padding-left: 10px;
					font-size: 12px;
					font-weight: bold;
					color: #333333;
				}
				.item_span{
					font-size: 14px;
				}
				.btn_pass{
					margin-left: 70px;
				}
				.btn_tel{
					margin-left: 44px;
				}
				.btn_trans{
					margin-left: 54px;
				}
				.red{
					color: #ff0000;
				}
			}
		}
	}

	@media only screen and (max-width: 768px) {
		.personal{
			height: auto;
		}
	}

</style>
