<template>
	<view class="container avstarBack">
		<uni-navbar title="修改信息"></uni-navbar>
		<view class="box commonMt relative">
			<view class="absolute">
				<view class="choose-line-bottom commonBj">
					<u--form :model="form" ref="formRef" :rules="rules" labelWidth="0" class="formStyle">
						<u-form-item prop="nickName" ref="form1">
							<view class="redCircle"></view>
							<text class="itemText">姓&emsp;&emsp;名</text>
							<u-input class="inputStyle" maxlength="10" v-model="form.nickName"
								placeholder="请输入账号所属联系人姓名">
							</u-input>
						</u-form-item>
						<u-form-item prop="phonenumber">
							<view class="redCircle"></view>
							<text class="itemText">手&ensp;机&ensp;号</text>
							<u-input class="inputStyle" v-model="form.phonenumber" placeholder="请输入联系人手机号">
							</u-input>
						</u-form-item>
						<u-form-item @tap="showIt">
							<view class="redCircle"></view>
							<text class="itemText">部&emsp;&emsp;门</text>
							<u-input disabled disabledColor="rgba(0,0,0,0)" type="default" suffixIcon="arrow-right"
								suffixIconStyle="color: #495B93" class="inputStyle" v-model="deptName"
								placeholder="请选择所属部门">
							</u-input>
							<!-- </fy-tree-select> -->
						</u-form-item>
					</u--form>
					<tki-tree currentIcon="../../static/images/cityEditInfo/active.png"
						defaultIcon="../../static/images/cityEditInfo/defalut.png" :selectParent="true"
						@confirm="onConfirm" ref="tkitree" :range="list" />
				</view>
				<u-button @click="submit" shape="circle" color="linear-gradient(to top, #F59A02, #FFD334 )"
					class="custom-style mt33 commonMb">确认修改
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import tkiTree from '@/components/tki-tree/tki-tree.vue'
	import {
		cityInfo,
		tree,
		editCity
	} from "@/api/city/city.js"
	export default {
		components: {
			tkiTree
		},
		data() {
			return {
				cumtomClass: {
					// fontSize: '16rpx'
				},
				defaultValue: [],
				list: [{
					deptName: '一级 1',
					deptId: 1,
					children: [{
						deptName: '二级 1',
						deptId: 4,
						children: [{
							deptName: '三级 1',
							deptId: 5
						}]
					}, {
						deptName: '二级 2',
						deptId: 5,
						children: [{
							deptName: '三级 1',
							deptId: 9
						}]
					}]
				}, {
					deptName: '一级 2',
					deptId: 2,
					children: []
				}, {
					deptName: '一级 3',
					deptId: 3,
					children: []
				}, {
					deptName: '一级 4',
					deptId: 99,
					children: []
				}],
				form: {
					companyName: null,
					companyAbbreviation: null,
					companyCode: null,
					legalPerson: null,
					phoneNumber: null,
					deptName: null,
					deptId: null
				},
				rules: {
					nickName: [{
						required: true,
						whitespace:true,
						message: '请输入名称',
						trigger: ['blur', 'change']
					}, ],
					deptName: [{
						required: true,
						message: '请选择部门',
						trigger: ['blur', 'change']
					}, ],
					phonenumber: [{
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					}, {
						// 自定义验证函数
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							return uni.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['blur'],
					}]
				},
				// 城管id
				userId: null,
				userName: null,
				deptName: null,
				roleIds: []
			}
		},
		methods: {
			showIt() {
				console.log(111);
				this.$refs.tkitree._show()
				console.log(this.$refs.tkitree);
			},
			onChange(e) {
				console.log(e);
			},
			onConfirm(e) {
				console.log(e)
				console.log(e[0].label);
				this.form.deptId = e[0].id
				this.form.deptName = e[0].label
				console.log(this.form);
				this.deptName = e[0].label
				// let arr = e.primary.filter((item, index) => {
				// 	return item != undefined
				// })
				// this.form.deptName = arr[arr.length - 1]
				// let array = e.values.filter((item) => {
				// 	return item != undefined
				// })
				// this.form.deptId = array[array.length - 1]
			},
			submit() {
				this.$refs.formRef.validate().then(async res => {
					console.log('form', this.form);
					let obj = {}
					obj.deptId = this.form.deptId
					obj.userId = this.form.userId
					obj.nickName = this.form.nickName
					obj.phonenumber = this.form.phonenumber
					obj.userName = this.form.userName
					console.log('obj', obj);
					const result = await editCity({
						...this.form,
						roleIds: this.roleIds
					})
					console.log('修改成功', result);
					this.$tab.navigateBack()
				}).catch(errors => {
					console.log(errors, 'errors');
				})
			},
			// 获取当前登陆人信息
			async getCityInfo(userId) {
				const res = await cityInfo(userId)
				console.log('信息', res);
				this.form = res.data
				this.roleIds = res.roleIds
				this.deptName = res.data.dept.deptName
			},
			// 获取树
			async getTree() {
				const res = await tree()
				console.log('树', res);
				this.list = res.data[0].children.filter((item, index) => index === 0)
			}
		},
		onLoad() {
			this.userId = this.$store.state.user.userId
			this.getCityInfo(this.userId)
			this.getTree()

		}
	}
</script>

<style scoped lang="scss">
	.avstarBack {
		background-image: url(@/static/images/editInfo/editInfo.png);
	}

	.relative {
		position: relative;
	}

	.absolute {
		position: absolute;
		bottom: 46rpx;
		width: 100%;
	}

	.choose-line-bottom {
		overflow: hidden;
		padding: 49rpx 35rpx 93rpx;
		box-sizing: border-box;

		.small-box-middle {
			position: relative;
			margin-bottom: 27rpx;

			border: 1rpx solid #EBEBEB;
			border-radius: 20rpx;
			padding: 28rpx;
			box-sizing: border-box;

			.small-box-middle-startAndEnd {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				font-weight: bold;

				uni-text {
					margin-left: 22rpx;
				}
			}

			.small-box-middle-line {
				display: flex;
				justify-content: start;
				align-items: center;
				margin-left: 11rpx;

				.small-box-middle-line-left {
					width: 2rpx;
					height: 26rpx;
					background: #eeeeee;
					border-radius: 1rpx;
					margin: 6rpx 7rpx;
				}

				.small-box-middle-line-right {
					width: 439rpx;
					height: 1rpx;
					background: #eeeeee;
					margin-left: 15rpx;
				}
			}
		}
	}


	/deep/.u-form-item__body {
		padding: 26rpx 0;
	}
</style>
