<template>
	<view>
		<!-- 顶部操作条 -->
		<cu-custom :bgColor="'bg-main-color'" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">师傅入驻信息填写</block>
		</cu-custom>
		<!-- 步骤条 -->
		<view class="bg-white padding">
			<view class="cu-steps">
				<view class="cu-item" :class="index>curStep?'':'text-main-color'" v-for="(stepName, index) in stepList">
					<text class='cuIcon-radioboxfill'></text> {{stepName}}
				</view>
			</view>
		</view>
		<!-- 服务区域 -->
		<view v-if="curStep === 0">
			<view class="margin-bottom-with-bar">
				<view class="bg-white margin-top-sm" v-for="(item, index) in servArea">
					<view class="cu-bar padding-lr solid-bottom">
						<view class="text-lg">
							<text class="cuIcon-titles"></text>
							<text>服务区域{{index + 1}}(将按服务范围派单)</text>
						</view>
						<view @click="delServArea(index)">
							<text class="text-main-color">删除</text>
						</view>
					</view>
					<view class="padding">
						<view class="flex">
							<my-uni-combox class="flex-sub margin-right-xs" :candidates="provinceList"
								:showField="'areaName'" placeholder="选择省份" v-model="servArea[index].provinceObj"
								@input="chooseRegion($event, 0, index)"></my-uni-combox>
							<my-uni-combox class="flex-sub margin-right-xs" :candidates="servArea[index].cityList"
								:showField="'areaName'" placeholder="选择城市" v-model="servArea[index].cityObj"
								@input="chooseRegion($event, 1, index)"></my-uni-combox>
							<my-uni-combox class="flex-sub" :candidates="servArea[index].districtList"
								:showField="'areaName'" placeholder="选择地区" v-model="servArea[index].districtObj"
								@input="chooseRegion($event, 2, index)"></my-uni-combox>
						</view>
						<checkbox-group @change="checkStreet($event, index)" class="grid col-2 margin-top-xs">
							<view v-for="(item0, index0) in servArea[index].streetList" class="margin-tb-xs">
								<checkbox style="transform:scale(1)" class="main-color margin-right-xs"
									:value="item0.areaCode"
									:checked="servArea[index].streetIds.indexOf(String(item0.areaCode)) !== -1 ? true : false">
								</checkbox>
								<text>{{item0.areaName}}</text>
							</view>
						</checkbox-group>
					</view>
				</view>
				<view class="text-center margin-top">
					<button class="cu-btn bg-main-color light" @click="addServArea">
						<text class="margin-right-xs"><text class="cuIcon-add"></text></text>
						<text>继续添加服务区域</text>
					</button>
				</view>
			</view>
			<!-- 下一步 -->
			<view class="cu-bar tabbar border shop fixed-bottom-bar bg-back">
				<button class="bg-main-color long-btn margin-lr" @click="nextStep">下一步</button>
			</view>
		</view>
		<!-- 服务技能 -->
		<view v-if="curStep === 1" class="margin-top-sm">
			<view class="margin-bottom-with-bar">
				<view class="bg-white margin-top-sm" v-for="(item, index) in servSkill">
					<view class="cu-bar padding-lr solid-bottom">
						<view class="text-lg">
							<text class="cuIcon-titles"></text>
							<text>服务技能{{index + 1}}(将按服务技能派单)</text>
						</view>
						<view @click="delServSkill(index)">
							<text class="text-main-color">删除</text>
						</view>
					</view>
					<view class="padding">
						<view class="flex">
							<my-uni-combox class="flex-sub margin-right-xs" :candidates="typeList"
								:showField="'goodsCategoryName'" placeholder="请选择" v-model="servSkill[index].typeObj"
								@input="chooseType($event, 0, index)"></my-uni-combox>
							<my-uni-combox class="flex-sub" :candidates="servSkill[index].subTypeList"
								:showField="'goodsCategoryName'" placeholder="请选择" v-model="servSkill[index].subTypeObj"
								@input="chooseType($event, 1, index)"></my-uni-combox>
						</view>
						<checkbox-group @change="checkGoodType($event, index)" class="grid col-3 margin-top-xs">
							<view v-for="(item0, index0) in servSkill[index].subSubTypeList" class="margin-tb-xs">
								<checkbox style="transform:scale(1)" class="main-color margin-right-xs"
									:value="item0.goodsCategoryId"
									:checked="servSkill[index].subSubTypeIds.indexOf(String(item0.goodsCategoryId)) !== -1 ? true : false">
								</checkbox>
								<text>{{item0.goodsCategoryName}}</text>
							</view>
						</checkbox-group>
						<view>
							<input type="text" class="radius-input" placeholder="输入其他服务，用“，”隔开。"
								v-model="servSkill[index].otherServSkill">
						</view>
					</view>
				</view>
				<view class="text-center margin-top">
					<button class="cu-btn bg-main-color light" @click="addServSkill">
						<text class="margin-right-xs"><text class="cuIcon-add"></text></text>
						<text>继续添加服务技能</text>
					</button>
				</view>
			</view>
			<!-- 下一步 -->
			<view class="cu-bar tabbar border shop fixed-bottom-bar bg-back">
				<button class="bg-white long-btn margin-lr" @click="preStep">上一步</button>
				<button class="bg-main-color long-btn margin-lr" @click="nextStep">下一步</button>
			</view>
		</view>
		<view v-if="curStep === 2" class="margin-top-sm">
			<view class="margin-bottom-with-bar">
				<view class="bg-white margin-top-sm" v-for="(item, index) in specialSkill">
					<view class="cu-bar padding-lr solid-bottom">
						<view class="text-lg">
							<text class="cuIcon-titles"></text>
							<text>特殊技能{{index + 1}}(特殊工种如空调安装等请上传)</text>
						</view>
						<view @click="delSpecialSkill(index)">
							<text class="text-main-color">删除</text>
						</view>
					</view>
					<view class="padding">
						<view class="flex align-center">
							<text>特殊技能：</text>
							<my-uni-combox class="flex-sub margin-right-xs" :candidates="specialTypeList"
								:showField="'goodsCategoryName'" placeholder="请选择"
								v-model="specialSkill[index].specialTypeObj" @input="chooseSpecialSkill($event, 0, index)">
							</my-uni-combox>
							<my-uni-combox class="flex-sub margin-right-xs"
								:candidates="specialSkill[index].specialSubTypeList" :showField="'goodsCategoryName'"
								placeholder="请选择" v-model="specialSkill[index].specialSubTypeObj"
								@input="chooseSpecialSkill($event, 1, index)"></my-uni-combox>
							<my-uni-combox class="flex-sub" :candidates="specialSkill[index].specialSubSubTypeList"
								:showField="'goodsCategoryName'" placeholder="请选择"
								v-model="specialSkill[index].specialSubSubTypeObj"
								@input="chooseSpecialSkill($event, 2, index)"></my-uni-combox>
						</view>
						<view class="flex justify-start align-center margin-top-sm">
							<view>技能证书：</view>
							<view class="grid col-1 upload-pic-grid grid-square flex-sub">
								<view class="bg-img" v-for="(url, index0) in specialSkill[index].skillCert" :key="index0"
									@tap="viewImage($event, specialSkill[index].skillCert)" :data-url="url">
									<image :src="url" mode="aspectFill"></image>
									<view class="cu-tag bg-red" @tap.stop="delImg($event, specialSkill[index].skillCert)" :data-index="index0">
										<text class='cuIcon-close'></text>
									</view>
								</view>
								<view class="solids" @tap="chooseImage($event, specialSkill[index].skillCert)" v-if="specialSkill[index].skillCert.length < 2">
									<text class='cuIcon-cameraadd'></text>
								</view>
							</view>
						</view>
						<view class="flex justify-start align-center margin-top-sm">
							<view>保险证明：</view>
							<view class="grid col-1 upload-pic-grid grid-square flex-sub">
								<view class="bg-img" v-for="(url, index0) in specialSkill[index].insurCert" :key="index0"
									@tap="viewImage($event, specialSkill[index].insurCert)" :data-url="url">
									<image :src="url" mode="aspectFill"></image>
									<view class="cu-tag bg-red" @tap.stop="delImg($event, specialSkill[index].insurCert)" :data-index="index0">
										<text class='cuIcon-close'></text>
									</view>
								</view>
								<view class="solids" @tap="chooseImage($event, specialSkill[index].insurCert)" v-if="specialSkill[index].insurCert.length < 2">
									<text class='cuIcon-cameraadd'></text>
								</view>
							</view>
						</view>
						<view class="flex justify-start align-center">
							<view>保险日期：</view>
							<uni-datetime-picker style="width: 75%;"
								:value="[specialSkill[index].insurStartDate, specialSkill[index].insurEndDate]"
								type="datetimerange" rangeSeparator="~" @change="changeInsurDate($event, index)" />
						</view>
					</view>
				</view>
				<view class="text-center margin-top">
					<button class="cu-btn bg-main-color light" @click="addSpecialSkill">
						<text class="margin-right-xs"><text class="cuIcon-add"></text></text>
						<text>继续添加特殊技能</text>
					</button>
				</view>
			</view>
			<!-- 下一步 -->
			<view class="cu-bar tabbar border shop fixed-bottom-bar bg-back">
				<button class="bg-white long-btn margin-lr" @click="preStep">上一步</button>
				<button class="bg-main-color long-btn margin-lr" @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import myUniCombox from '@/components/uni-combox/my-uni-combox.vue';

	export default {
		components: {
			myUniCombox
		},
		onLoad() {
			this.loadData();
		},
		data() {
			return {
				curStep: 0,
				stepList: ['服务范围', '服务技能', '特殊技能'],
				provinceList: [],
				servArea: [{
					cityList: [],
					districtList: [],
					streetList: [],
					provinceObj: {},
					cityObj: {},
					districtObj: {},
					streetIds: []
				}],
				typeList: [],
				servSkill: [{
					subTypeList: [],
					subSubTypeList: [],
					typeObj: {},
					subTypeObj: {},
					subSubTypeIds: [],
					otherServSkill: null
				}],
				specialTypeList: [],
				specialSkill: [{
					specialSubTypeList: [],
					specialSubSubTypeList: [],
					specialTypeObj: {},
					specialSubTypeObj: {},
					specialSubSubTypeObj: {},
					skillCert: [],
					insurCert: [],
					insurStartDate: '',
					insurEndDate: ''
				}],
			}
		},
		methods: {
			loadData() {
				this.loadProvinceList();
				this.loadTypeList();
				this.loadSpecialSkill();
			},
			/* 服务区域 start */
			async loadProvinceList() {
				let res = await this.$request.areaListByStep();
				this.provinceList = res.data;
			},
			async chooseRegion(e, type, index) {
				let res = await this.$request.areaListByStep({
					parentCode: e.areaCode
				});
				if (res.code == 0) {
					switch (type) {
						case 0: {
							this.servArea[index].cityList = res.data;
							this.servArea[index].districtList = [];
							this.servArea[index].streetList = [];
							this.servArea[index].cityObj = {};
							this.servArea[index].districtObj = {};
							this.servArea[index].streetIds = [];
						}
						break;
					case 1: {
						this.servArea[index].districtList = res.data;
						this.servArea[index].streetList = [];
						this.servArea[index].districtObj = {};
						this.servArea[index].streetIds = [];
					}
					break;
					case 2: {
						this.servArea[index].streetList = res.data;
						this.servArea[index].streetIds = [];
					}
					break;
					default:
						break;
					}
				}
			},
			checkStreet(e, index) {
				let checkedIndexArr = e.detail.value;
				this.servArea[index].streetIds = checkedIndexArr;
			},
			addServArea() {
				this.servArea.push({
					cityList: [],
					districtList: [],
					streetList: [],
					provinceObj: {},
					cityObj: {},
					districtObj: {},
					streetIds: []
				});
			},
			delServArea(index) {
				this.servArea.splice(index, 1);
			},
			/* 服务区域 end */
			/* 服务技能 start */
			async loadTypeList() {
				let res = await this.$request.listByStep();
				this.typeList = res.data;
			},
			async chooseType(e, type, index) {
				let res = await this.$request.listByStep({
					goodsCategoryId: e.goodsCategoryId
				});
				if (res.code == 0) {
					switch (type) {
						case 0: {
							this.servSkill[index].subTypeList = res.data;
							this.servSkill[index].subSubTypeList = [];
							this.servSkill[index].subTypeObj = {};
							this.servSkill[index].subSubTypeIds = [];
						}
						break;
					case 1: {
						this.servSkill[index].subSubTypeList = res.data;
						this.servSkill[index].subSubTypeIds = [];
					}
					break;
					default:
						break;
					}
				}
			},
			checkGoodType(e, index) {
				let checkedIndexArr = e.detail.value;
				this.servSkill[index].subSubTypeIds = checkedIndexArr;
			},
			addServSkill() {
				this.servSkill.push({
					subTypeList: [],
					subSubTypeList: [],
					typeObj: {},
					subTypeObj: {},
					subSubTypeIds: [],
					otherServSkill: null
				});
			},
			delServSkill(index) {
				this.servSkill.splice(index, 1);
			},
			/* 服务技能 end */
			/* 特殊技能 start */
			async loadSpecialSkill() {
				let res = await this.$request.listByStep();
				this.specialTypeList = res.data;
			},
			async chooseSpecialSkill(e, type, index) {
				let res = await this.$request.listByStep({
					goodsCategoryId: e.goodsCategoryId
				});
				if (res.code == 0) {
					switch (type) {
						case 0: {
							this.specialSkill[index].specialSubTypeList = res.data;
							this.specialSkill[index].specialSubSubTypeList = [];
							this.specialSkill[index].specialSubTypeObj = {};
							this.specialSkill[index].specialSubSubTypeObj = {};
						}
						break;
						case 1: {
							this.specialSkill[index].specialSubSubTypeList = res.data;
							this.specialSkill[index].specialSubSubTypeObj = {};
						}
						break;
						default:
							break;
					}
				}
			},
			addSpecialSkill() {
				this.specialSkill.push({
					specialSubTypeList: [],
					specialSubSubTypeList: [],
					specialTypeObj: {},
					specialSubTypeObj: {},
					specialSubSubTypeObj: {},
					skillCert: [],
					insurCert: [],
					insurStartDate: '',
					insurEndDate: ''
				});
			},
			delSpecialSkill(index) {
				this.specialSkill.splice(index, 1);
			},
			changeInsurDate(e, index) {
				this.specialSkill[index].insurStartDate = e[0];
				this.specialSkill[index].insurEndDate = e[1];
			},
			async chooseImage(e, imgList) {
				uni.chooseImage({
					count: 2, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						// 上传图片
						this.$request.uploadFile(res.tempFilePaths[0]).then((uploadRes) => {
							// 成功后才存入缓存
							if (uploadRes.code === 0) {
								imgList.push(uploadRes.url);
							}
						});
					}
				});
			},
			viewImage(e, imgList) {
				uni.previewImage({
					urls: imgList,
					current: e.currentTarget.dataset.url
				});
			},
			delImg(e, imgList) {
				uni.showModal({
					title: '',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			/* 特殊技能 end */
			nextStep() {
				this.curStep = this.curStep === this.stepList.length - 1 ? this.curStep : ++this.curStep;
				console.log(this.servSkill)
			},
			preStep() {
				this.curStep = this.curStep === 0 ? 0 : --this.curStep;
				console.log(this.servSkill)
			},
			submit() {
				console.log(this.specialSkill)
			}
		},
	}
</script>

<style scoped>
	/deep/ .uni-combox__selector {
		z-index: 99 !important;
	}

	.grid.col-1.grid-square.upload-pic-grid>view {
		padding-bottom: 35%;
		height: 0;
		margin-right: 20rpx;
	}

	.grid.col-1.upload-pic-grid>view {
		width: 35%;
	}

	/deep/ .uni-date__x-input {
		height: 36px;
	}
</style>
