<!-- 专利申请 -->
<template>
	<view class="container">
		<scroll-view scroll-y style="padding-bottom: 180rpx;background-color: #fff;">
			<uni-forms v-model="form" label-width="150upx"  ref="customForm" :rules="customRules">
				<ticketd :ticket_="form.ticket_"></ticketd>
				<uni-forms-item label="专利名称" required name="name">
					<uni-easyinput v-model="form.name" placeholder="请输入专利名称" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="发明人" required>
					<uni-easyinput v-model="form.author" placeholder="请输入发明人" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="专利类型" required>
					<uni-data-checkbox v-model="form.type" :localdata="hobby"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="是否公开" required>
					<uni-data-checkbox v-model="form.is_public" :localdata="hobby2"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="申请地域" required>
					<uni-data-checkbox v-model="form.area" :localdata="hobby3"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="" v-if="form.area.includes('Foreign')||form.area.includes('PCT')">
					<uni-easyinput v-model="form.other_area" placeholder="请输入申请的国家" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="技术状态"></uni-forms-item>
				<template v-for="(item,index) in hobby4">
					<uni-data-checkbox multiple v-model="item[0].status" :localdata="item" @change="checkChange" style="padding-left:150upx;"></uni-data-checkbox>
					<template v-if="item[0].status.length>0" style="display: flex;justify-content: flex-end;width:100%">
						<uni-file-picker v-model="item[0].file" limit="1" :inde=index return-type="object" file-mediatype="all" style="padding-left:150upx;" class="flex_file_picker"  @select="fileSelect" @success="fileSuccess"></uni-file-picker>
					</template>
				</template>
				<uni-forms-item label="技术文件"></uni-forms-item>
				<template v-for="item in hobby5">
					<view  style="display: flex;margin-bottom: 20upx;">
						<uni-data-checkbox multiple v-model="item[0].name" :localdata="item" @change="checkChange" style="padding-left:150upx;flex: none"></uni-data-checkbox>
						<uni-number-box v-model="item[0].pages" :disabled="mode=='show'||item[0].name.length<1"></uni-number-box>
					</view>
				</template>
			</uni-forms>
		</scroll-view>
		<view class="footer_fixed">
			<ticketd_b :workflow_key="'wf_patent'" :title="form.name + '-专利审批'" :t_id="form.id" :ticket_="form.ticket_"
				@success="submitSuccess" :submit_b_func="submit_b_func" ref="ticketd_b_start"></ticketd_b>
		</view>
	</view>
</template>

