<!-- 论文申密 -->
<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.paper_name" placeholder="请输入论文名称" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="期刊名称" required name="publication_name">
					<uni-easyinput v-model="form.publication_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="文章类型">
					<uni-data-checkbox v-model="form.paper_type" :localdata="hobby" @change="sealTypeChange"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="中文核心">
					<uni-data-checkbox v-model="form.is_chinese_core" :localdata="hobby2" @change="sealTypeChange"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="SCI/EI收录">
					<uni-data-checkbox v-model="form.is_sci" :localdata="hobby3" @change="sealTypeChange"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="" v-if="(form.seal || '').includes('PCT申请')">
					<uni-easyinput v-model="form.seal_other" 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_paperse'" :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 config from '/utils/config';
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:  '研究论文'},
					{text: '综述',value: '综述'},
				],
				hobby2:[
					{text: '是',value: true},
					{text: '否',value: false},
				],
				hobby3: [
					{text: '是',value: true},
					{text: '否',value: false},
				],
				hobby4: [
					[{text: '是否进行过科技成果鉴定',value: '是',checked:false,status:[],file:'',files:[]}],
					[{text: '是否发表过文章',value: '是',checked:false,status:[],file:'',files:[]}],
					[{text: '是否参与过展会展出',value: '是',checked:false,status:[],file:'',files:[]}],
					[{text: '是否参与应用于生产/销售',value: '是',checked:false,status:[],file:'',files:[]}],
					[{text: '是否参与过技术交流',value: '是',checked:false,status:[],file:'',files:[]}],
					],
				hobby5: [// 发明专利、实用新型专利申请提供：
					[{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.paperseItem(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;
						that.hobby4[index][0].checked = item.checked;
						that.hobby4[index][0].status = [];
						if(item.status=='是'){
							that.hobby4[index][0].status.push(item.status)
							let obj = {name:item.file,path:item.file};
							that.hobby4[index][0].file =obj;
							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}
						that.hobby5[index2][0].pages = item2.pages;
						that.hobby5[index2][0].checked = item2.checked;
						that.hobby5[index2][0].name=[];
						if(item2.checked){
							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: `${config.baseUrl}/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);
				})
				let res = await that.$api.paperseCreate(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>