<template>
	<view class="serviceCategory">
		<view class="checkbox">
			<u-checkbox-group @change="checkSelect">
				<u-checkbox name="全选" v-model="selectAll" @change="checkedAll">{{selectAllText}}</u-checkbox>
				<u-checkbox
				v-for="(item,key) in checkList"
				:key="key"
				:name="item.name"
				v-model="item.checked"
				@change="checkChange"
				>
					{{item.name}}
				</u-checkbox>
			</u-checkbox-group>
		</view>
		<view class="order-skill">
			<view style="margin: 5% 0;">
				<text style="color:#000000">其他技能</text>
			</view>
			<u-input type="textarea" placeholder="如:修电脑 修手机等" :border="true" v-model="form.Model.FOtherSkills"></u-input>
		</view>
		<view class="tips">
			<view>
				<text>注意：其他服务技能请</text><text style="color: red;">用空格或逗号隔开</text>
			</view>
			<view>
				<text>例如：修电脑 修手机，修汽车等</text>
			</view>
		</view>
		<view class="pics">
			<view style="margin: 5% 0;">
				<text style="color:#000000">请上传擅长安装产品完工的照片</text>
			</view>
			<u-upload
			ref="upload"
			:file-list="uploadFileList"
			:action="action"
			:header="header"
			@on-change="uploaded"
			@on-remove="remove"
			></u-upload>
		</view>
		<view class="btn">
			<u-button @click="confirm" :custom-style="btnStyle">确定</u-button>
		</view>
	</view>
</template>

<script>
	import {apiBaseUrl} from '../../api/request.js'
	import {loading,hidden,toast} from '../../tips/myTips.js'
	import {getServiceItem,getFileUrl,uploadpic,editInfo} from '../../api/api.js'
	export default {
		onLoad(){
			this.init()
			uni.$u.mpShare.title = '页面分享';
		},
		data(){
			return {
				btnStyle:{
					background:"#7D5AFC",
					color:'#FFFFFF'
				},
				checkList:[],
				selectAll:false,
				selectAllText:'全选',
				form:{
					IsDeleteEntry:true,
					Model:{
						FID:this.$store.state.userInfo.Id,
						FOtherSkills:'',
						FServiceTypeEntity:[],
						FServiceImagesEntity:[]
					}
				},
				url:'',
				header:{
					test:11
				},
				
				//上传图片列表
				uploadFileList:[]
			}
		},
		methods:{
			init(){
				this.url = apiBaseUrl
				let userInfo = this.$store.state.userInfo
				let that = this
				this.form.Model.FOtherSkills = userInfo.FOtherSkills
				//获取服务类目
				loading()
				getServiceItem().then((res) => {
					hidden()
					if(res.data.code == 0){
						for(let i in res.data.result){
							res.data.result[i].checked = false
							this.checkList.push(res.data.result[i])
						}
						//获取已选择的服务类目
						for(let i in this.checkList){
							for(let j in userInfo.FServiceTypeEntity){
								if(userInfo.FServiceTypeEntity[j].FSERVICEREMARK == this.checkList[i].name){
									this.checkList[i].checked = true
									break
								}
							}
						}
						let count=0
						for(var i =0,len=this.checkList.length;i<len;i++){
							if(this.checkList[i].checked == true){
								count+=1
							}
						}
						if(count==this.checkList.length){
							this.selectAll=true
						}
					}
				})
				//循环获取完工图片
				console.log("服务类目实体类:",userInfo.FServiceImagesEntity)
				if(userInfo.FServiceImagesEntity.length !== 0){
					for(let i in userInfo.FServiceImagesEntity){
						loading()
						getFileUrl(userInfo.FServiceImagesEntity[i].FServiceImage).then((res) => {
							hidden()
							if(res.data.code == 0){
								let tempFormImagesEntity = {url:'',FServiceImage:''}
								tempFormImagesEntity.FServiceImage = res.data.result[0].fileid
								tempFormImagesEntity.url = res.data.result[0].fileurl
								that.form.Model.FServiceImagesEntity.push(tempFormImagesEntity)
								that.uploadFileList.push(tempFormImagesEntity)
							}else{
								toast(res.data.message)
							}
						})
					}
				}
				
			},
			checkChange(e){
				
			},
			checkSelect(e){
				if(e.length==this.checkList.length&&this.selectAll==false)
				{
					this.selectAll=true
				}
				else if(this.selectAll==true&&e.length<=this.checkList.length){
					this.selectAll=false
				}
			},
			checkedAll(e){
				if(e.value == true){
					this.checkList.map(val => {
						val.checked = true
					})
				}else{
					this.checkList.map(val => {
						val.checked = false
					})
				}
			},
			uploaded(res, index, lists, name){
				console.log(JSON.parse(res.data))
				if(JSON.parse(res.data).code == 0){
					console.log(lists[index].response.result.fileurl)
					let tempObj = {FServiceImage:'',fileurl:''}
					tempObj.FServiceImage = lists[index].response.result.fileid
					tempObj.fileurl = lists[index].response.result.fileurl
					this.form.Model.FServiceImagesEntity.push(tempObj)
				}
				// if(data.code == 0){
				// 	console.log("上传成功:",data)
				// 	let tempobj = {FServiceImage:'',fileurl:''}
				// 	tempobj.FServiceImage = data.result.fileid
				// 	tempobj.fileurl = data.result.fileurl
				// 	this.form.Model.FServiceImagesEntity.push(tempobj)
				// 	// this.form.Model.FServiceImagesEntity.push(tempobj)
				// }else{
				// 	console.log("上传失败:",data)
				// 	uni.showToast({
				// 		title:data.message,
				// 		icon:'none'
				// 	})
				// 	lists.splice(index,1)
				// }
				
			},
			remove(index, lists, name){
				lists.splice(index,1)
				this.form.Model.FServiceImagesEntity.splice(index,1)
				console.log("删除后:",this.form.Model.FServiceImagesEntity)
			},
			confirm(){
				for(let i in this.checkList){
					let temp = {
						FSERVICEITEM:{
							FNUMBER:''
						},
						FSERVICEREMARK:''
					}
					if(this.checkList[i].checked == true){
						temp.FSERVICEITEM.FNUMBER = this.checkList[i].number
						temp.FSERVICEREMARK = this.checkList[i].name
						this.form.Model.FServiceTypeEntity.push(temp)
					}
				}
				console.log("修改参数:",this.form)
				loading()
				editInfo(this.form).then((res) => {
					console.log(res)
					hidden()
					uni.navigateBack()
				})
			}
		},
		computed:{
			action:function(){
				return this.url + '/api/UploadAttachments'
			}
		}
	}
</script>

<style>
	.serviceCategory{
		padding: 7% 4%;
	}
	.checkbox{
		margin: 5% 0;
	}
	.order-skill{
		margin: 2% 0;
	}
	.tips{
		margin: 2% 0;
	}
	.pics{
		margin: 2% 0;
	}
</style>