<script>
import ticketd_b from "../wf/ticketd_b.vue"
import ticketd from "../wf/ticketd.vue"
import {actStateEnum} from "@/utils/enum.js"
	export default {
		components: { ticketd_b, ticketd },
		data(){
			return{
				mode:"add",
				t_id: null,
				form:{
					name:"",
					author:"",
					type:"",
					is_public:false,
					area:"",
					other_area:"",
					belong_dept:"",
					tech_status:[],
					tech_file:[],
				},
				type:0,
				hobby: [
					{text: '发明专利',value: 'invention'},
					{text: '实用新型专利',value: 'utility'}, 
					{text: '外观设计专利',value: 'design'},
				],
				hobby2:[
					{text: '是',value: true},
					{text: '否',value: false},
				],
				hobby3: [
					{text: '国内申请',value: 'Domestic'},
					{text: '国外申请',value: 'Foreign'}, 
					{text: 'PCT申请',value: 'PCT'},
				],
				hobby4: [
					[{text: '是否进行过科技成果鉴定',value: '是',status:[],file:'',files:[]}],
					[{text: '是否发表过文章',value: '是',status:[],file:'',files:[]}],
					[{text: '是否参与过展会展出',value: '是',status:[],file:'',files:[]}],
					[{text: '是否参与应用于生产/销售',value: '是',status:[],file:'',files:[]}],
					[{text: '是否参与过技术交流',value: '是',status:[],file:'',files:[]}],
					],
				hobby5: [// 发明专利、实用新型专利申请提供：
					[{text: '技术背景材料',value: '技术背景材料',pages:0,checked:false, name:[]}],
					[{text: '技术交底材料',value: '技术交底材料',pages:0,checked:false,name:[]}],
					[{text: '查新检索报告',value: '查新检索报告',pages:0,checked:false,name:[]}],
					[{text: '图或者照片',value: '图或者照片',pages:0,checked:false,name:[]}],//外观设计专利申请提供：
				],
				header:"",
				customRules: {
					filename: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					age: {
						rules: [{
							required: true,
							errorMessage: '年龄不能为空'
						}]
					},
				},
			}
		},
		async onLoad(options) {
			let that = this;
			that.form.id='';
			that.mode = options.mode?options.mode:'show';
			that.t_id = options.t_id?options.t_id:null;
			that.header = {
				Authorization: "Bearer " + uni.getStorageSync('access')
			}
			if(that.mode != "add"){
				if(that.t_id) {
					that.form = await that.$api.patentItem(that.t_id);
					that.form.tech_status.forEach(item=>{
						let index = 0;
						if(item.name=='是否进行过科技成果鉴定'){index=0}
						else if(item.name=='是否发表过文章'){index=1}
						else if(item.name=='是否参与过展会展出'){index=2}
						else if(item.name=='是否参与应用于生产/销售'){index=3}
						else if(item.name=='是否参与过技术交流'){index=4}
						that.hobby4[index][0].file = item.file;
						if(item.status=='是'){
							that.hobby4[index][0].status.push(item.status)
							let obj = {name:item.file,path:item.file};
							that.hobby4[index][0].files.push(obj)
						}
					})
					that.form.tech_file.forEach(item2=>{
						let index2 = 0;
						if(item2.name=='技术背景材料'){index2=0}
						else if(item2.name=='技术交底材料'){index2=1}
						else if(item2.name=='查新检索报告'){index2=2}
						else if(item2.name=='图或者照片'){index2=3}
						that.hobby5[index2][0].pages = item2.pages;
						that.hobby5[index2][0].checked = item2.checked;
						if(item2.checked){
							that.hobby5[index2][0].name=[];
							that.hobby5[index2][0].name.push(item2.name);
						}
					})
					console.log('_____________that.form______________',that.form)
					console.log('_____________that.hobby5',that.hobby5)
					console.log('_____________that.hobby4',that.hobby4)
					if(that.form.ticket_.state_.type == 1 && that.form.create_by == uni.getStorageSync("userInfo").id ) {
						that.mode = "edit";
					}else{
						that.mode = "show";
					}
				}
			}
		},
		methods:{
			checkChange(e){
				console.log('e',e)
			},
			sealChange(){
				console.log('seal1',this.seal1)
			},
			fileSelect(e,index){
				console.log('fileSelect',e)
				console.log('fileSelectindex',index)
				this.uploadFile(e,index)
			},
			fileSuccess(e){
				console.log('fileSuccess',e)
			},
			uploadFile(e,index) {
				let that = this;
				if (e.tempFiles.length === 0) {
					uni.showToast({
					  title: '请先选择文件',
					  icon: 'none',
					});
					return;
				}
				// 获取第一个选择的文件
				const file = e.tempFiles[0];
				let fieldName = 'file';
				let filePath = file.url;
				// 上传文件
				uni.uploadFile({
					url: "http://49.232.14.174:2226/api/file/", 
					filePath: filePath,           
					name: fieldName,               
					formData: {},
					header: that.header,
					success: (uploadRes) => {
					  	// 上传成功后的回调
					  	const data = JSON.parse(uploadRes.data);
						if(data){
							that.hobby4[index][0].file ={name:data.name,path:data.path} ;
							uni.showToast({
								title: '上传成功',
								icon: 'success',
							});
						}
					},
				});
			},
			async submit_b_func(id){
				let that = this;
				console.log('that.form',that.form)
				let tech_file = [],tech_status=[];
				that.hobby4.forEach(item4=>{
					let obj4 = {};
					if(item4[0].status.length>0){
						obj4.name = item4[0].text;
						obj4.status = '是';
						obj4.file = item4[0].file.path;
					}else{
						obj4.name = item4[0].text;
						obj4.status = '否';
						obj4.file = "";
					}
					tech_status.push(obj4)
				})
				that.hobby5.forEach(item5=>{
					let obj5 = {};
					obj5.name = item5[0].text;
					obj5.pages = item5[0].pages;
					obj5.checked = item5[0].name.length>0?true:false;
					tech_file.push(obj5)
				})
				let obj = {};
				obj = that.form;
				obj.tech_file = tech_file;
				obj.tech_status = tech_status;
				that.$refs.customForm.validate().then(res => {
					
				}).catch(err => {
					console.log('err', err);
				})
				if (that.mode != 'show') {
					if(obj.id) {
						await that.$api.patentUpdate(obj.id,obj)
					}else{
						let res = await that.$api.patentCreate(obj)
						that.form.id = res.id;
					}
				}
			},
			submitSuccess(){
				uni.navigateTo({
					url: "/pages/index/index"
				})
			},
		}
	}
</script>

<style scoped>
	.uni-data-checklist .checklist-group .checklist-box{
		margin: 10px 0!important;
	}
	.flex_file_picker>.uni-file-picker__files{
		flex-direction: row;
	}
	.flex_file_picker{
		width: 100%;
		flex: none;
	}
</style>