<template>
	<view>
		<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="80">
			<view class="se-p-20">
				<view class="se-p-20 se-bgc-white se-br-10 se-fs-20">
					<u-form-item label="公司名称" prop="company">
						<u--input v-model="form.company"  placeholder="请输入公司名称"></u--input>
					</u-form-item>
					<u-form-item label="联系人" prop="name">
						<u--input v-model="form.name"  placeholder="请输入姓名名称"></u--input>
					</u-form-item>
					<u-form-item label="联系电话" prop="mobile">
						<u--input v-model="form.mobile"  placeholder="请输入联系方式"></u--input>
					</u-form-item>
					<u-form-item label="公司地址" prop="address">
						<u--input v-model="form.address"  placeholder="请输入公司地址"></u--input>
					</u-form-item>
				</view>
			</view>
			<view class="se-p-20">
				<view class="se-px-20 se-pb-20 se-bgc-white se-br-10 se-fs-20">
					<u-form-item prop="file" labelWidth="0">
						<view class="se-flex se-flex-v-sa">
							<view class="se-py-20 se-w-p-100 se-flex">
								<view class="line-green"></view>
								<view class="se-ml-10">
									营业执照上传
								</view>
							</view>
							<view class="se-py-20 se-w-p-100">
								<u-upload :fileList="form.fileList" @afterRead="afterRead" @delete="deletePic" name="1"
									multiple :maxCount="10"></u-upload>
							</view>
						</view>
					</u-form-item>
				</view>
			</view>
			<view class="se-px-20 se-pt-20">
				<view class="se-px-20 se-pb-80 se-fs-20 se-flex">
					<view @click="submit"
						class="se-mx-10 se-flex-1 se-br-40 se-flex-h-c se-h-80 se-lh-80 se-ta-c se-fs-28 se-c-white se-bgc-green">
						<text>确认</text>
					</view>
				</view>
			</view>
		</u--form>
	</view>
</template>
<script>
	import { addCompany } from "@/common/api.js"
	export default{
		data(){
			return{
				form: {
					name: '',
					mobile: '',
					company:'',
					address:'',
					file: '',
					fileList:[],
					
					// name: 'jerryxiao',
					// mobile: '13189698114',
					// company:'小肖公司',
					// address:'湖南长沙市岳麓区',
					// file: '1',
					// fileList:[
					// 	{
					// 		url:"https://tennis-oss.xzaiyp.top/2024-12-14/7e98056b-e0b9-496f-89f8-88026ce9426e.png"
					// 	},
					// 	{
					// 		url:"https://tennis-oss.xzaiyp.top/2024-12-14/499fcd8a-3d4b-42f3-b023-61bac6b1c020.png"
					// 	}
					// ],
				},
				rules: {
					name: [{
						type: 'string',
						required: true,
						message: '请输入姓名名称',
						trigger: ['blur', 'change']
					}],
					mobile: [
						{
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							validator: (rule, value, callback) => {
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							trigger: ['change','blur'],
						},
					],
					company: [{
						type: 'string',
						required: true,
						message: '请输入公司名称',
						trigger: ['blur', 'change']
					}],
					address: [{
						type: 'string',
						required: true,
						message: '请输入公司地址',
						trigger: ['blur', 'change']
					}],
					
					file: [{
						type: 'string',
						required: true,
						message: '请选择营业执照',
						trigger: ['blur', 'change']
					}]
				},
			}
		},
		watch: {
			'form.fileList'(newValue, oldValue) {
				if (newValue.length > 0) {
					this.form.file = '有'
				} else {
					this.form.file = ''
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			submit() {
				let that = this;
				that.$refs.uForm.validate().then(res => {
					// uni.$u.toast('校验通过')
					that.onAddCompany()
				}).catch(errors => {
					// uni.$u.toast('校验失败')
				})
			},
			onAddCompany(){
				let that = this
				let params={
					companyName:that.form.company,
					userName:that.form.name,
					phone:that.form.mobile,
					address:that.form.address,
					businessLicense:that.form.fileList.map(item => item.url).join(','),
				}
				addCompany(params).then(response=>{
					uni.$u.toast(response.result)
					setTimeout(()=>{
						uni.switchTab({
							url:"/pages/home/index"
						})
					},1500)
				}).catch(error=>{
					
				})
			},
			deletePic(event) {
				this.form.fileList.splice(event.index, 1)
			},
			async afterRead(e) {
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self.form.fileList.push({
							url
						})
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.line-green {
		width: 8rpx;
		height: 32rpx;
		background: #669A32;
		border-radius: 4rpx;
	}
</style>