<template>
	<view class="template-edit tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
			</view>
			
			<view slot="default"  style="display: flex;">
				<view style="flex:1;margin-left:25px">
					<text :style="{fontSize:(wxFontSize)+'px'}">发布需求</text>
				</view>
				<view>
					<button plain="true" style="margin-right: 4px;border-radius: 24px;height:32px;line-height: 32px;font-size:15px;margin-top:4px;color:#1d60b1;border-color:#1d60b1;" @click="saveForm(1)" :style="{fontSize:(wxFontSize-3)+'px'}">暂存</button>
				</view>
			</view>
		</tn-nav-bar>

		<view class="tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">

			<tn-steps style="pointer-events:none;" :list="stepList" :current="stepIndex" mode="dotIcon" :fontSize="(wxFontSize-3)+'px'"></tn-steps>
			
			<view v-if="stepIndex==1">
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold" style="font-size: 16px;" :style="{fontSize:(wxFontSize-1)+'px'}">以下三种方式可任选其一，若有可都输入</view>
				</view>
			</view>

			<view class="tn-margin tn-bg-gray--light tn-padding" style="border-radius: 10rpx;">
				<textarea maxlength="500" v-model="content" placeholder="请描述您的需求..." :style="{fontSize:(wxFontSize-3)+'px'}"  :placeholder-style="styleString"></textarea>
			</view>

			<view class="tn-flex tn-flex-row-between tn-flex-col-center  tn-margin" style="margin-top:40px;margin-bottom: 0;">
				<view class="tn-flex justify-content-item">
					<view class=" tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-image" style="font-size: 20px;" :style="{fontSize:(wxFontSize+3)+'px'}"></text>
					</view>
					<view class="tn-padding-right-xs tn-text-bold"  style="font-size: 16px;" :style="{fontSize:(wxFontSize-1)+'px'}">上传图片</view>
				</view>
				<!-- <view class="justify-content-item tn-text-df tn-color-grey" @tap="clear">
					<text class="tn-padding-xs">清空上传</text>
					<text class="tn-icon-delete"></text>
				</view> -->
			</view>
			
			




			<view class="tn-margin-left tn-padding-top-xs">
				<uni-file-picker
					v-model="imgList" :limit="6" @delete="deleteFile"  :auto-upload="false" @select="select" @success="success">
				</uni-file-picker>
				<view style="margin-top: 8px;">
					<text style="color: #999;font-size: 12px;" :style="{fontSize:(wxFontSize-5)+'px'}">一次只能上传六张图片</text>
				</view>
				

			</view>
			
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin" style="margin-bottom: 0;">
				<view class="tn-flex justify-content-item">
					<view class=" tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-link" style="font-size: 20px;" :style="{fontSize:(wxFontSize+3)+'px'}"></text>
					</view>
					<view class="tn-padding-right-xs tn-text-bold" style="font-size: 16px;" :style="{fontSize:(wxFontSize-1)+'px'}">上传文件</view>
				</view>
				<!-- <view class="justify-content-item tn-text-df tn-color-grey" @tap="clear">
					<text class="tn-padding-xs">清空上传</text>
					<text class="tn-icon-delete"></text>
				</view> -->
			</view>
			
			<view class="tn-margin-left tn-padding-top-xs">
				<uni-file-picker
					v-model="fileList" :limit="3" mode="grid" @delete="deleteFile"  file-mediatype="all" file-extname="pdf,docx,doc,xls,xlsx" :auto-upload="false" @select="select" @success="success">
				<!-- <button size="default">上传文件</button> -->
				<view style="text-align: left;">
					<button size="mini" style="color:#1d60b1;border-color:#1d60b1;" plain="true" :style="{fontSize:(wxFontSize-5)+'px'}">上传文件</button>
				</view>
				<!-- <tn-button shadow shape="round" fontColor="tn-color-white" size="lg" backgroundColor="tn-bg-blue" :fontSize="24" height="auto" padding="20rpx 36rpx">上传文件</tn-button>
				 -->
				</uni-file-picker>
				<view style="margin-top: 4px;">
					<text style="color: #999;font-size: 12px;" :style="{fontSize:(wxFontSize-5)+'px'}">一次只能上传三个文件，支持格式 pdf .doc .xls，不超过5MB。</text>
				</view>
				
			
			</view>
			
			<view style="margin-top:20px;padding: 16px">
				<text style="font-size: 16px;line-height: 30px;" :style="{fontSize:(wxFontSize-1)+'px'}">需求有效期:</text>
				<uni-data-select
				    v-model="selectValue"
				    :localdata="selectList"
				    @change="changeSelect"
					placement="top"
					placeholder="请选择"
					:fontSize="(wxFontSize-3)+'px'"
				  ></uni-data-select>
			</view>
			

			<!-- 悬浮按钮-->
			<view class="tn-flex tn-footerfixed">
				<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
					<button type="default" plain="true" style="border-radius: 24px;"
						@click="nextStep()" :style="{fontSize:(wxFontSize-2)+'px'}">
						 下一步 
					</button>
				</view>
			</view>
