<template>
	<view class="page-wrap">
		<view class="card">
			<uni-card margin="0" :is-shadow="true">
				<u--form
				  labelPosition="left"
				  labelWidth="auto"
				  labelAlign="center"
				  :model="dataModel"
				  :rules="rules"
				  ref="wForm"
				  errorType="border-bottom"
				>
				<u--textarea v-html="dataModel.CONTEXT" border="surround" v-if="dataModel.FORM_STATUS==4"
					disabled></u--textarea>
					<u-form-item label="表单名称" prop="NAME" borderBottom>
						<u--input v-model="dataModel.NAME" placeholder="请输入编号" disabled disabledColor="#ffffff" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="组织结构" prop="Department" borderBottom>
						<u--input v-model="dataModel.Nav_Department.NAME" placeholder="请输入编号" disabled disabledColor="#ffffff" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="岗位" prop="Post" borderBottom>
						<u--input v-model="dataModel.Nav_Post.NAME" placeholder="请输入编号" disabled disabledColor="#ffffff" border="none" inputAlign="right">
						</u--input>
					</u-form-item>
					<u-form-item label="生产工艺" prop="stepName" borderBottom>
						<u--input v-model="dataModel.Nav_OperationStep.NAME" disabled disabledColor="#ffffff" placeholder="请输入作业名称" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
					<u-form-item label="班次" prop="CLASS" borderBottom>
						<u--input v-model="dataModel.CLASS" placeholder="请输入作业地点" border="none"
							inputAlign="right"></u--input>
					</u-form-item>
				</u--form>
			</uni-card>
		</view>
		
		<view class="sub-form">
			<view class="sub-form-wrap">
				<view class="sub-form-btns">
					<view class="sub-form-btn" @click="handleAddDetail">
						<!-- <u-icon class="icon" name="plus" color="#2979ff" size="14"></u-icon> -->
						<u--text type="primary" text="新增记录明细+"></u--text>
					</view>
				</view>
				<u--form labelPosition="left" labelWidth="auto" labelAlign="center" errorType="border-bottom"
								 ref="sForm">
					<uni-collapse :border="false" accordion>
						<uni-card style="margin-bottom: 16px;" margin="0" spacing="0" :is-shadow="false"
											v-for="(item, index) in dataModel.Nav_Details" :key="index">
							<uni-collapse-item title-border="none" :show-arrow="false">
								<view slot="title" class="collapse-title">
									<view class="down">
										<uni-icons type="bottom"></uni-icons>
									</view>
									<view class="text">{{index + 1 + '. ' + item.NAME}}</view>
									
								</view>
								<u-form-item label="顺序" prop="NUM" borderBottom>
									<u-number-box v-model="item.NUM" @change="valChangeDetail" :min="0" style="margin-left: auto;"></u-number-box>
									<!-- <u--input disabledColor="#fff"
										v-model="item.NUM" border="none"
										inputAlign="right"></u--input> -->
								</u-form-item>
								<u-form-item label="事项" prop="NAME" borderBottom>
									<u--input disabledColor="#fff"
														v-model="item.NAME"
														border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="参数" prop="PARAM" borderBottom>
									<u--input disabledColor="#fff"
														v-model="item.PARAM"
														border="none" inputAlign="right"></u--input>
								</u-form-item>
								<u-form-item label="单位" prop="UNIT" borderBottom>
									<u--input disabledColor="#fff"
														v-model="item.UNIT"
														border="none" inputAlign="right"></u--input>
								</u-form-item>
							<u-form-item label="数值" prop="VALUE" borderBottom>
								<u-number-box v-model="item.VALUE" @change="valChangeDetail" :min="0" style="margin-left: auto;"></u-number-box>
							</u-form-item>
							<u-form-item label="记录时间" prop="jobDate" @click="showCheckDate({ dataIndex: index, name: 'jobDate'})">
								<u--input disabledColor="#fff" v-model="item.RECORD_TIME" disabled placeholder="请选择记录时间" suffixIcon="arrow-right" suffixIconStyle="font-size:12px" fontSize="14px"
									customStyle="margin:0px;display:flex;padding:3px 9px" border="none">
								</u--input>
							</u-form-item>
								<u-form-item>
									<button :disabled="!isLoadOK" type="primary" style="height: 100%;background-color: darkorange;width: 60%;font-size:12px"
										@click="handleDelRowBeforeDetail(index)">删除</button>
								</u-form-item>
							</uni-collapse-item>
						</uni-card>
					</uni-collapse>
				</u--form>
			</view>
		</view>
		
	
	<u-modal :show="showDelModalDetail >= 0" @confirm="confirmDelDetail" title="确认删除？"></u-modal>
	
		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="closePicker" @cancel="closePicker" keyName="NAME"></u-picker>
			<u-datetime-picker :show="dateTimePickerInfo.showCheckDate" mode="datetime" v-model='dateTimePickerInfo.defaultDateTime' :formatter="formatter" @confirm="handleCheckDate"
				@close="dateTimePickerInfo.showCheckDate = false;" @cancel="dateTimePickerInfo.showCheckDate = false;"></u-datetime-picker>
		<view class="bottom-button">
			<button type="primary" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		guid,
		initFilter,
		initFilterGroup,
		extendIgnoreDataRule
	} from '../../../../utils/common'
	import {
		getRequest,
		saveProcessFormxk,
	} from '../../../../services/apply/subPages/SCPT/PTServices'
	import config from '../../../../config/common'

	export default {
		data() {
			return {
				ID:'',
				tableKey: 0,
				curTotal: 0,
				showDelModalDetail: undefined,
				isLoadOK: false,
				userLists: [],
				dataModel: {
					NAME: '',
					CLASS:'',
					Nav_OperationStep:{},
					Nav_Department:{},
					Nav_Post:{},
					Nav_Details:{
						NUM:0,
						NAME:'',
						PARAM:'',
						UNIT:'',
						VALUE:'',
						RECORD_TIME:uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
					}
				},
				subDataModel: {
					Nav_Details:{
						NUM:0,
						NAME:'',
						PARAM:'',
						UNIT:'',
						VALUE:'',
						RECORD_TIME:uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
					}
				},
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					},
					'PARAM': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change']
					}
				},
				comPickerInfo: {
					showSheet: false,
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},
				dateTimePickerInfo: {
					showCheckDate: false,
					dataIndex: undefined,
					defaultDateTime: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM'),
					value: '',
					name: ''
				},
				currentOperateUser: {},
				showPopup: false,
				TaskID: '',
				formatter: null,
			}
		},
		onLoad(opt) {
			this.handleGetProcessFormxkData(opt)
		},
		methods: {
			handleGetProcessFormxkData(opt) {
				this.TaskID = opt.taskID ? opt.taskID : '';
				this.ID = opt.ID ? opt.ID : '';
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "")
				 extendRule(json, 'ID', 1, opt.ID)
					extendInclude(json, 'Nav_Department');
					extendInclude(json, 'Nav_Post');
					extendInclude(json, 'Nav_OperationStep');
					// extendInclude(json, 'Nav_JobEventPerson');
					// extendInclude(json, 'Nav_JobEventPerson.Nav_User.Nav_Department');
					extendInclude(json, 'Nav_Details');
				// TODO: id 未获取
				getRequest(json,"/PR/ProcessFormXk/Get").then(res => {
					this.dataModel = res
					if(!res.OPERATION_STEP_ID)
					{
						res.Nav_OperationStep =[]
					}
					if(!res.POST_ID)
					{
						res.Nav_Post =[]
					}
					if(!res.DEPARTMENT_ID)
					{
						res.Nav_Department =[]
					}
					if (res.Nav_Details && res.Nav_Details.length > 0) {
						for (let i = 0; i < res.Nav_Details.length; i++) {
							if (res.Nav_Details[i].RECORD_TIME === null) {
								res.Nav_Details[i].RECORD_TIME = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd hh:MM')
							}
							else
							{
								res.Nav_Details[i].RECORD_TIME = uni.$u.timeFormat(res.Nav_Details[i].RECORD_TIME, 'yyyy-mm-dd hh:MM')
							}
						}
					}
					
					this.isLoadOK = true
				})
			},
			valChangeDetail(e){
				// this.dataModel.Nav_Details[index].NUM=e.value;
			},
			valChangeFlow(e) {
				// this.dataModel.Nav_Flow[index].NUM=e.value;
			},
			valChangeDeal(e) {
				// this.dataModel.Nav_Measure[index].NUM=e.value;
			},
			//隐藏控件 显示时间
			handleCheckDate(e) {
				// let v = uni.$u.timeFormat(e.value, 'yyyy-mm-dd')
				const {
					name,
					dataIndex
				} = this.dateTimePickerInfo
				if (name === 'jobDate') {
					// this.dataModel.jobDate = uni.$u.timeFormat(e.value,
					// 	'yyyy-mm-dd')
					this.dataModel.Nav_Details[dataIndex].RECORD_TIME = uni.$u.timeFormat(e.value,
						'yyyy-mm-dd hh:MM')
				}
				this.dateTimePickerInfo.showCheckDate = false
			},
			//显示时间控件
			showCheckDate(p) {
				if (p.name === 'jobDate') {
					if (this.dataModel.Nav_Details[p.dataIndex].RECORD_TIME == null) {
						this.dataModel.Nav_Details[p.dataIndex].RECORD_TIME = uni.$u.timeFormat(new Date(),
							'yyyy-mm-dd hh:MM')
					}
					this.dateTimePickerInfo = {
						showCheckDate: true,
						dataIndex: p.dataIndex,
						defaultDateTime: this.dataModel.Nav_Details[p.dataIndex].RECORD_TIME,
						name: p.name
					}
				} 
			
			},
			handleAddDetail() {
				this.dataModel.Nav_Details.unshift(this.subDataModel.Nav_Details)
			},
			handleDelRowBeforeDetail(index) {
				this.showDelModalDetail = index
			},
			
			confirmDelDetail() {
				this.dataModel.Nav_Details.splice(this.showDelModalDetail, 1)
				this.showDelModalDetail = undefined
			},
			
			handleClosePopup() {
				this.showPopup = false
			},
			
			onConfirmPicker(e) {
				const {
					name,
					dataIndex,
					formIndex
				} = this.comPickerInfo
				
				
				this.comPickerInfo.showSheet = false
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					dataIndex: undefined,
					formIndex: undefined
				}
			},
			handleDelAction({
				name
			}) {
				uni.showModal({
					title: '是否删除数据？',
					success: (res) => {
						if (res.confirm) {

						}
					}
				})
			},
			submit() {
				const ele = this.$refs
				ele['wForm'].validate().then(res => {
					this.dataModel.PUBLISH = "SaveAndNotify";
					this.dataModel.TaskID = this.TaskID;
					this.dataModel.ID = this.ID;
					this.dataModel.ORG_ID =uni.getStorageSync('orgId')

					saveProcessFormxk(this.dataModel).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							// if (this.tableKey == null || this.tableKey == 0) {
							// 	uni.navigateTo({
							// 		url: 'processFormxkRecord'
							// 	})
							// } else {
								uni.navigateBack()
							// }
						}, 'success', 1000)
					})
				}).catch(err => {
					uni.$showErrorInfo('请检查必填项，必填项不能为空')
				})
			},
			}
	}
</script>

<style scoped>
	.page-wrap {
		padding: 16px 16px 66px;
	}

	.card {
		margin-bottom: 18px;
	}

	.sub-form {
		margin-bottom: 16px;
	}
	.sub-form-btn {
		margin-bottom: 8px;
	}
	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 10px 16px;
		box-sizing: border-box;
		background: #fff;
		z-index: 999;
	}

	.upload-title {
		color: #303133;
		font-size: 15px;
		line-height: 22px;
		padding: 8px 0;
	}
	
	.collapse-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}
	.collapse-title .down {
		width: 20px;
		flex: 0 0 auto;
	}
	.collapse-title .text {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		flex: 1;
		width: calc(100% - 60px);
	}
	.collapse-title .action {
		width: 40px;
		flex: 0 0 auto;
	}

	.page-wrap>>>.u-upload__button {
		margin-bottom: 0;
	}
</style>
