<!-- 印章申请 -->
<template>
	<view class="container">
		<scroll-view scroll-y style="padding-bottom: 180rpx;background-color: #fff;">
			<uni-forms v-model="form" label-width="200rpx">
				<ticketd :ticket_="form.ticket_"></ticketd>
				<uni-forms-item label="使用类型">
					<uni-data-checkbox v-model="type" :localdata="hobby1"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="文件名称" required>
					<uni-easyinput v-model="form.filename" placeholder="请输入文件名称" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="联系电话">
					<uni-easyinput v-model="form.contacts" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="印章类型">
					<uni-data-checkbox multiple v-model="form.seal" :localdata="hobby" @change="sealTypeChange"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item label="" v-if="(form.seal || '').includes('其他')">
					<uni-easyinput v-model="form.seal_other" placeholder="请输入印章名称" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="借用日期" v-if="type==1">
					<uni-datetime-picker v-model="timeRange" type="daterange" @maskClick="maskClick" />
				</uni-forms-item>
				<uni-forms-item label="借用理由" v-if="type==1">
					<uni-easyinput v-model="form.note" :disabled="mode=='show'"/>
				</uni-forms-item>
				<uni-forms-item label="文件内容" required>
					<uni-file-picker v-model="form.file" limit="1" return-type="object" file-mediatype="all" @select="fileSelect" @success="fileSuccess"></uni-file-picker>
				</uni-forms-item>
				<uni-forms-item label="用印份数" required>
					<uni-number-box v-model="form.file_count" :disabled="mode=='show'"></uni-number-box>
				</uni-forms-item>
			</uni-forms>
		</scroll-view>
		<view class="footer_fixed">
			<ticketd_b :workflow_key="'wf_seal'" :title="form.filename + '-印章申请'" :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{
				type:"out",
				mode:"add",
				t_id: null,
				timeRange:[],
				form:{
					id:null,
					file:"",
					filename:"",
					file_count:"",
					is_lending:false,
					contacts:"",
					belong_dept:"",
					ticket_:null,
				},
				type:0,
				hobby: [
					{text: '公章',value:  '公章'},
					{text: '法人章',value: '法人章'}, 
					{text: '合同章',value: '合同章'}, 
					{text: '财务章',value: '财务章'}, 
					{text: '其他',value: '其他'},
				],
				hobby1:[
					{text: '内用',value: 0},
					{text: '外用',value: 1},
				],
				header:"",
			}
		},
		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;
			if(that.mode != "add"){
				if(that.t_id) {
					that.form = await that.$api.sealItem(that.t_id);
					if(that.form.ticket_.state_.type == 1 && that.form.create_by == uni.getStorageSync("userInfo").id ) {
						that.mode = "edit";
					}else{
						that.mode = "show";
					}
				}else{
					// that.form = uni.getStorageSync("currentBooking");
				}
			}
			that.getHeader();
		},
		watch: {	
			timeRange(newval) {
				console.log('范围选:', this.timeRange);
			},
		},
		methods:{
			getHeader() {
				this.header = {
					Authorization: "Bearer " + uni.getStorageSync('access')
				}
			},
			maskClick(e){
				console.log('maskClick事件:', e);
			},
			fileSelect(e){
				console.log('fileSelect',e);
				this.uploadFile(e)
			},
			fileSuccess(e){
				console.log('fileSuccess',e)
			},
			sealTypeChange(){
				console.log('this.form.seal',this.form.seal)
			},
			uploadFile(e) {
				if (e.tempFiles.length === 0) {
					uni.showToast({
					  title: '请先选择文件',
					  icon: 'none',
					});
					return;
				}
				// 获取第一个选择的文件
				const file = e.tempFiles[0];
				console.log(this.$api.sealCreate)
				let fieldName = 'file';
				let filePath = file.url;
				// 上传文件
				uni.uploadFile({
					url: `${config.baseUrl}/file/`, 
					filePath: filePath,           
					name: fieldName,               
					formData: {},
					header: this.header,
					success: (uploadRes) => {
						console.log('uploadRes',uploadRes)
					  	// 上传成功后的回调
					  	const data = JSON.parse(uploadRes.data);
						console.log('上传返回数据:', data);
						if(data){
							this.form.file = data.path;
							uni.showToast({
								title: '上传成功',
								icon: 'success',
							});
						}
					},
				});
			},
			//选择会议室和日期后查询有无预定
			async submit_b_func(id){
				let that = this;
				if (that.mode != 'show') {
					if(that.form.id) {
						await that.$api.sealUpdate(that.form.id, that.form);
					}else{
						let res = await that.$api.sealCreate(that.form);
						that.form.id = res.id;
					}
				}
			},
			submitSuccess(){
				uni.navigateTo({
					url: "/pages/index/index"
				})
			},
		}
	}
</script>

<style scoped>
</style>