<template>
	<view class="pb100">
		<form @submit="formSubmit" @reset="formReset">
			<view class="form-title">{{ $t('CK_KS_56') }}</view>
			<view class="form-group">
				<!-- 审核状态 -->
				<view class="form-item" v-if="approveStatus != 0">
					<view class="form-label">
						<text class="domation mr10">*</text>
						{{ $t('MN_F_11') }}
					</view>
					<input :maxlength="100" v-model="form.approveStatusVal" class="form-input" type="text" disabled />
				</view>
				<!-- 营业执照名称 -->
				<view class="form-item">
					<view class="form-label">
						<text class="domation mr10">*</text>
						{{ $t('CK_KS_32') }}
					</view>
					<input
						:maxlength="100"
						v-model="form.licenseName"
						class="form-input"
						type="text"
						:disabled="approveStatus != 0 && approveStatus != 4"
						:placeholder="$t('S_C_70')"
					/>
				</view>
				<!-- 统一社会信用代码 -->
				<view class="form-item">
					<view class="form-label">
						<text class="domation mr10">*</text>
						{{ $t('CK_KS_33') }}
					</view>
					<input
						:maxlength="100"
						v-model="form.creditCode"
						class="form-input"
						type="text"
						:disabled="approveStatus != 0 && approveStatus != 4"
						:placeholder="$t('S_C_70')"
					/>
				</view>
				<!-- 法人姓名 -->
				<view class="form-item">
					<view class="form-label">
						<text class="domation mr10">*</text>
						{{ $t('CK_KS_34') }}
					</view>
					<input
						:maxlength="100"
						v-model="form.legalName"
						class="form-input"
						type="text"
						:disabled="approveStatus != 0 && approveStatus != 4"
						:placeholder="$t('S_C_70')"
					/>
				</view>
				<!-- 所在地区 -->
				<view class="form-item">
					<view class="form-label">
						<text class="domation mr10">*</text>
						{{ $t('MY_WAL_37') }}
					</view>
					<input class="form-input" type="text" :placeholder="$t('CK_KS_38')" v-model="selectCity" disabled="true" @click="showMulLinkageThreePicker" />
					<text class="icon iconfont icon-jiantou" v-if="approveStatus == 0 || approveStatus == 4"></text>
				</view>
				<!-- 详细地址 -->
				<view class="form-item">
					<view class="form-label">
						<text class="domation mr10">*</text>
						{{ $t('S_C_27') }}
					</view>
					<textarea
						:maxlength="200"
						v-model="form.licenseAddress"
						auto-height
						class="form-textarea"
						:disabled="approveStatus != 0 && approveStatus != 4"
						placeholder=""
					/>
				</view>
			</view>
			<!-- 营业执照 -->
			<view class="form-group">
				<view class="form-upload">
					<view class="form-upload-name">
						<text class="domation mr10">*</text>
						{{ $t('CK_KS_56') }}
					</view>
					<view class="d-s-c f-w" @click="openUpload('licenseImage')">
						<view>
							<image class="upload-add" v-if="form.licenseImage" :src="form.licenseImage" mode=""></image>
							<image v-else class="upload-add" src="/static/icon/upload-add.png" mode=""></image>
						</view>
					</view>
				</view>
			</view>
			<button v-if="approveStatus == 0 || approveStatus == 4" class="normal-sub-btn" form-type="submit" style="margin-top: 37rpx;">{{ $t('CK_KS_72') }}</button>
		</form>
		<mpvue-city-picker
			v-if="is_load"
			ref="mpvueCityPicker"
			:province="province"
			:city="city"
			:area="area"
			:pickerValueDefault="cityPickerValueDefault"
			@onConfirm="onConfirm"
		></mpvue-city-picker>
		<!--上传图片-->
		<Upload v-if="isUpload" @getImgs="getImgsFunc"></Upload>
	</view>
</template>

