<template>
  <view :data-theme="'theme-' + proxy.$settingStore.themeColor.name" class="info-container">
    <u-sticky class="shadow-default" bgColor="#fff">
      <u-tabs :list="tabsList" :current="tabsCurrent" @click="tabsClick" lineColor="#333" :activeStyle="{ color: '#333' }" :inactiveStyle="{ color: '#909399' }" :scrollable="false"></u-tabs>
    </u-sticky>

    <view class="content-section" v-show="tabsCurrent == 0">
      <view class="menu-list margin-t-0 margin-lr-0">
        <view class="list-cell list-cell-arrow" @click="proxy.$settingStore.handleToAvatar(2)">
          <view class="menu-item-box">
            <view class="title">头像</view>
            <image style="margin: 0 15px 0 auto" v-if="avatar" :src="avatar" class="cu-avatar radius"> </image>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleUserModal('修改昵称')">
          <view class="menu-item-box">
            <view class="title">昵称</view>
            <view class="content" style="margin: 0 15px 0 auto; font-size: 14px; color: #909399">{{ user.nickName }}</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleUserModal('修改手机号')">
          <view class="menu-item-box">
            <view class="title">手机号码</view>
            <view style="margin: 0 15px 0 auto; font-size: 14px; color: #909399">{{ user.phonenumber }}</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleUserModal('修改邮箱')">
          <view class="menu-item-box">
            <view class="title">邮箱</view>
            <view style="margin: 0 15px 0 auto; font-size: 14px; color: #909399">{{ user.email }}</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="handleUserModal('修改性别')">
          <view class="menu-item-box">
            <view class="title">性别</view>
            <view style="margin: 0 15px 0 auto; font-size: 14px; color: #909399">{{ user.sex == "0" ? "男" : "女" }}</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="proxy.$modal.showToast('模块建设中~')">
          <view class="menu-item-box">
            <view class="title">我的名片</view>
          </view>
        </view>
      </view>

      <view class="menu-list margin-t-0 margin-lr-0">
        <view class="list-cell list-cell-arrow" @click="proxy.$modal.showToast('模块建设中~')">
          <view class="menu-item-box">
            <view>个人实名认证</view>
          </view>
        </view>
        <view class="list-cell list-cell-arrow" @click="proxy.$modal.showToast('模块建设中~')">
          <view class="menu-item-box">
            <view>更多</view>
          </view>
        </view>
      </view>
    </view>

    <!-- <view class="user-section" :class="'bg-' + proxy.$settingStore.themeColor.name" :style="{ paddingTop: proxy.$settingStore.barHightTop }">
      <image class="bg" src="@/static/images/mine/user-bg.png"></image>
      <view class="portrait-box">
        <image v-if="!avatar" @click="proxy.$settingStore.handleToAvatar(1)" class="portrait" src="@/static/images/mine/missing-face.png"></image>
        <image v-if="avatar" @click="proxy.$settingStore.handleToAvatar(2)" :src="avatar" class="portrait cu-avatar xl round"> </image>
      </view>
    </view> -->

    <!-- <uni-list>
      <uni-list-item showExtraIcon="true" :extraIcon="{ type: 'person-filled' }" title="昵称" :rightText="user.nickName" />
      <uni-list-item showExtraIcon="true" :extraIcon="{ type: 'phone-filled' }" title="手机号码" :rightText="user.phonenumber" />
      <uni-list-item showExtraIcon="true" :extraIcon="{ type: 'email-filled' }" title="邮箱" :rightText="user.email" />
      <uni-list-item showExtraIcon="true" :extraIcon="{ type: 'auth-filled' }" title="岗位" :rightText="postGroup" />
      <uni-list-item showExtraIcon="true" :extraIcon="{ type: 'staff-filled' }" title="角色" :rightText="roleGroup" />
      <uni-list-item showExtraIcon="true" :extraIcon="{ type: 'calendar-filled' }" title="创建日期" :rightText="user.createTime" />
    </uni-list> -->

    <!-- <view class="example">
      <uni-forms ref="form" :model="user" labelWidth="80px">
        <uni-forms-item label="用户昵称" name="nickName">
          <uni-easyinput v-model="user.nickName" placeholder="请输入昵称" />
        </uni-forms-item>
        <uni-forms-item label="手机号码" name="phonenumber">
          <uni-easyinput v-model="user.phonenumber" placeholder="请输入手机号码" />
        </uni-forms-item>
        <uni-forms-item label="邮箱" name="email">
          <uni-easyinput v-model="user.email" placeholder="请输入邮箱" />
        </uni-forms-item>
        <uni-forms-item label="性别" name="sex" required>
          <uni-data-checkbox v-model="user.sex" :localdata="sexs" />
        </uni-forms-item>
      </uni-forms>
      <button type="primary" class="round text-dfl" @click="submit">修改资料</button>
    </view> -->
  </view>

  <u-modal :show="modalShow" :showConfirmButton="true" :showCancelButton="true" @cancel="modalShow = false" @confirm="handleConfirm">
    <view class="slot-content" style="width: 100%">
      <view class="margin-b-15" style="color: #909399">{{ modalTitle }}</view>

      <view v-if="modalTitle === '修改昵称'">
        <u--input v-model="userArr.nickName" :maxlength="5" :placeholder="modalTitle" border="bottom" clearable></u--input>
      </view>

      <view v-if="modalTitle === '修改手机号'">
        <u--input type="number" v-model="userArr.phonenumber" :maxlength="11" :placeholder="modalTitle" border="bottom" clearable></u--input>
      </view>

      <view v-if="modalTitle === '修改邮箱'">
        <u--input v-model="userArr.email" :placeholder="modalTitle" border="bottom" clearable></u--input>
      </view>

      <view v-if="modalTitle === '修改性别'">
        <u-radio-group v-model="userArr.sex" placement="row">
          <u-radio v-for="se in sexs" :key="se" :activeColor="proxy.$settingStore.themeColor.color" :label="se.text" :name="se.value" style="margin-right: 15px"></u-radio>
        </u-radio-group>
      </view>
    </view>
  </u-modal>
