<template>
    <view class="user-info-wrap">
        <view class="nav-bar">
            <view class="nav-content">
                <view class="nav-info">
                    <view class="nav-desc">
                        <view class="nav-name">
                            <view class="">头像</view>
                        </view>
                    </view>
                    <view @click="uploadImg1(1)" class="arrow-right">
                        <view class="head-img">
                            <image v-if="userInfo.facePhotoUrl" :src="userInfo.facePhotoUrl" mode=""></image>
                            <image v-else src="/static/me/u1796.png" mode=""></image>
                        </view>
                        <!-- <u-icon name="arrow-right" color="#666" size="26"></u-icon> -->
                    </view>
                </view>
            </view>
            <view class="custom-line"></view>
            <view class="cotent-from">
                <u--form
                        labelPosition="left"
                        :model="userInfo"
                        :rules="rules"
                        ref="form1"
                >
                    <u-form-item
                            label="昵称"
                            prop="nickName"
                            labelWidth="auto"
                            labelAlign="right"
                    >
                        <u--input
                                v-model="userInfo.nickName"
                                placeholder=""
                                border="bottom"
                                maxlength="20"
                                type="text"
                        ></u--input>
                    </u-form-item>
                    <u-form-item
                            labelWidth="auto"
                            labelAlign="right"
                            label="关系"
                            prop="blood"
                            width="120"
                    >
                        <u--input
                                maxlength="20"
                                v-model="userInfo.blood"
                                placeholder=""
                                border="bottom"
                                type="text"
                        ></u--input>
                    </u-form-item>
                    <u-form-item
                            labelWidth="auto"
                            labelAlign="right"
                            label="备注"
                            prop="remark"
                            width="120"
                    >
                        <u--input
                                maxlength="20"
                                v-model="userInfo.remark"
                                placeholder=""
                                border="bottom"
                                type="text"
                        ></u--input>
                    </u-form-item>
                </u--form>
            </view>
        </view>

        <view @click="saveUserInfo()" class="sure-btn">
            <view class="btn">确认</view>
        </view>

    </view>
</template>

<script>

    export default {
        data() {
            return {
                userInfo: {
                    auth: true,
                    nickName: '',
                    facePhoto: '',
                    blood: '',
                    remark: '',
                    id: ''
                },
                genderShow: false,
                value1: '2023-12-12',
                imgUrl1: ''
            };
        },
        mounted() {
        },
        onLoad(option) {

            console.log('++++++++++++++++option+++++++++++++++',option)

            if(option.data){
                const data = JSON.parse(option.data);
                let url = data.facePhoto;
                this.userInfo.facePhoto = url.startsWith("https") ? url :url.replace("http", "https");
                this.userInfo = data;
            }

            // 用户信息
            let user = uni.getStorageInfo('userInfo');

        },
        methods: {

            // 保存用户信息
            saveUserInfo(){
                if(!this.userInfo.blood){
                    uni.showToast({
                        title: "请填写关系"
                    })
                    return;
                }
                if(!this.userInfo.remark){
                    uni.showToast({
                        title: "请填写备注"
                    })
                    return;
                }
                this.userInfo.auth = true;

                if(this.userInfo.opType == '1'){ // 绑定
                    // 保存数据
                    this.$api.bindServiceObject(this.userInfo).then((res)=>{
                        // 去服务对象列表页
                        uni.navigateTo({
                            url: '/myPages/ServiceObjectManagement/index',
                        })
                    }).catch(() =>{
                        uni.showToast({
                            title: "操作失败"
                        })
                    });
                }else if(this.userInfo.opType == '2'){ // 修改
                    // 修改数据
                    this.$api.updateServiceObject(this.userInfo).then((res)=>{
                        // 去服务对象列表页
                        uni.navigateTo({
                            url: '/myPages/ServiceObjectManagement/index',
                        })
                    }).catch(() =>{
                        uni.showToast({
                            title: "操作失败"
                        })
                    });
                }
            },
            //头像上传
            uploadImg1(imgIndex) {
                let that = this;
                uni.chooseImage({
                    count: 1,
                    success: (chooseImageRes) => {
                        const tempFilePaths = chooseImageRes.tempFilePaths;

                        uni.uploadFile({
                            url: that.$baseUrl  + '/resource/oss/upload', //仅为示例，非真实的接口地址
                            filePath: tempFilePaths[0],
                            name: 'file',
                            header: {
                                'Authorization': 'Bearer ' + uni.getStorageInfo('accessToken'),
                            },
                            success: (uploadFileRes) => {
                                let res = JSON.parse(uploadFileRes.data)
                                that.imgUrl1 = res.data.url;
                                that.userInfo.facePhoto = res.data.ossId
                            }
                        });
                    }
                });
            }
        }
    };
</script>

<style lang="scss">
    .user-info-wrap{
        margin-left: 12px;
        margin-right: 12px;
        /* 导航菜单 */
        .nav-bar{
            background: #fff;
            border-radius: 10px;
            margin-top: 12px;
            .cotent-from{
                padding-right: 12px;
                padding-left: 12px;
            }
            /* 商店信息 */
            .nav-content{
                padding-right: 6px;
                padding-top: 10px;
                padding-bottom: 6px;
                .nav-info{
                    display: flex;
                    padding-left: 10px;
                    align-items: center;
                    .logo {
                        width: 12%;
                        text-align: center;
                        image{
                            width: 24px;
                            height: 24px;
                        }
                    }
                    .nav-desc{
                        width: 46%;
                        display: flex;
                        line-height: 30px;
                        font-family: PingFangSC-Regular, PingFang SC;
                        font-weight: 400;
                        align-items: center;
                        .custom-input{
                            font-size: 12px;
                        }
                        .nav-name{
                            width: 60%;
                            font-size: 14px;

                            color: #333333;
                            line-height: 22px;
                        }
                        .change-store{
                            text-align: right;
                            width: 40%;
                            font-size: 12px;
                            color: #333333;
                            line-height: 22px;
                        }
                    }

                    .arrow-right{
                        width:64%;
                        text-align: right;
                        margin: 0 auto;
                        cursor: pointer;
                        padding-top: 3px;
                        display: flex;
                        justify-content: flex-end;
                        align-items: center;
                        image{
                            width: 18px;
                            height: 18px;
                        }
                        .head-img {
                            text-align: center;
                            padding-right: 20px;
                            image{
                                width: 24px;
                                height: 24px;
                            }
                        }
                        .change-store{
                            font-size: 14px;
                            color: #999;
                            line-height: 22px;
                        }
                    }
                }
            }
        }

        .sure-btn{
            margin-top: 20px;
            width: 100%;
            .btn{
                text-align: center;
                height: 42px;
                background: #FFE05C;
                border-radius: 27px;
                color: #333333;
                line-height: 42px;
                margin-top: 12px;
                font-size: 14px;
            }
        }
    }

</style>