</view>
<view v-if="stepIndex==2">
	
	<view style="padding:16px">
		<uni-forms :modelValue="formData" label-width="0">
			<view style="margin:16px 0" :style="{fontSize:(wxFontSize-3)+'px'}">
				<text style="color: red;line-height: 30px;vertical-align: middle;margin-right: 4px;" >*</text><text>公司名称:</text>
			<w-select
			     style="width: 100%;" 
			     v-model='searchValue' 
			     :list='items'
			     valueName='name' 
			     keyName="regNumber"
			     @change='selectChange'
				 :filterable="true"
				 :fontSize="(wxFontSize-3)+'px'"
			   >
			   </w-select>
			</view>
			<!-- <uni-forms-item label="公司名称" name="name">
				<uni-easyinput type="text" v-model="userInfo.company" placeholder="请输入所在公司名称" />
			</uni-forms-item> -->
			<text style="color: red;line-height: 30px;vertical-align: middle;margin-right: 4px;" :style="{fontSize:(wxFontSize-3)+'px'}">*</text><text :style="{fontSize:(wxFontSize-3)+'px'}">您的职称:</text>
			<uni-forms-item label="" name="realName" label-width="0">
				 <uni-easyinput type="text" v-model="formInfo.jobTitle" :inputSize="wxFontSize-3" :placeholder-style="styleString" :clearable="false" placeholder="请输入您的职称" />
			</uni-forms-item>
			<text style="color: red;line-height: 30px;vertical-align: middle;margin-right: 4px;" :style="{fontSize:(wxFontSize-3)+'px'}">*</text><text :style="{fontSize:(wxFontSize-3)+'px'}">联系人姓名:</text>
			<uni-forms-item label="联系人姓名" name="contactMethod">
				 <uni-easyinput type="text" v-model="formInfo.contactPerson" :inputSize="wxFontSize-3" :placeholder-style="styleString" :clearable="false" placeholder="请输入联系人姓名" />
			</uni-forms-item>
			<text style="color: red;line-height: 30px;vertical-align: middle;margin-right: 4px;" :style="{fontSize:(wxFontSize-3)+'px'}">*</text><text :style="{fontSize:(wxFontSize-3)+'px'}">联系方式:</text>
			<uni-forms-item label="联系方式" name="phone">
				 <uni-easyinput type="text"  v-model="formInfo.contactMethod" :inputSize="wxFontSize-3" :placeholder-style="styleString" :clearable="false" placeholder="请输入联系手机/微信/邮箱" />
			</uni-forms-item>
			 
			<uni-forms-item label="11" name="check">
				<tn-checkbox v-model="formInfo.agree" activeColor="#45c05d" :size="wxFontSize+17" name="选项1"  >
					<text :style="{fontSize:(wxFontSize-4)+'px',color:formInfo.agree?'#45c05d':'#666666'}">同意平台核查所填信息的真实性</text>
				</tn-checkbox>
			</uni-forms-item>
		</uni-forms>
	</view>
	
	<view class="tn-flex tn-footerfixed">
		<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
			<button type="default" plain="true" style="border-radius: 24px;"
				@click="preStep()" :style="{fontSize:(wxFontSize-2)+'px'}">
				 上一步 
			</button>
		</view>
		<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
			<button type="primary" style="background-color:#1d60b1;border-radius: 23px" @click="saveForm()" :style="{fontSize:(wxFontSize-2)+'px'}">提交审核</button>
		</view>
	</view>
