<!-- 离职申请 -->
<template>
	<view class="form-page">
		<scroll-view scroll-y class="form-scroll">
			<uni-forms v-model="form" label-width="180upx"  ref="customForm" style="height: 60%;">
				<view class="form-card" v-if="form.ticket_">
					<ticketd :ticket_="form.ticket_"></ticketd>
				</view>
				<view class="form-card">
					<view class="form-card-title">维修信息</view>
				<uni-forms-item label="创建时间" required v-if="mode!== 'add'">
					<span>{{form.create_time}}</span>
				</uni-forms-item>
				<uni-forms-item label="维修设备" required>
					<searchSelect 
					v-model="form.equipment" 
					:apiobjs="apiobj" 
					v-if="mode== 'add'"/>
					<span v-else>{{form.equipment_fullname}}</span>
				</uni-forms-item>
				<uni-forms-item label="故障描述" required>
					<textarea placeholder-style="color:#efefef" v-model="form.fault_description"  placeholder="故障描述" v-if="mode!='show'" class="form-textarea"/>
					<span v-else>{{form.fault_description}}</span>
				</uni-forms-item>
				<uni-forms-item label="故障类别" required v-if="mode!== 'add'&&(form.fault_cate!==null||form.ticket && form.ticket_.state_.name == '条保负责人分派')">
					<uni-data-select
					  v-if="form.ticket && form.ticket_.state_.name == '条保负责人分派'" 
					  v-model="ticket_data.fault_cate"
					  :localdata="cateList"
					></uni-data-select>
					<span v-else>{{form.fault_cate}}</span>
				</uni-forms-item>
				<uni-forms-item label="维修人" required v-if="mode!== 'add'&&(form.repair_user!==null||form.ticket && form.ticket_.state_.name == '条保负责人分派')">
					<searchSelect v-model="ticket_data.repair_user" :apiobjs="userApiobj" v-if="form.ticket && form.ticket_.state_.name == '条保负责人分派'"/>
					<span v-else>{{form.repair_user_name}}</span>
				</uni-forms-item>
				<uni-forms-item label="维修开始时间" required v-if="mode!== 'add'&&(form.repair_start_time!==null||form.ticket && form.ticket_.state_.name == '维修人员处理')">
					<uni-datetime-picker v-model="ticket_data.repair_start_time" type="date" v-if="form.ticket && form.ticket_.state_.name == '维修人员处理'" :clear-icon="false"/>
					<span v-else>{{form.repair_start_time}}</span>
				</uni-forms-item>
				<uni-forms-item label="维修工时" required v-if="mode!== 'add'&&(form.repair_duration!==null&&form.repair_duration>0||form.ticket && form.ticket_.state_.name == '维修人员处理')">
					<uni-number-box v-model="ticket_data.repair_duration"  v-if="form.ticket && form.ticket_.state_.name == '维修人员处理'"></uni-number-box>
					<span v-else>{{form.repair_duration}}</span>
				</uni-forms-item>
				<uni-forms-item label="维修描述" required v-if="mode!== 'add'&&(form.repair_description!==null||form.ticket && form.ticket_.state_.name == '维修人员处理')">
					<textarea v-model="ticket_data.repair_description"  placeholder="故障描述"  placeholder-style="color:#efefef" v-if="form.ticket && form.ticket_.state_.name == '维修人员处理'" class="form-textarea"/>
					<span v-else>{{form.repair_description}}</span>
				</uni-forms-item>
				</view>
			</uni-forms>
		</scroll-view>
		<view class="footer_fixed">
			<button v-if="mode=='edit'" class="form-btn form-btn-danger" @click="handleDel" :loading="saveLoading" :disabled="saveLoading">
				删除
			</button>
			<button v-if="mode!='show'" class="form-btn form-btn-primary" @click="handleSave" :loading="saveLoading" :disabled="saveLoading">
				提交审批
			</button>
			<ticketd_b v-if="form.ticket_ && mode == 'show'" :t_id="form.id" :ticket_="form.ticket_" 
			:ticket_data="ticket_data" @success="()=>{uni.navigateBack()}" ref="ticketd_b"></ticketd_b>
		</view>
	</view>
</template>

<script>
import ticketd_b from "../wf/ticketd_b.vue"
import ticketd from "../wf/ticketd.vue"
import searchSelect from "@/components/searchselect.vue"
export default {
	components: { ticketd_b, ticketd, searchSelect },
	data(){
		return{
			mode:"show",
			t_id: null,
			form:{
				equipment:"",
				fault_description:"",
			},
			apiobj:this.$api.equipmentList,
			userApiobj:this.$api.userList,
			userInfo:{},
			ticket_data:{},
			cateList:[
				{text: '机械',value: '机械'},
				{text: '电气',value: '电气'},
			],
		}
	},
	onLoad(options) {
		let that = this;
		that.mode = options.mode?options.mode:'show';
		that.t_id = options.t_id?options.t_id:null;
		if(that.t_id) {
			that.$api.repairItem(that.t_id).then(res=>{
				that.form = res;
				if(that.form.ticket_.state_.type == 1 && that.form.create_by == uni.getStorageSync("userInfo").id ) {
					that.mode = "edit";
				}else{
					that.mode = "show";
				}
			})
		}
	},
	methods:{
		async handleDel(){
			let that = this;
			await that.$api.repairDelete(that.form.id)
			uni.navigateBack()
		},
		handleSave(){
			let that = this;
			that.$refs.customForm.validate().then(res => {
				that.$api.repairCreate(that.form).then(ress=>{
					uni.navigateBack()
				})
			}).catch(err => {
				console.log('err', err);
			})
		},
	}
}
</script>

<style lang="scss" scoped>
.form-page { min-height: 100vh; background: #F0F2F5; }
.form-scroll { padding: 0; padding-bottom: 180rpx; }
.form-card { background: #fff; border-radius: 0; padding: 24rpx; margin-bottom: 0; box-shadow: none; }
.form-card-title { font-size: 30rpx; font-weight: 600; color: #1F2937; margin-bottom: 16rpx; padding-left: 16rpx; border-left: 6rpx solid #2BA471; }
.form-textarea { width: 100%; min-height: 160rpx; border: 2rpx solid #E5E7EB; border-radius: 12rpx; padding: 16rpx; font-size: 28rpx; color: #374151; box-sizing: border-box; background: #F9FAFB; }
.form-btn { flex: 1; height: 80rpx; line-height: 80rpx; border-radius: 14rpx !important; font-size: 28rpx; font-weight: 500; border: none !important; }
.form-btn-primary { background: linear-gradient(135deg, #2BA471, #1F8C5E) !important; color: #fff !important; box-shadow: 0 4rpx 12rpx rgba(43,164,113,0.2); }
.form-btn-danger { background: linear-gradient(135deg, #EF4444, #DC2626) !important; color: #fff !important; box-shadow: 0 4rpx 12rpx rgba(239,68,68,0.3); }
.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: 90px;
	flex: none;
}
</style>