<template>
	<view class="f-straight wrapper">
		<uni-forms ref="form" :modelValue="formData" :rules="rules" validateTrigger="bind" label-width="80px" label-align="right">
			<uni-forms-item label="头像" name="avatar" class="f-straight" required>
        <view style="display: flex;flex-wrap: wrap;">
          <view class="upload-img" v-if="formData?.avatar">
            <uni-icons size="35" type="clear" color="#fe574a" style="position: absolute;right: -15px; top: -15px; z-index: 9" @click="formData.avatar = ''"></uni-icons>
            <image :src="formData?.avatar" mode="contain" style="width: 200rpx;height: 200rpx;" @click="handlePreviewImage"></image>
          </view>
          <view v-else class="upload-file" @click="uploadPhotos">
            <uni-icons type="plusempty" size="50" color="#f1f1f1"></uni-icons>
          </view>
        </view>
			</uni-forms-item>
			<uni-forms-item required label="用户名" name="name">
        <uni-easyinput v-model="formData.name" placeholder="请输入用户名" />
			</uni-forms-item>
      <uni-forms-item label="联系电话" name="phone" clearable required>
        <uni-easyinput v-model="formData.phone" placeholder="请输入电话号码" />
			</uni-forms-item>
      <uni-forms-item label="企业邮箱" name="email" clearable required>
        <uni-easyinput disabled v-model="formData.email" placeholder="请输入您的企业邮箱" />
			</uni-forms-item>
			<uni-forms-item label="所属岗位" name="postName" clearable>
        <uni-easyinput v-model="formData.postName" placeholder="请输入您所任职的岗位名称" />
			</uni-forms-item>
			<view class="f-horizon-center">
				<button type="primary" size="default" class="send-button"  @click="submit">提 交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script setup>
import { ref, unref } from 'vue'
import { userStore } from '@/store/user'
import { uploadFile } from '@/api/file'
import { cloneDeep } from 'lodash-es'
import { emailRequired } from '@/utils/validate'
import { updateStaffInfo } from '@/api/enterprise'

const form = ref()
const useUserStore = userStore()

const formData = ref({})
const getInfo = () => {
  formData.value = cloneDeep(useUserStore?.userInfo) || {
    avatar: '',
    name: '',
    phone: '',
    email: '',
    postName: ''
  }
}
getInfo()

// 图片预览
const handlePreviewImage = () => {
  uni.previewImage({
    current: 0,
    urls: [formData.value.avatar]
  })
}

// 选择头像
const uploadPhotos = () => {
  wx.chooseImage({
    count: 1,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success: function(res){
      const size = res.tempFiles[0]?.size || 0
      if (size >= 31457280) {
        uni.showToast({
          icon: 'none',
          title: '头像上传大小不得超过 20MB !',
          duration: 2000
        })
        return
      }
      const path = res.tempFilePaths[0]
      uploadFile(path, 'img').then(res => {
        formData.value.avatar = res.data
      }).catch(error => {
        uni.showToast({
          icon: 'error',
          title: '图片上传失败!',
          duration: 2000
        })
      })
    }
  })
}

const rules = {
	avatar:{
		rules: [{required: true, errorMessage: '请上传头像' }]
	},
	name:{
		rules: [{required: true, errorMessage: '请输入您的用户名' }]
	},
  email: emailRequired,
  phone: {
		rules: [{required: true, errorMessage: '请输入您的联系电话' }]
	}
}

const submit = async () => {
  const valid = await unref(form).validate()
  if (!valid) return
  await updateStaffInfo(formData.value)

  uni.showToast({ title: '编辑成功', icon: 'success' })
  await useUserStore.getUserInfos()
  getInfo()
  setTimeout(() => {
		uni.navigateBack({
			delta: 1
		})
	}, 1000)
}
</script>

<style lang="less" scoped>

.wrapper{
	padding: 15px;
  padding-top: 30px;
}
.upload-img{
  position: relative;
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
}
.upload-file{
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 10rpx;
}
</style>