<script>
import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue';
import Upload from '@/components/upload/upload.vue';
export default {
	components: {
		mpvueCityPicker,
		Upload
	},
	data() {
		return {
			accountList: [],
			list: [
				{
					cardName: this.$t('ENU_I_T_1'),
					id: 1
				},
				{
					cardName: this.$t('w_0419'),
					id: 2
				}
			],
			gender: 0,
			typeIndex: -1,
			/*是否已发验证码*/
			is_send: false,
			/*发送按钮文本*/
			send_btn_txt: this.$t('w_0055'),
			/*当前秒数*/
			second: 60,
			form: {
				licenseName: '',
				creditCode: '',
				legalName: '',
				approveStatusVal: '',
				licenseProvince: '',
				licenseCity: '',
				licenseCounty: '',
				licenseAddress: ''
			},
			cityPickerValueDefault: [0, 0, 0],
			selectCity: '',
			recProvince: 0,
			recCity: 0,
			recCounty: 0,
			// address: {},
			delta: 1,
			province: [],
			city: [],
			area: [],
			is_load: false,
			/*是否打开上传图片*/
			isUpload: false,
			upload_type: '',
			approveStatus: 0
		};
	},
	onLoad(e) {
		this.getAreaData();
	},
	methods: {
		getData() {
			let self = this;
			self._get('member/api/business/license/detail', {}, res => {
				Object.assign(self.form, res.data);
				self.approveStatus = res.data.approveStatus || 0;
				console.log(self.approveStatus);
				if (self.form.licenseProvince) {
					self.selectCity = self.getAddressName(self.form.licenseProvince, self.form.licenseCity, self.form.licenseCounty, self.cityPickerValueDefault);
				}
			});
		},
		/* 省市区回显 */
		getAddressName(pid, cid, aid, piker) {
			let self = this;
			/* 省市区列表 */
			if (!self.province) {
				return;
			}
			let add = '';
			let citydata = [];
			let areadata = [];
			console.log(self.province);
			self.province.forEach((item, index) => {
				if (item['id'] == pid) {
					add += item['name'];
					citydata = item['children'];
					piker[0] = index;
					citydata.forEach((citem, cindex) => {
						if (citem['id'] == cid) {
							add += citem['name'];
							areadata = citem['children'] ? citem['children'] : [];
							piker[1] = cindex;
							if (areadata) {
								areadata.forEach((aitem, aindex) => {
									if (aitem['id'] == aid) {
										add += aitem['name'];
										piker[2] = aindex;
										return;
									}
								});
							}
						}
					});
				}
			});
			return add;
		},
		/*提交*/
		formSubmit: function(e) {
			let self = this;
			var formdata = self.form;
			// // console.log(formdata);
			if (formdata.licenseName == '') {
				uni.showToast({
					title: self.$t('CK_KS_38') + self.$t('CK_KS_32'),
					duration: 1000,
					icon: 'none'
				});
				return false;
			}
			if (formdata.creditCode == '') {
				uni.showToast({
					title: self.$t('CK_KS_38') + self.$t('CK_KS_33'),
					duration: 1000,
					icon: 'none'
				});
				return false;
			}
			if (formdata.legalName == '') {
				uni.showToast({
					title: self.$t('CK_KS_38') + self.$t('CK_KS_34'),
					duration: 1000,
					icon: 'none'
				});
				return false;
			}
			if (formdata.licenseProvince == '') {
				uni.showToast({
					title: self.$t('CK_KS_38') + self.$t('MY_WAL_37'),
					duration: 1000,
					icon: 'none'
				});
				return false;
			}
			if (formdata.licenseAddress == '') {
				uni.showToast({
					title: self.$t('CK_KS_38') + self.$t('CK_KS_37'),
					duration: 1000,
					icon: 'none'
				});
				return false;
			}

			if (formdata.licenseImage == '') {
				uni.showToast({
					title: self.$t('CK_KS_79'),
					duration: 1000,
					icon: 'none'
				});
				return false;
			}
			let url = 'member/api/business/license/add';
			if (self.approveStatus == 4) {
				url = 'member/api/business/license/update';
			}
			self._post(url, formdata, function(res) {
				self.showSuccess(res.msg, function() {
					// #ifndef H5
					uni.navigateBack({
						delta: parseInt(self.delta)
					});
					// #endif
					// #ifdef H5
					history.go(-self.delta);
					// #endif
				});
			});
		},
		// 获取省市区
		getAreaData() {
			let self = this;
			self._get('system/api/area/tree', {}, function(res) {
				self.province = res.data;
				// self.city = res.data.regionData[1];
				// self.area = res.data.regionData[2];
				self.is_load = true;
				self.getData();
			});
		},
		/*三级联动选择*/
		showMulLinkageThreePicker() {
			if (this.approveStatus != 0 && this.approveStatus != 4) {
				return;
			}
			this.$refs.mpvueCityPicker.show();
		},

		/*确定选择的省市区*/
		onConfirm(e) {
			console.log(e);
			this.selectCity = e.label;
			this.form.licenseProvince = e.cityCode[0];
			this.form.licenseCity = e.cityCode[1];
			this.form.licenseCounty = e.cityCode[2];
		},

		changeType(e) {
			this.typeIndex = e.detail.value;
		},
		/*打开上传图片*/
		openUpload(type) {
			if (this.approveStatus != 0 && this.approveStatus != 4) {
				return;
			}
			this.isUpload = true;
			this.upload_type = type;
		},

		/*获取上传的图片*/
		getImgsFunc(e) {
			let self = this;

			self.isUpload = false;
			if (e && typeof e != 'undefined') {
				self.form.licenseImage = e[0].url;
			}
		}
	}
};
</script>

