<template>
  <view class="data-container">
    <view class="content">
      <view class="header">
        <u-image
          width="260rpx"
          height="260rpx"
          :src="baseUrl + '/' + globalStore.infoData.FIMAGEFILESERVER"
          border-radius="15"
        ></u-image>
        <text @tap="updateImage">点击更换头像</text>
      </view>

      <u-card
        :show-head="false"
        :show-foot="false"
        margin="0"
        border-radius="30"
        ><template #body>
          <u-cell-group :border="false">
            <u-cell-item title="姓名">
              <template #right-icon>
                <u-input v-model="FNAME" placeholder="请输入姓名" />
              </template>
            </u-cell-item>
            <u-cell-item title="性别" :arrow="false">
              <template #right-icon>
                <u-radio-group v-model="radio">
                  <u-radio
                    @change="radioChange"
                    v-for="(item, index) in radioList"
                    :key="index"
                    :name="item.name"
                  >
                    {{ item.name }}
                  </u-radio>
                </u-radio-group>
              </template>
            </u-cell-item>
            <u-cell-item title="工作单位">
              <template #right-icon>
                <u-input v-model="FWORKUNIT1" placeholder="请输入工作单位" />
              </template>
            </u-cell-item>
            <u-cell-item title="单位职务">
              <template #right-icon>
                <u-input v-model="FDUTIES" placeholder="请输入单位职务" />
              </template>
            </u-cell-item>
            <u-cell-item
              title="手机号码"
              :value="hidePhoneNumber(globalStore.infoData.FMOBILE)"
              :border-bottom="false"
              :arrow="false"
            >
              <template #right-icon>
                <text
                  style="margin-left: 20rpx; color: #2979ff"
                  @tap="updateMode"
                  >修改</text
                >
              </template>
            </u-cell-item>
          </u-cell-group>
        </template>
      </u-card>
    </view>
    <view style="margin-bottom: 120rpx">
      <u-button type="primary" @tap="submit">保存</u-button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { baseUrl } from "@/utils/request";
import { useStudentStore } from "@/stores/modules/studentStore";
import { register } from "@/api/login";
import { toast, zconfirm, hidePhoneNumber } from "@/utils/util";

const globalStore = useStudentStore();

const radio = ref();
const radioList = [
  {
    name: "男",
  },
  {
    name: "女",
  },
];

const FWORKUNIT1 = ref(globalStore.infoData.FWORKUNIT1);
const FDUTIES = ref(globalStore.infoData.FDUTIES);
const FNAME = ref(globalStore.infoData.FNAME);

const updateMode = () => {
  uni.navigateTo({
    url: "/pages/login/index?type=1",
  });
};

const updateImage = () => {
  uni.chooseImage({
    count: 1,
    success(res: any) {
      uni.uploadFile({
        url:
          baseUrl +
          "/LQKJ.K3.PeiXunSystem.WebApi.WebApiEditService.UserImage,LQKJ.K3.PeiXunSystem.WebApi.common.kdsvc",
        filePath: res.tempFilePaths[0],
        fileType: "image",
        name: "image",
        formData: {
          studentId: globalStore.infoData.FSTUDENTID,
        },
        success: ({ data }) => {
          const uploadFileRes = JSON.parse(data);
          if (uploadFileRes.code == 200) {
            globalStore.setInfoData({
              ...globalStore.infoData,
              ...{ FIMAGEFILESERVER: uploadFileRes.data },
            });
          }
        },
      });
    },
  });
};

const radioChange = (e: string) => {};

const submit = () => {
  zconfirm("确认修改信息?", async (result: boolean) => {
    if (result) {
      const state = {
        type: 0,
        parmeters: {
          NeedUpDateFields: [
            "FName",
            "FSex",
            "FMobile",
            "FDuties",
            "FRemark",
            "FOPENID",
            "FWorkUnit",
          ],
          IsDeleteEntry: "false",
          Model: {
            FName: FNAME.value, //姓名
            FSex: radio.value == "男" ? "0" : "1", //性别0男1女
            FMobile: globalStore.infoData.FMOBILE, //手机
            FDuties: FDUTIES.value, //职务
            FOPENID: globalStore.openId, //openid
            FWorkUnit: FWORKUNIT1.value, //工作单位
            FRemark: "", //备注
            FSTUDENTID: globalStore.infoData.FSTUDENTID,
          },
        },
      };
      const { data: res }: any = await register(state);
      if (res.code == 200) {
        globalStore.setInfoData(res.data[0]);
        toast("保存成功");
      }
    }
  });
};

onShow(() => {
  radio.value = globalStore.infoData.FSEX == "1" ? "女" : "男";
});
</script>

<style lang="scss" scoped>
.data-container {
  width: 100%;
  height: calc(100vh - 45px);
  padding: 30rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 50rpx;
    text {
      margin-top: 20rpx;
    }
  }

  .u-card {
    width: 100%;
  }
}

:deep(.uni-input-input) {
  color: #909399;
  text-align: end;
}
</style>