</view>
		</view>
		
		

		<view class='tn-tabbar-height'></view>

	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js';
	import request from '../utils/request';
	export default {
		name: 'TemplateEdit',
		mixins: [template_page_mixin],
		data() {
			return {
				wxFontSize:17,
				canSave: true,
				selectValue: '',
				//非常紧急/两周/一月/长期
				selectList: [{
						value: '一周',
						text: '一周'
					},
					{
						value: '两周',
						text: '两周'
					},
					{
						value: '一月',
						text: '一月'
					}
				],
				content:'',
				formInfo:{
					jobTitle:'',
					agree:false,
					contactPerson:JSON.parse(uni.getStorageSync('userInfo')).contactNickName,
					contactMethod:JSON.parse(uni.getStorageSync('userInfo')).contactMethod||JSON.parse(uni.getStorageSync('userInfo')).userName
				},
				imgList:[],
				fileDetailList:[],
				stepIndex:1,
				stepList: [{
						name: '填写需求',
						icon: 'circle',
						selectIcon: 'circle-fill'
					},
					{
						name: '填写联系方式',
						icon: 'trusty',
						selectIcon: 'trusty-fill'
					}
				],
				formData: {
					apiType: 'this,ali',
					token: 'dffc1e06e636cff0fdf7d877b6ae6a2e',
					image: null
				},
				fileList: [],
				showUploadList: true,
				customBtn: false,
				autoUpload: true,
				showProgress: false,
				deleteable: true,
				customStyle: false,
				maxCount: 9,
				disabled: false,
				searchValue: '',
				items: [],
				org:{
					name:'',
					regNumber:''
				},
				editItem:{},
				styleString:'font-size:'+14+'px'
			}
		},
		watch: {
			searchValue(val, oldval) {
				console.error(val,this.org.name);
				if(val!==this.org.name){
					this.current = null;
				}
				if(this.stepIndex==2){
					this.search(val)
				}
				
			}
		},
		onLoad(props) {
			const appBaseInfo = wx.getAppBaseInfo();
			this.wxFontSize = uni.getStorageSync('fontSize')||appBaseInfo.fontSizeSetting||17;
			this.styleString='font-size:'+(this.wxFontSize-3)+'px';
			if(props.sid){
				this.isEdit = true;
				this.editItem = JSON.parse(uni.getStorageSync(props.sid)||'{}');
				this.content = this.editItem.content;
				this.fileDetailList = this.editItem.fileDetailList;
				
				for(let i=0;i<this.fileDetailList.length;i++){
					this.fileDetailList[i].path = this.fileDetailList[i].ftpUrl
					if(this.isImage(this.fileDetailList[i].fileName)){
						this.imgList.push({
							name: this.fileDetailList[i].fileName,
							url: this.fileDetailList[i].ftpUrl,
							path: this.fileDetailList[i].path
						})
					}else{
						this.fileList.push({
							name: this.fileDetailList[i].fileName,
							url: this.fileDetailList[i].ftpUrl,
							path: this.fileDetailList[i].path
						})
					}
				}
				
				this.org.name = this.editItem.company;
				this.formInfo.jobTitle = this.editItem.jobTitle;
				this.formInfo.contactPerson = this.editItem.contactPerson;
				this.formInfo.contactMethod = this.editItem.contactMethod;
			}
			this.getCompany();
		},
		methods: {
			 isImage(fileName) {
				const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'ico']
				const extension = fileName.split('.').pop().toLowerCase();
				return imageExtensions.includes(extension);
			 },
			 getCompany(){
			 	let that = this;
			 		request.post('/slbUserCompanyRel/show/my', {
			 			userNo: uni.getStorageSync('userNo')
			 		}).then(res => {
			 			if (res.success) {
			 				let list = res.list || [];
			 				for(let i=0;i<list.length;i++){
			 					list[i].name = list[i].company;
			 					list[i].regNumber= list[i].company;
								if(!that.isEdit&&list[i].isDefault=='1'){
									that.searchValue = list[i].name
									that.current = list[i].name;
									that.org = list[i];
									if(list[i].jobTitle){
										that.formInfo.jobTitle = list[i].jobTitle
										
									}
									if(list[i].contactPerson){
										that.formInfo.contactPerson = list[i].contactPerson
									}
									if(list[i].contactMethod){
										that.formInfo.contactMethod = list[i].contactMethod;
									}
									
									that.getAddress(list[i].name);
								}
								if(that.isEdit&&list[i].name==that.editItem.company){
									that.searchValue = list[i].name
									that.current = list[i].name;
									that.formInfo.jobTitle = list[i].jobTitle;
									that.formInfo.contactPerson = list[i].contactPerson;
									that.formInfo.contactMethod = list[i].contactMethod;
									that.org = list[i];
									
									that.getAddress(list[i].name);
								}
			 					 
			 				}
			 				that.items = list;
			 			}  
			 		})
			},
			preStep(){
				this.stepIndex = 1;
			},
			nextStep(){
				
				if(this.content==''&&this.fileDetailList.length===0){
					 uni.showToast({
						title:'请输入您的需求或上传图片/文件',
						icon:'none'
					 })
					 this.stepIndex = 1;
					 
					 return false;
				}
				 
				this.stepIndex = 2;
			},
			
			changeSelect(e) {
			        this.selectValue = e;
			},
			saveForm(status){
				if(!this.canSave){
					return false;
				}
				let that = this;
				let params = {
					
				};
				
				if(status!=1&&!this.org.name){
					uni.showToast({
						title: '请输入公司名称',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				
				
				
				if(status!=1&&this.formInfo.jobTitle.length<1){
					uni.showToast({
						title: '请输入您的职称',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				if(status!=1&&this.formInfo.contactPerson.length<1){
					uni.showToast({
						title: '请输入联系人姓名',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				if(status!=1&&this.formInfo.contactMethod.length<1){
					uni.showToast({
						title: '请输入联系方式',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				
				if(status!=1&&!this.formInfo.agree){
					uni.showToast({
						title: '请勾选同意平台核查所填信息的真实性',
						duration: 2000,
						icon:'none'
					});
					return false;
				}
				uni.showToast({
					title: '提交中...',
					icon:'none'
				});
				
				that.canSave = false;
				
				params.slbResourceDemand = {
					type:'3',
					company:this.org.name,
 					jobTitle:this.formInfo.jobTitle,
					contactPerson:this.formInfo.contactPerson,
					contactMethod:this.formInfo.contactMethod,
					content:that.content,
					userNo:uni.getStorageSync('userNo'),
					validDate:this.selectValue,
					status: status==1?status:3,
					id: that.isEdit?that.editItem.id:undefined,
					bisNo: that.isEdit?that.editItem.bisNo:undefined,
					version: that.isEdit?that.editItem.version:undefined
				};
				
				params.slbResourceDemand.companyEntity = {};
				params.slbResourceDemand.companyEntity.company = params.slbResourceDemand.company;
				params.slbResourceDemand.companyEntity.jobTitle = params.slbResourceDemand.jobTitle;
				params.slbResourceDemand.companyEntity.creditCode = this.org.creditCode;
				params.slbResourceDemand.companyEntity.address = this.org.base;
				params.slbResourceDemand.companyEntity.regStatus = this.org.regStatus;
				
				params.slbResourceDemand.companyEntity.contactPerson = params.slbResourceDemand.contactPerson;
				params.slbResourceDemand.companyEntity.contactMethod = params.slbResourceDemand.contactMethod;
				
				params.slbResourceDemand.slbUserExt = {
						contactPerson:params.slbResourceDemand.contactPerson,
						contactMethod:params.slbResourceDemand.contactMethod,
						userNo:uni.getStorageSync('userNo'),
				};
				
				params.slbResourceDemand = JSON.stringify(params.slbResourceDemand);
				
				
				// params.slbResourceDemand.companyEntity.contactNickName = params.slbResourceDemand.contactNickName;
				// params.slbUserExt = JSON.stringify({
				// 	jobTitle:this.formInfo.jobTitle,
				// 	contactPerson:this.formInfo.contactPerson,
				// 	contactMethod:this.formInfo.contactMethod,
				// 	userNo:uni.getStorageSync('userNo'),
				// });
				
				params.fileDetailList = JSON.stringify(this.fileDetailList);
				request.post(that.isEdit?'/slbResourceDemand/update':'/slbResourceDemand/add', params).then(res => {
				    that.canSave = true;
					if(res.success){
						uni.showToast({
							title:status==1?'信息已暂存，请在我的需求中查看暂存信息':'发布已提交，请在我的需求中查看进度',
							icon:'none',
							success:()=>{
								setTimeout(()=>{
									uni.redirectTo({
									 url: "/pages/mine/need?tab="+(status==1?2:1)
									});
								},1500)
								
							}
						})
						
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
					console.warn(res);
				})
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			// 手动上传文件
			upload() {
				console.warn(121212);
			},
			// 手动清空列表
			clear() {
				this.$refs.imageUpload.clear()
			},
			// 图片拖拽重新排序
			onSortList(list) {
				console.log(list);
			},
			select(e) {
				console.log('选择文件：', e)
				let tempFiles = e.tempFiles;
				for (let i in tempFiles) {
					this.upfile(tempFiles[i])
				}
			},
			deleteFile(e, index) {
				for(let i=0;i<this.fileDetailList.length;i++){
					if(e.tempFile.path===this.fileDetailList[i].path){
						this.fileDetailList.splice(i, 1);
					}
				}
			},
			upfile(file) {
				let that = this;
				console.warn(file);
				uni.uploadFile({
					url: 'https://slb-m.lx-device.com/oss/upload/userFeedback', //仅为示例，非真实的接口地址
					filePath: file.url,
					name: 'file',
					success: (uploadFileRes) => {
						console.warn(JSON.parse(uploadFileRes.data));
						let resultMap = JSON.parse(uploadFileRes.data).resultMap;
						that.fileDetailList.push({
							name: file.name,
							fileName: file.name, // 原始文件名
							ftpUrl: resultMap.uploadUrl, // 文件访问url
							path:file.path
						})
					}
				});
			},
			// 上传成功
			success(e) {
				console.log('上传成功')
			},
			selectChange(e){
				console.error(e);
				this.searchValue = e.name
				this.current = e.regNumber;
				if(e.jobTitle){
					this.formInfo.jobTitle = e.jobTitle;
				}
				this.org = e;
				this.getAddress(e.name);
			},
			getAddress(name){
				let that = this;
				request.post('/member/getEnterPriseInfo', {
					keyWord:  name
				}).then(res => {
					if(res&&res.success&&res.resultMap.data){
					  if( res.resultMap.data.creditCode){
					  	 that.org.creditCode = res.resultMap.data.creditCode
					  }
					  if( res.resultMap.data.regStatus){
					  	 that.org.regStatus = res.resultMap.data.regStatus
					  }
					  if( res.resultMap.data.regLocation){
						 that.org.base = res.resultMap.data.regLocation
					  }
					}
				})
			},
			search: function(val) {
				let that = this;
				
				if (val && val.length > 3) {
					request.post('/member/searchCompys', {
						keyWord:  val
					}).then(res => {
						if(res&&res.success){
							let list = res.resultMap.data || [];
							that.items = list;
						} 
					})
				}else{
					that.items = [];
					that.org = {}; 
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.template-edit {}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 60%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		// background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		// color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 100rpx;
		height: calc(120rpx + env(safe-area-inset-bottom) / 2);
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);
		background: #fff;
	}

	/* 底部悬浮按钮 end*/

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}
	
	/deep/ .uni-forms-item__label {
		display: none;
	}
	/deep/ .uni-stat__select .uni-select__input-placeholder {
		font-size: 14px;
	}

	/* 标签内容 end*/
</style>