<style lang="scss">
.agreement-box {
	width: 750rpx;
	background: #ffffff;
	padding: 28rpx 22rpx;
	box-sizing: border-box;
	margin-top: 2rpx;
	margin-bottom: 20rpx;

	.agreement-content {
		font-size: 24rpx;
		font-family: SourceHanSansCN;
		font-weight: 400;
		color: #999999;
		line-height: 32rpx;
		word-break: break-all;
		margin-bottom: 32rpx;
	}

	.agreement-btn {
		min-width: 320rpx;
		height: 76rpx;
		line-height: 1.5;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 20rpx;
		box-sizing: border-box;
		background: #282828;
		border-radius: 38rpx;
		color: #ffffff;
		font-size: 24rpx;
	}
}

.form-title {
	width: 750rpx;
	height: 106rpx;
	background: #fb3024;
	padding-left: 44rpx;
	position: relative;
	box-sizing: border-box;
	font-size: 28rpx;
	color: #ffffff;
	line-height: 106rpx;
	font-weight: bold;
	z-index: 1;
}

.form-title::before {
	content: '';
	position: absolute;
	top: 0;
	bottom: 0;
	margin: auto;
	left: 25rpx;
	width: 6rpx;
	height: 24rpx;
	background-color: #fb3024;
	z-index: 1;
}

.form-group {
	padding: 0 22rpx;
	background: #ffffff;

	.form-item {
		min-height: 104rpx;
		box-sizing: border-box;
		padding: 15rpx 0;
		line-height: 1.5;
		display: flex;
		justify-content: center;
		align-items: center;
		border-bottom: 1rpx solid #eee;

		.icon-jiantou {
			font-size: 32rpx;
			color: 090000;
		}

		.form-tips {
			font-size: 24rpx;
			color: #999;
		}

		.form-label {
			width: 255rpx;
			flex-shrink: 0;
			word-break: break-all;
			margin-right: 20rpx;
		}

		.disabled-input {
			width: 450rpx;
			height: 78rpx;
			padding: 0 18rpx;
			line-height: 78rpx;
			background: #eee;
			font-size: 28rpx;
			color: #333;
			flex: 1;
		}

		.form-textarea {
			width: 450rpx;
			padding: 0 18rpx;
			line-height: 1.5;
			font-size: 28rpx;
			color: #333;
			flex: 1;
			background: #fff;
		}

		.form-input {
			width: 450rpx;
			height: 78rpx;
			padding: 0 18rpx;
			line-height: 78rpx;
			background: #fff;
			font-size: 28rpx;
			color: #333;
			flex: 1;
		}

		.form-item:last-child {
			border: none;
		}
	}
}

.form-upload {
	padding: 30rpx 23rpx;
}

.form-upload-name {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
}

.form-upload-tips {
	font-size: 24rpx;
	color: #999;
	margin-bottom: 20rpx;
}

.form-upload-tips2 {
	font-size: 24rpx;
	color: #fb3024;
	line-height: 1.5;
}

.upload-add {
	width: 142rpx;
	height: 142rpx;
	margin-right: 18rpx;
	margin-bottom: 18rpx;
}

.get-code-btn {
	min-width: 72rpx;
	height: 62rpx;
	background: #333333;
	line-height: 62rpx;
	padding: 0rpx 20rpx;
	border-radius: 40rpx;
	white-space: nowrap;
	// border: 1rpx solid $dominant-color;
	color: #ffffff;
	box-sizing: border-box;
	margin-left: 46rpx;
	font-size: 24rpx;
}

.get-code-btn[disabled='true'] {
	// border: 1rpx solid #cccccc;
	color: #333;
	background-color: #ffffff;
}
</style>

<style lang="scss">
.address-form {
	padding: 0;
	border-radius: 0;
}

.address-box {
	padding: 0 31rpx 0 27rpx;
}

.address-form .key-name {
	width: 180rpx;
	height: 90rpx;
	line-height: 90rpx;
	font-size: 28rpx;
}

.address-form .btn-red {
	height: 88rpx;
	line-height: 88rpx;
	border-radius: 44rpx;
	box-shadow: 0 8rpx 16rpx 0 rgba(226, 35, 26, 0.6);
}

.addBtn {
	position: fixed;
	bottom: 60rpx;
	left: 0rpx;
	margin: 0 auto;
	width: 750rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}

.address-input {
	line-height: 90rpx;
	height: 90rpx;
	box-sizing: border-box;
	font-size: 28rpx;
}

.addtextarea {
	font-size: 28rpx;
	line-height: 1.5;
	padding: 25rpx 0;
}

.icon.icon-icon1 {
	font-size: 28rpx;
	color: #999;
}
</style>
