<template>
<view class="bg">
        <view v-if="showType == '1'" class="scrollview">
            <u--form :rules="rules" labelPosition="left" :model="form"  ref="uForm">
                <u-form-item prop="username" :labelWidth="labelWidth" label="用户名"  borderBottom >
                    <u--input maxlength="12" placeholder="请输入用户名" v-model="form.username" border="none" ></u--input>
                </u-form-item>
                <!-- 
                <u-form-item prop="address" :labelWidth="labelWidth" label="签名"  borderBottom >
                    <u--input placeholder="签名" v-model="form.address" border="none" ></u--input>
                </u-form-item> -->
                <u-form-item  :labelWidth="labelWidth" label="头像"  borderBottom >
                    <u-upload
                        :fileList="fileList1"
                        @afterRead="afterRead"
                        @delete="deletePic"
                        name="1"
                        multiple
                        :maxCount="1"
                    ></u-upload>
                </u-form-item>
            </u--form>
        </view>

        <view v-if="showType == 'fangkui'" class="scrollview">
            <u--form :rules="rules" labelPosition="left" :model="form"  ref="uForm">
                <u-form-item prop="fangkuiyijian" :labelWidth="labelWidth" label="反馈意见"  borderBottom >
                    <u--textarea  v-model="form.fangkuiyijian" placeholder="请输入反馈意见" 
                    count  maxlength="200" ></u--textarea>
                </u-form-item>
                <u-form-item prop="phone" :labelWidth="labelWidth" label="手机号" borderBottom >
                    <u--input placeholder="请输入手机号" v-model="form.phone" border="none" ></u--input>
                </u-form-item>
            </u--form>
        </view>
        <view class="btnbox" style="left: 24rpx;">
            <view @click="submit" class="btn">
                保存
            </view>
        </view>

</view>
</template>

<script>
import {
    modifyInfo,myInfo,feedback
    } from '@/http/api/common.js'
import env from "@/http/config/config.js"
import {toastFunc} from '@/pages/util/util.js'
export default {

data() {
    return {
        form:{
            username:'',
            phone:'',
            address:'',
            address2:'',
            check:false,
            fangkuiyijian:''
        },
        fileList1:[],
        fileList:[],
        file:[],
        labelWidth:100,
        popupShow:false,
        rules: {
            'username': {
                type: 'string',
                required: true,
                message: '请填写用户名',
                trigger: ['blur', 'change']
            },
            'fangkuiyijian': {
                type: 'string',
                required: true,
                message: '请填写反馈意见',
                trigger: ['blur', 'change']
            },
            'phone':[ 
                {
                    type: 'string',
                    required: true,
                    message: '请输入手机号',
                    trigger: ['blur', 'change']
                },
                {
                    validator: (rule, value, callback) => {
                        return uni.$u.test.mobile(value);
                    },
                    message: '手机号码格式不正确',
                    // 触发器可以同时用blur和change
                    trigger: ['blur'],
                }
            ]
        },
        showType:'1',
    }
},
components: {
    // appitem,
},

onLoad(e) {
    if (e?.showType) {
        uni.setNavigationBarTitle({
            title:  "意见反馈"
        })
        this.showType = e?.showType
    }
    if (this.showType == '1') {
        //获取个人信息
        this.getmyInfo()
    }
    
},
onReady() {
    //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    this.$refs.uForm?.setRules(this.rules)
},

methods: {
    async getmyInfo(){
      let res = await myInfo()
      if (res.data.code == 200) {
        this.form.username = res.data.data.nickName

        this.fileList1.push({
            url:res.data.data.image
        })
        
      }
    },
    submit(){
        this.$refs.uForm.validate().then(async res1 => {

            if (this.showType == '1') {
                //修改个人信息
                let images = this.fileList1[0].url
                let res = await modifyInfo({
                    nickname:this.form.username,
                    images:images
                })
                if (res.data.code == 200) {
                    toastFunc('修改成功',()=>{
                        uni.switchTab({
                            url: '/pages/myinfo/myinfo',
                        });
                    })
                }
            }else{
                //意见反馈
                let res = await feedback({
                    content:this.form.fangkuiyijian,
                    contact:this.form.phone
                })
                if (res.data.code == 200) {
                    toastFunc('反馈成功',()=>{
                        uni.switchTab({
                            url: '/pages/myinfo/myinfo',
                        });
                    })
                }
            }
        }).catch(errors => {
            // uni.$u.toast('校验失败')
        })
    },
    // 删除图片
    deletePic(event) {
        this[`fileList${event.name}`].splice(event.index, 1)
    },
    // 新增图片
    async afterRead(event) {
        // 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
        let lists = [].concat(event.file)
        let fileListLen = this[`fileList${event.name}`].length
        lists.map((item) => {
            this[`fileList${event.name}`].push({
                ...item,
                status: 'uploading',
                message: '上传中'
            })
        })
        for (let i = 0; i < lists.length; i++) {
            const result = await this.uploadFilePromise(lists[i])
            console.log('result',result);
            if (result != '') {
                let item = this[`fileList${event.name}`][fileListLen]
                this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
                    status: 'success',
                    message: '',
                    url: result
                }))
                fileListLen++
            }else{
                toastFunc('上传失败，请删除重试')
                this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign({}, {
                    status: 'fail',
                    message: '',
                    url: ''
                }))
            }
        }
    },
    uploadFilePromise(file) {
        return new Promise((resolve, reject) => {
            let a = uni.uploadFile({
                url: env.BASEURL + '/open/upload/uploadFile', // 仅为示例，非真实的接口地址
                filePath: file.url,
                name: 'file',
                success: (res) => {
                    const fileData = JSON.parse(res.data)
                    if (fileData.code == 500) {
                        resolve('')
                    }else{
                        const fileData = JSON.parse(res.data)
                        resolve(fileData.data.fileUrl)
                    }
                }
            });
        })
    },
  

}
}
</script>

<style lang="scss">
.bg{
    width: 750rpx;
    height: auto;
    // min-height: 100vh;
    // background: #F1F1F1;
    border-radius: 0rpx 0rpx 0rpx 0rpx;

    display: flex;
    flex-direction: column;
    align-items: center;
}

.btnbox{
    position: fixed;
    bottom: 20rpx;
}
.btn{
    width: 702rpx;
    height: 80rpx;
    background: #46A6FF;
    border-radius: 64rpx 64rpx 64rpx 64rpx;
    opacity: 1;

    font-size: 28rpx;
    font-weight: 500;
    color: #FFFFFF;
    line-height: 80rpx;
    text-align: center;
}

</style>
