<template>
	<view class="wrap">
		<view class="top">
			<view class="top-list">
				<view>
					<text class="title">建议描述</text>
					<text class="red">*</text>
				</view>
				<view style="margin-top: 20rpx;">
					<u--textarea v-model="commentText" placeholder="请描述您的建议,我们会尽快处理" count maxlength='400' height='200'>
						
					</u--textarea>
				</view>
				
			</view>
			<view class="top-list" style="padding-bottom: 24rpx;">
				<view>
					<text class="title">相关图片</text>
					<text class="grey">（最多二张）</text>
				</view>
				<view style="margin-top: 20rpx;">
					<u-upload
						:fileList="fileList1"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="2"
						width='160'
						height='160'
					></u-upload>
				</view>
			</view>
			<view class="top-list" style="border-bottom:none;">
				<view class="title">
					<text>联系手机</text>
				</view>
				<view class="phone">
					<input class="uni-input" type="number" placeholder="请输入您的手机号码" v-model="phone" />
				</view>
			</view>
		</view>
		
		<view class="bottom">
			<text class="full-btn" @click="commit()">提交</text>
		</view>
	</view>
</template>

<script>
export default{
  data(){
    return{
      active:0,//默认下标
      merchantId:'',//商户Id
      openid:uni.getStorageSync('openid')||'',//openid
      commentText:'',//建议内容
      phone:'',//电话号码
      fileList1: [],//文件列表
      imgList:[],//上传的图片列表
    }	
  },
  onLoad(option) {
    this.merchantId=option.merchantId||''
  },
  methods:{
    //---删除图片
    deletePic(event) {
      this[`fileList${event.name}`].splice(event.index, 1)
    },
    //---新增图片
    async afterRead(event) {
      // 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
      let lists = [].concat(event.file)
      if(event.file[0].size>1024*1024*10){
			  uni.showToast({
				  title:'图片内存不能大于10M',
				  icon: 'none'
			  })
			  return
      }
      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)
        let item = this[`fileList${event.name}`][fileListLen]
        this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
          status: 'success',
          message: '',
          url:'http://picture.pangdly.com/'+result
        }))
        fileListLen++
      }
			
    },
    uploadFilePromise(url) {
      return new Promise((resolve, reject) => {
        let baseUrl=''
        if(process.env.NODE_ENV === 'development'){//开发环境
          baseUrl='http://192.168.0.213:7888/'
        }else{
          baseUrl= 'https://manager.pangdly.com/'
        }
        let fun = uni.uploadFile({
          url: baseUrl+'upload/file/newUpload', // 仅为示例，非真实的接口地址
          filePath:url,
          name: 'uploadFile',
          success: (res) => {
            let res1=JSON.parse(res.data)
            if(res1.code == '00'){
              resolve(res1.data)
            }
          }
        })
      })
    },
    //---提交
    commit(){
      this.imgList=this.fileList1.map(item=>{
        return {
          imgUrl:item.url,
          imgCommentType:0//0游客发起图片 1客服回复图片
        }
      })
      if(!this.commentText){
			  uni.showToast({
				  title: '请输入建议内容',
				  icon: 'none'
			  })
			  return
      }
		  let data={
		    merchantId:this.merchantId,//商户Id
		    openid:this.openid,//openid
        phone:this.phone,//电话号码
        submitType:1,//0-投诉 1-建议
        commentText:this.commentText,//建议内容
        handleStatus:0,//处理状态 0未处理1已处理（默认填0）
        commentImgList:this.imgList,//图片列表
		  }
		  this.$request('comment/userComment/save',data).then((res)=>{
			  if(res.code=='00'){
          // 加上mask,防止停留在页面2秒时间内，游客一直点击影响性能
          uni.showToast({
					  title:'提交成功',
					  icon: 'none',
            mask:true
          })
          setTimeout(()=>{
            uni.navigateBack({
              delta:1 
            })
          },2000)
			  }else{
          // 超过5次，当天不能进行提交，由于是后端直接判断的，加上mask,防止游客一直点击影响性能
			    uni.showToast({
			      title: res.message,
			      icon: 'none',
            mask:true
			    })
			  }
		  })
    }
  }
}
</script>

<style lang="scss" scoped>
	.wrap{
		background: #ffffff;
		min-height: 100%;
	}
	.top{
		padding-left:40rpx;
	}
	.top-list{
		padding: 32rpx 40rpx 40rpx 0;
		border-bottom: 1px solid #f5f5f5;
	}
	.top-list view:first-child{
		display: flex;
		align-items: center;
	}
	.title{
		color: #000000;
	}
	.grey{
		color: #999999;
	}
	.red{
		color: #F53F3F;
	}
	.phone{
		margin-top: 20rpx;
		background: #F7F8FA;
		padding:32rpx;
		border-radius: 8rpx;
	}
	.bottom{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background: #ffffff;
		padding: 20rpx 40rpx 60rpx 40rpx;
		border-top: 1px solid #EDEDED;
		z-index: 1;
	}
	/*修改上传组件样式*/
	.top-list ::v-deep .uicon-camera-fill{
		font-size: 36px !important;
		line-height: 36px !important;
	}
	.top-list ::v-deep .uicon-close{
		font-size: 10px !important;
		line-height: 10px !important;
	}
	.top-list ::v-deep .uicon-checkmark{
		font-size: 12px !important;
		line-height: 12px !important;
	}
	.top-list ::v-deep .u-textarea{
		background:#F7F8FA;
		border: none;
	}
	.top-list ::v-deep .u-upload__button{
		border-radius: 8rpx;
	}
	.top-list ::v-deep .u-textarea__count{
		background: #F7F8FA !important;
	}
</style>