<template>
    <view class="bg">
        <!-- 提问 -->
        <view  class="commodity">
            <view class="rateBox">
                描述问题
            </view>
            <view class="pjBox">
                <u--textarea  v-model="value1" placeholder="输入你想提问的问题..." 
                count  maxlength="200" ></u--textarea>
            </view>
            
            <view class="upload">
                <u-upload
                    :fileList="fileList1"
                    @afterRead="afterRead"
                    @delete="deletePic"
                    name="1"
                    multiple
                    :maxCount="3"
                ></u-upload>
            </view>
        </view>
        <view style="height: 200rpx;"></view>
        <view class="btnBox">
            <view class="btn" @click="submit">
                提交
            </view>
        </view>

    </view>
</template>
<script>
import env from "@/http/config/config.js"
import { toastFunc } from "@/pages/util/util";
import {createQuestion} from '@/http/api/common.js'
export default {
components: {
},
data() {
    return {
        fileList1:[],
        value1:'',
    }
},
onLoad() {

},
methods: {
    linktoPublishComments(){
        uni.navigateTo({
             url: '/pages/market/PublishComments'
        });
    },
    // 删除图片
    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].url)
            console.log('1111',result);
            let item = this[`fileList${event.name}`][fileListLen]
            this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
                status: 'success',
                message: '',
                url: result
            }))
            fileListLen++
        }
    },
    uploadFilePromise(url) {
        return new Promise((resolve, reject) => {
            let a = uni.uploadFile({
                url: env.BASEURL + '/open/upload/uploadFile',
                filePath: url,
                name: 'file',
                formData: {
                    user: 'test'
                },
                success: (res) => {
                    const fileData = JSON.parse(res.data)
                    if (fileData.code == 500) {
                        resolve('')
                    }else{
                        const fileData = JSON.parse(res.data)
                        resolve(fileData.data)
                    }
                }
            });
        })
    },
    async submit(){
        if (this.value1 == '') {
            toastFunc('请填写问题')
            return
        }
        let uploadList = this.fileList1.map(item=>{
            return item.url
        })
        let res = await createQuestion({
            content:this.value1,
            uploadList:uploadList
        })
        if (res.data.code == 200) {
            toastFunc('提问成功',()=>{
                uni.navigateBack({
                     delta: 1
                });
            })
        }
    },
}
}
</script>

<style>
.bg{
    width: 750rpx;
    height: auto;
    min-height: calc(100vh - 100rpx) ;
    background: #F1F1F1;
    border-radius: 0rpx 0rpx 0rpx 0rpx;

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

.commodity{
    width: 702rpx;
    height: auto;
    /* min-height: 954rpx; */
    background: #FFFFFF;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    opacity: 1;
    margin-top: 24rpx;
    /* padding-bottom: 100rpx; */
}

.commodity_title{
    width: 650rpx;
    height: 40rpx;
    font-size: 28rpx;
    font-weight: 500;
    color: #999;
    line-height: 40rpx;
    padding: 24rpx 0 0 24rpx;

    display: flex;
    justify-content: space-between;
    align-items: center;
}



.mc_item{
	width: 702rpx;
	height: 244rpx;
	background: #FFFFFF;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	opacity: 1;
	margin-top: 24rpx;
}
.item_swipe{
	width: 678rpx;
	height: 244rpx;
	background: #FFFFFF;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	opacity: 1;
	display: flex;
    align-items: center;
    flex-direction: row;
	padding-left: 24rpx;
}

.box_img{
    width: 180rpx;
    height: 180rpx;
    background-color: #999999;
}
.box_text{
	width: 392rpx;
	height: 200rpx;
	margin-left: 20rpx;

	display: flex;
    flex-direction: column;
    justify-content: space-between;
}
.box_title{
	width: 392rpx;
	height: 40rpx;
	font-size: 28rpx;
	font-weight: bold;
	color: #333333;
	line-height: 40rpx;

	display: -webkit-box;
    word-break: break-all;
    text-overflow: ellipsis;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp:1;
}
.box_type{
	width: 410rpx;
	height: 48rpx;
	background: #EEEEEE;
	border-radius: 8rpx 8rpx 8rpx 8rpx;
	opacity: 1;

	font-size: 28rpx;
	font-weight: 500;
	color: #999999;
    padding-left: 24rpx;
    line-height: 48rpx;

	display: -webkit-box;
    word-break: break-all;
    text-overflow: ellipsis;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp:1;
}
.box_num{
	width: 410rpx;
	display: flex;
	justify-content: space-between;
    align-items: center;
}
.rateBox{
    display: flex;
    margin: 24rpx;
}
.pjBox{
    margin-left: 24rpx;

    width: 654rpx;
    /* height: 320rpx; */
    background: #F1F1F1;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    opacity: 1;
}
.btnBox{
    width: 750rpx;
    height: 100rpx;
    background: #FFFFFF;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    opacity: 1;

    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    position: fixed;
    bottom: 0;
}
.btn{
    width: 702rpx;
    height: 80rpx;
    background: #46A6FF;
    border-radius: 64rpx 64rpx 64rpx 64rpx;
    opacity: 1;

    font-weight: 500;
    color: #FFFFFF;
    line-height: 80rpx;
    text-align: center;
}
.upload{
    width: 702rpx;
    margin: 48rpx 0 48rpx 24rpx;
}
</style>
    