</template>

<script setup>
import config from "@/config";
import storage from "@/utils/storage";
import { onLoad, onShow, onReady, onHide, onLaunch, onNavigationBarButtonTap, onPageScroll } from "@dcloudio/uni-app";
import { ref, reactive, computed, getCurrentInstance, toRefs, inject } from "vue";
import { publicStores, useStores } from "@/store/modules/index";

import { updateUserProfile, getUserProfile } from "@/api/system/user";

const useStore = useStores();

const { proxy } = getCurrentInstance();

const avatar = computed(() => {
  return useStore.$state.avatar;
});

const user = computed(() => {
  return useStore.$state.user;
});

const userArr = computed(() => {
  return useStore.$state.userArr;
});

const postGroup = computed(() => {
  return useStore.$state.postGroup;
});

const roleGroup = computed(() => {
  return useStore.$state.roleGroup;
});

const data = reactive({
  tabsList: [
    {
      name: "个人信息",
    },
    // {
    //   name: "其它信息",
    // },
    {
      name: "企业/团队",
    },
  ],
  tabsCurrent: 0,

  sexs: [
    {
      text: "男",
      value: "0",
    },
    {
      text: "女",
      value: "1",
    },
  ],

  modalShow: false,
  modalTitle: "",
});

const { name, phone, tabsList, tabsCurrent, sexs, modalShow, modalTitle } = toRefs(data);

const form = ref(null);

function handleUserModal(title) {
  modalShow.value = true;
  modalTitle.value = title;
}

function handleConfirm() {
  if (modalTitle.value == "修改昵称") {
    if (!userArr.value.nickName) {
      proxy.$modal.showToast("昵称不能为空");
      return;
    }
  }

  if (modalTitle.value == "修改手机号") {
    if (!userArr.value.phonenumber) {
      proxy.$modal.showToast("手机号码不能为空");
      return;
    }

    if (!/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(userArr.value.phonenumber)) {
      proxy.$modal.showToast("请输入正确的手机号码");
      return;
    }
  }

  if (modalTitle.value == "修改邮箱") {
    if (!userArr.value.email) {
      proxy.$modal.showToast("邮箱不能为空");
      return;
    }

    if (!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(userArr.value.email)) {
      proxy.$modal.showToast("请输入正确的邮箱地址");
      return;
    }
  }

  if (modalTitle.value == "修改性别") {
    if (!userArr.value.sex) {
      proxy.$modal.showToast("请选择性别");
      return;
    }
  }

  updateUserProfile(userArr.value).then((response) => {
    proxy.$tab.reLaunch("/pages/mine");
    useStore.getUser();
  });
}

function submit(ref) {}

/**
 * @tabs点击事件
 */
function tabsClick(e) {
  tabsCurrent.value = e.index;
}

onLoad((options) => {
  useStore.getUser();
});

onReady(() => {
  //   form.value.setRules(rules.value);
});

onShow(() => {
  //调用系统主题颜色
  proxy.$settingStore.systemThemeColor([1]);
});

// 自定义导航事件
onNavigationBarButtonTap((e) => {
  if (e.float == "right") {
    proxy.$tab.navigateTo("/pages/mine/setting/index");
  }
});
</script>

<style lang="scss" scoped>
:deep(.uni-page-head__title) {
  opacity: 1 !important;
}

.info-container {
  .user-section {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 240px;
    padding-top: 44px;
    overflow: hidden;
    position: relative;

    .bg {
      position: absolute;
      left: 0;
      top: 0;
      width: 100vw;
      opacity: 0.84;
    }

    .portrait {
      position: relative;
      width: 104px;
      height: 104px;
      border-radius: 50%;
      border: 3px solid #fff;
    }
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
}
</style>
