<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar :user="user" />
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ user.phonenumber }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ user.email }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree" />所属部门
                <div class="pull-right" v-if="user.dept">{{ user.dept.deptName }} / {{ postGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ roleGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
            </ul>
            <div class="text-center" style="margin-top: 20px;" v-if="needWxTemplateMsg">
              <div v-if="user.wechatBindStatus">
                <el-tag type="success">已绑定服务号通知</el-tag>
              </div>
              <div v-else>
                <el-button type="primary" @click="openWechatBindDialog">
                  订阅服务号通知
                </el-button>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd :user="user" />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      title="订阅服务号通知"
      :visible.sync="bindDialogVisible"
      width="360px"
      :close-on-click-modal="false"
    >
      <div class="text-center">
        <img v-if="wechatQrcode" :src="wechatQrcode" style="width: 260px; height: 260px;" />
        <p style="margin-top: 10px;">请使用微信扫码绑定服务号通知</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="bindDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import userAvatar from "./userAvatar";
import userInfo from "./userInfo";
import resetPwd from "./resetPwd";
import { getUserProfile } from "@/api/company/companyUser";
import { getWechatBindQrcode, checkWechatBindStatus } from "@/api/wechat";
export default {
  name: "Profile",
  components: { userAvatar, userInfo, resetPwd },
  data() {
    return {
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      bindDialogVisible: false,
      wechatQrcode: "",
      wechatBindTimer: null,
      needWxTemplateMsg: false,
    };
  },
  created() {
    if (process.env.VUE_APP_COMPANY_NAME ==="河北红德堂医药连锁有限公司保定第五十七分公司"){
      this.needWxTemplateMsg = true
    }
    this.getUser();
  },
  methods: {
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.roleGroup = response.roleGroup;
        this.postGroup = response.postGroup;
      });
    },
    openWechatBindDialog() {
      getWechatBindQrcode().then(res => {
        console.log(res)
        this.wechatQrcode = res.data.qrcodeUrl;
        this.bindDialogVisible = true;
        this.startCheckWechatBind(res.data.sceneId);
      });
    },
    startCheckWechatBind(sceneId) {
      // 清除旧定时器
      if (this.wechatBindTimer) clearInterval(this.wechatBindTimer);

      this.wechatBindTimer = setInterval(() => {
        checkWechatBindStatus(sceneId).then(res => {
          if (res.data === true) {
            this.$message.success("绑定成功！");
            clearInterval(this.wechatBindTimer);
            this.bindDialogVisible = false;
            this.getUser(); // 刷新绑定状态
          }
        });
      }, 2000);
    }
  },
  beforeDestroy() {
    if (this.wechatBindTimer) clearInterval(this.wechatBindTimer);
  }
};
</script>
