<template>
  <div class="es-simplicity-userinfo">
    <div class="es-simplicity-user">
      <div class="es-simplicity-user-name">
        {{ user.username }}
        <el-dropdown
          v-if="user.identityList && user.identityList.length > 1"
          trigger="click"
          placement="bottom-start"
          @command="handleSelect"
        >
          <span class="el-dropdown-link">
            <i class="es-icon-qiehuangangwei"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item
              v-for="item in user.identityList"
              :key="item.id"
              :command="item"
              :class="{ 'is-active': user.identityId == item.id }"
              >{{ item.name }}</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="es-simplicity-org-name">{{ user.orgName }}</div>
      <div class="es-simplicity-dep-post">
        <span class="es-simplicity-dep-name"> {{ user.depName }}</span>
        <template v-if="user.postName">
          |
          <span class="es-simplicity-post-name">{{
            user.position || user.postName
          }}</span>
        </template>
      </div>

      <avatar
        slot="reference"
        upload
        :user="user"
        :code="headImgCode"
        @change="handleChange"
      >
      </avatar>
    </div>
    <div class="es-simplicity-notice-type" v-if="userScanBindImgUrl">
      <div class="es-simplicity-notice-title">微信公众号：</div>
      <el-image
        class="es-simplicity-qrcode"
        @click="getQrcode"
        :src="qrcode"
      ></el-image>
    </div>
    <!-- <div class="es-simplicity-notice-type">
      <div class="es-simplicity-notice-title">接收消息类型：</div>
      <es-checkbox-group
        class="es-simplicity-notice-list"
        v-model="checked"
        :min="1"
        :data="notifyList"
        @change="handleCheckboxChange"
      ></es-checkbox-group>
    </div> -->
    <div class="es-simplicity-password" @click="handleOpen">
      资料修改 <i class="es-icon-you"></i>
    </div>
    <es-dialog title="资料修改" :visible.sync="visible">
      <user-info
        v-if="visible"
        :contents="contents"
        :value="userInfos"
        @success="handleSuccess"
      >
      </user-info>
    </es-dialog>
  </div>
</template>

<script>
import avatar from './avatar.vue';
import userInfo from './userinfo.vue';
import util from 'eoss-ui/src/utils/util';
import { debounce } from 'throttle-debounce';
import {
  initUserSet,
  switchUserTo,
  updateUserCustomInfo
} from 'eoss-ui/src/config/api.js';
export default {
  name: 'User',
  components: { avatar, userInfo },
  props: {
    notify: String,
    notifyList: Array,
    user: Object,
    contents: Array
  },
  data() {
    return {
      userInfos: {},
      userScanBindImgUrl: null,
      qrcode: null,
      checked: [],
      visible: false,
      checkPassword: null,
      checkPasswordMsg: null,
      headImgCode: undefined
    };
  },
  computed: {},
  watch: {
    notify: {
      immediate: true,
      handler(val) {
        if (val) {
          this.checked = val.split(',');
        }
      }
    }
  },
  beforeCreate() {
    this.setNoticeType = debounce(1000, () => {
      this.setNotice();
    });
  },
  created() {},
  mounted() {
    this.getData();
  },
  methods: {
    //获取用户详细信息
    getData() {
      util
        .ajax({
          url: initUserSet
        })
        .then((res) => {
          if (res.rCode === 0) {
            const results = JSON.parse(JSON.stringify(res.results));
            this.userInfos = results.simpleUserInfo;
            if (results.userScanBindImgUrl) {
              this.userScanBindImgUrl = results.userScanBindImgUrl;
              this.qrcode = this.userScanBindImgUrl;
            }
            this.checkPassword = results.checkPassword;
            this.checkPasswordMsg = results.checkPasswordMsg;
            this.headImgCode = results.headImgCode || undefined;
            this.$emit('success', results);
          } else {
            let msg = res.msg || '系统错误，请联系管理员！';
            this.$message.error(msg);
          }
        })
        .catch((err) => {
          if (err.message && err.message !== 'canceled') {
            this.$message.error(err.message);
          }
        });
    },
    //刷新公众号二维码图片
    getQrcode() {
      this.qrcode =
        this.userScanBindImgUrl +
        '&_tt=' +
        Math.random().toString().split('.')[1];
    },
    //修改头像
    handleChange(res) {
      this.$emit('change', 'userHeadUrl', res);
    },
    //设置通知类型
    setNotice() {
      let notify = this.checked.join(',');
      util
        .ajax({
          method: 'post',
          url: updateUserCustomInfo,
          data: {
            notify: notify
          }
        })
        .then((results) => {
          if (results.rCode === 0) {
            this.$message.success(results.msg);
            this.$emit('change', 'notify', notify);
          } else {
            this.$message.error(results.msg || '系统错误，请联系管理员！');
          }
        })
        .catch((err) => {
          if (err.message && err.message !== 'canceled') {
            this.$message.error(err.message);
          }
        });
    },
    //改变通知类型回调
    handleCheckboxChange() {
      this.setNoticeType(this.checked);
    },
    //切换职务
    handleSelect(res) {
      if (this.user.identityId !== res.id) {
        util
          .ajax({
            method: 'post',
            url: switchUserTo,
            data: { userId: res.value }
          })
          .then((res) => {
            if (res.rCode == 0) {
              this.$emit('close');
              util.setStorage({
                type: this.storage,
                key: 'userId',
                value: res.value
              });
              util.removeStorage(['mainConfig', 'jump']);
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 500,
                onClose: () => {
                  if (
                    util.win.location.href.indexOf('#/main') > -1 ||
                    util.win.location.href.indexOf('/main.html') > -1
                  ) {
                    location.reload();
                  } else {
                    this.$router.replace('/main');
                  }
                }
              });
            } else {
              this.$message({
                message: res.msg || '系统错误，请联系管理员！',
                type: 'error'
              });
            }
          })
          .catch((err) => {
            if (err.message && err.message !== 'canceled') {
              this.$message.error(err.message);
            }
          });
      }
    },
    //打开修改密码弹窗
    handleOpen() {
      this.visible = true;
    },
    handleSuccess() {
      this.visible = false;
      this.getData();
    }
  }
};
</script>

<style lang="scss" scoped></style>
