<template>
	<view class="todo-page">
		<step-title :stepPage="stepsPage" :stepText="stepsText"></step-title>

		<!-- 		<view>
			<button style="width: 47%;float: left;margin-left: 2px;" @click="startRecognition">开始识别</button>
			<button style="width: 47%;float: right;margin-right: 2px;" @click="stopRecognition"
				:disabled="!isRecording">停止识别</button>
		</view> -->

		<view class="allview">
			<view class="card">
				<u--form labelWidth="auto" :model="model" ref="wForm" :rules="rules" class="demo-ruleForm">

					<u-form-item required label="建议事项名称" prop="NAME" borderBottom>
						<u--input v-if="stepsPage==0" disabledColor="#ffffff" v-model="model.NAME" border="none"
							slot="right" inputAlign="right" placeholder="请输入建议事项名称" fontSize="14px"
							@blur='stopRecognition("NAME")'></u--input> <!-- @touchstart="handlfocus('NAME')" -->
						<u--input v-else disabled disabledColor="#ffffff" v-model="model.NAME" border="none"
							slot="right" inputAlign="right" placeholder="请输入建议事项名称" fontSize="14px"></u--input>
					</u-form-item>
					<u-form-item class="subRequired" required label="建议事项类别" prop="Nav_ItemEnum.NAME" borderBottom
						@click="handleShowSheet({title: '建议事项类别', name: 'ItemEnumNAME'})">
						<u--input v-model="model.Nav_ItemEnum.NAME" disabled disabledColor="#ffffff"
							placeholder="请选择建议事项类别" border="none" inputAlign="right" fontSize="14px">
						</u--input>
						<u-icon v-if="stepsPage==0" style="margin-left: 4px;" slot="right" name="arrow-down" size="12">
						</u-icon>
					</u-form-item>

					<u-form-item label="建议事项描述" prop="NAME" borderBottom>
					</u-form-item>
					<u--textarea v-if="stepsPage==0" autoHeight v-model="model.DESCRIPTION"
						@blur='stopRecognition("DESCRIPTION")' placeholder="请输入建议事项描述" border="surround"></u--textarea>
					<u--textarea v-else autoHeight disabled v-model="model.DESCRIPTION" placeholder="请输入建议事项描述"
						border="surround"></u--textarea>

					<u-form-item v-if="stepsPage>0" class="subRequired" label="受理人" prop="Nav_Assignee.NAME"
						borderBottom> <u--input disabled disabledColor="#fff" v-model="model.Nav_Assignee.NAME"
							border="none" inputAlign="right"></u--input>
					</u-form-item>

					<u-form-item v-if="stepsPage>0" label="受理状况" prop="ASSIGNEE_STATUS_SHOW" borderBottom
						@click="handleChange({ data:model, title: '受理状况', name: 'OGAssigneeStatus'})">
						<u--input disabled disabledColor="#ffffff" v-model="model.ASSIGNEE_STATUS_SHOW" border="none"
							inputAlign="right" placeholder="请输入受理状况"></u--input>
						<u-icon v-if="stepsPage==1" style="margin-left: 4px;" slot="right" name="arrow-down"
							size="12"></u-icon>
					</u-form-item>

					<u-form-item label="受理状况描述" v-if="stepsPage>0" prop="ASSIGNEE_DESCRIPTION"
						borderBottom></u-form-item>
					<u--textarea v-if="stepsPage==1" autoHeight v-model="model.ASSIGNEE_DESCRIPTION"
						placeholder="请输入受理状况描述" border="surround"></u--textarea>
					<u--textarea v-if="stepsPage>1" autoHeight disabled v-model="model.ASSIGNEE_DESCRIPTION"
						placeholder="请输入受理状况描述" border="surround"></u--textarea>

					<u-form-item v-if="model.STATUS>0" class="subRequired" label="落实部门" prop="Nav_DoDepartment.NAME"
						borderBottom @click="handleShowSheet({title: '落实部门',
						name: 'Nav_DoDepartmentNAME'})">
						<u--input disabledColor="#fff" disabled v-model="model.Nav_DoDepartment.NAME" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down" v-if="stepsPage==1"></u-icon>
					</u-form-item>
					<u-form-item v-if="model.STATUS>0" class="subRequired" label="落实人员" prop="Nav_DoUser.NAME"
						borderBottom @click="handleShowSheet({title: '落实人员', name: 'Nav_DoUserNAME'})">
						<u--input disabledColor="#fff" disabled v-model="model.Nav_DoUser.NAME" border="none"
							inputAlign="right"></u--input>
						<u-icon style="margin-left: 4px;" slot="right" name="arrow-down" v-if="stepsPage==1"></u-icon>
					</u-form-item>

					<u-form-item v-if="model.STATUS>1" label="落实情况描述" prop="IMPLEMENT_DESCRIPTION"
						borderBottom></u-form-item>
					<u--textarea v-if="stepsPage==2" autoHeight v-model="model.IMPLEMENT_DESCRIPTION"
						placeholder="请输入落实情况描述" border="surround"></u--textarea>
					<u--textarea v-if="stepsPage>2" disabled autoHeight v-model="model.IMPLEMENT_DESCRIPTION"
						border="surround"></u--textarea>

					<view class="upload-title">建议文件：</view>
					<full-upload v-model="model.Nav_FileList" :isShowBtn='model.STATUS==0' :listProp='listPropUpload'
						:listPropVal='listPropValUpload'></full-upload>


					<view v-if="model.STATUS>1" class="upload-title">落实文件：</view>
					<full-upload v-if="model.STATUS>1" v-model="model.Nav_ImpFileList" :isShowBtn='stepsPage==2'
						:listProp='listPropUpload' :listPropVal='listPropValUpload'></full-upload>

					<u-form-item v-if="stepsPage>2" label="反馈的满意度" prop="SATISFACTION_STATUS_SHOW" borderBottom
						@click="handleChange( { data:model, title: '反馈的满意度', name: 'OGSatisfactionStatus'} )">
						<u--input disabled disabledColor="#ffffff" v-model="model.SATISFACTION_STATUS_SHOW"
							border="none" inputAlign="right" placeholder="请选择反馈的满意度"></u--input>
						<u-icon v-if="stepsPage==3" style="margin-left: 4px;" slot="right" name="arrow-down"
							size="12"></u-icon>
					</u-form-item>

				</u--form>
			</view>
			<view style="padding: 10px 16px;" class="bottom-button">
				<u-button v-if='isLoadOK' type="primary" @click="submit" color="#3d4b70">提交</u-button>
			</view>
		</view>


		<query-selector :multiple="selectorInfo.isMultiple" :show="selectorInfo.showPopup"
			:defaultChecked="selectorInfo.defaultChecked" :title="selectorInfo.title" :lists="selectorInfo.dataLists"
			@search="handleSearch" @select="handleSelected" @close="selectorInfo.showPopup=false"
			:total="selectorInfo.totalCount" />

		<u-picker :show="comPickerInfo.showSheet" :columns="comPickerInfo.columns" @confirm="onConfirmPicker"
			@close="comPickerInfo.showSheet=false" @cancel="comPickerInfo.showSheet=false" keyName="NAME"></u-picker>
	</view>
</template>

<script>
	import {
		extendFilterGroup,
		extendGroupRule,
		extendInclude,
		extendOrder,
		extendRule,
		extendIgnoreDataRule,
		guid,
		initFilter,
		initFilterGroup
	} from '../../../../utils/common'

	import {
		OrderPagedItemEnum,
		FullUpdateOpinion,
		GetOpinion
	} from '../../../../services/apply/subPages/scOG'

	import {
		getDepartmentLists
	} from '../../../../services/app'

	import {
		UserPagedLimit,
		getEnum,
		GetEnumAnyOrder
	} from '../../../../services/common'


	import {
		FMProductionUnit,
		CheckPeriods,
		CheckLevels
	} from '../../../../utils/enums.js'
	import {
		getRequest,
	} from '../../../../services/apply/FOServices/FOServices';
	import stepTitle from '@/components/custom/step-title.vue'
	import swipeActionItem from '../../../../uni_modules/uview-ui/libs/config/props/swipeActionItem';
	export default {
		components: {
			stepTitle,
		},
		data() {
			return {
				// recognition: null,
				// isRecording: false,
				// resultText: '',
				// voiceProp: 'NAME',

				TaskID: null,
				tableKey: '',
				isLoadOK: false,
				ORG_ID: uni.getStorageSync('orgId'),
				stepsText: ['发起', '受理', '落实', '评价', '归档'], //头部导航
				stepsPage: 0, //步骤
				model: {
					ID: '',
					NAME: '',
					TaskID: null,
					TYPE_ID: '',
					ItemEnumNAME: '',
					user: {
						NAME: ''
					},
					Nav_ItemEnum: {
						NAME: ''
					},
					DESCRIPTION: '',
					STATUS: 0,
					Nav_FileList: [],

					ASSIGNEE_ID: '',
					Nav_Assignee: {
						NAME: ''
					},
					DO_DEPARTMENT_ID: '',
					Nav_DoDepartment: {
						NAME: ''
					},
					DO_USER_ID: '',
					Nav_DoUser: {
						NAME: ''
					},
					Nav_ImpFileList: [],
					ASSIGNEE_STATUS: 0,
					ASSIGNEE_STATUS_SHOW: '未受理',
					ASSIGNEE_DESCRIPTION: '',
					IMPLEMENT_DESCRIPTION: '',
					SATISFACTION_STATUS: 0,
					SATISFACTION_STATUS_SHOW: ''
				},

				listPropUpload: ['EMPLOYEE_OPINION_REPORT_ID'],
				listPropValUpload: [],

				selectorInfo: {
					isSubShow: false,
					isMultiple: false,
					showPopup: false,
					totalCount: 0,
					title: '检查人员',
					itemData: {},
					index: 0,
					dataLists: [],
					defaultText: '',
					defaultValue: '',
					name: '',
					defaultChecked: []
				},

				listEnumStatus: [],
				listEnumStatusText: [],
				listEnumSatisfaction: [],
				listEnumSatisfactionText: [],

				comPickerInfo: {
					showSheet: false,
					dataItem: {},
					columns: [],
					title: '',
					dataIndex: undefined,
					formIndex: undefined,
					name: ''
				},

				rules: {
					'NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请填写建议事项名称'
					},
					'Nav_ItemEnum.NAME': {
						type: 'string',
						required: true,
						trigger: ['blur', 'change'],
						message: '请选择建议事项类别'
					}
				}
			}
		},
		onLoad(option) {
			this.model.ID = option.ID
			this.model.TaskID = option.taskID
			this.TaskID = option.taskID
			this.tableKey = option.tableKey

			this.ORG_ID = uni.getStorageSync('orgId')
			if (option.ID != null && option.ID != 'null') {
				this.listPropValUpload.push(this.model.ID)
				//受理枚举  OGAssigneeStatus
				this.getEnums() // this.loadData()

			} else {
				this.isLoadOK = true
				this.model.ID = guid()
				this.model.ORG_ID = this.ORG_ID
				this.listPropValUpload.push(this.model.ID)
				// this.model.Nav_ListSafeCheckDetail = null
			}
		},
		methods: {
			// startRecognition() {
			// 	try {
			// 		// this.isRecording = true
			// 		this.recognition = new(window.SpeechRecognition || window.webkitSpeechRecognition)()

			// 		// 设置识别参数
			// 		this.recognition.continuous = true
			// 		this.recognition.interimResults = true
			// 		this.recognition.lang = 'zh-CN' // 设置中文识别

			// 		// 事件监听
			// 		this.recognition.onstart = () => {
			// 			this.isRecording = true
			// 			this.resultText = '录音中...'
			// 		}

			// 		this.recognition.onspeechend = () => {
			// 			this.recognition.stop()
			// 		}

			// 		this.recognition.onresult = (event) => {
			// 			const transcript = Array.from(event.results)
			// 				.map(result => result[0])
			// 				.map(result => result.transcript)
			// 				.join('')

			// 			this.model[this.voiceProp] = transcript
			// 			// if (transcript.length > 5) {
			// 			// 	this.voiceProp = 'NAME'
			// 			// 	this.model[this.voiceProp] = transcript
			// 			// }
			// 		}

			// 		this.recognition.onerror = (event) => {
			// 			// console.log(location.protocol)
			// 			console.error('识别错误:', event.error)
			// 		}

			// 		this.recognition.start()
			// 	} catch (error) {
			// 		console.log(location.protocol)

			// 		uni.showToast({
			// 			title: '浏览器不支持语音识别',
			// 			icon: 'none'
			// 		})
			// 	}
			// },

			// stopRecognition(prop) {
			// 	if (prop) {
			// 		// 替换标点符号
			// 		if (this.model[this.voiceProp]) {
			// 			let symbolToRemove = /。/g // text.replace(symbolToRemove, "");					
			// 			this.model[this.voiceProp] = this.model[this.voiceProp].replace(symbolToRemove, "");
			// 		}
			// 		this.voiceProp = prop
			// 	}
			// 	if (this.recognition) {
			// 		this.recognition.stop()
			// 		this.isRecording = false
			// 	}
			// },

			getEnums() {
				var dataParm = {
					'name': "OGAssigneeStatus|OGSatisfactionStatus"
				}
				GetEnumAnyOrder(dataParm).then(res => {
					this.listEnumStatus = res[0]
					this.listEnumStatusText = []
					this.listEnumStatus.forEach(e => {
						e.name = e.NAME
						e.code = e.ID
						this.listEnumStatusText.push(e.NAME)
					})

					this.listEnumSatisfaction = res[1]
					this.listEnumSatisfactionText = []
					this.listEnumSatisfaction.forEach(e => {
						e.name = e.NAME
						e.code = e.ID
						this.listEnumSatisfactionText.push(e.NAME)
					})
					this.loadData()
				})
			},
			loadData() {
				const json = initFilter(this.ORG_ID, "", "", 1)
				extendRule(json, 'ID', 1, this.model.ID);
				extendInclude(json, "Nav_User")
				extendInclude(json, "Nav_LaunchDepartment")
				extendInclude(json, "Nav_ItemEnum")
				extendInclude(json, "Nav_Assignee")
				extendInclude(json, "Nav_DoDepartment")
				extendInclude(json, "Nav_DoUser")
				extendInclude(json, "Nav_FileList.Nav_ImgFile")
				extendInclude(json, "Nav_ImpFileList.Nav_ImgFile")
				json.IgnoreDataRule = true
				GetOpinion(json).then(res => {
					if (res.Nav_DoDepartment == null)
						res.Nav_DoDepartment = {
							NAME: ''
						}
					if (res.Nav_DoUser == null)
						res.Nav_DoUser = {
							NAME: ''
						}

					res.ASSIGNEE_STATUS_SHOW = '' // = '未受理' //res
					this.listEnumStatus.forEach((e, index) => {
						if (e.code == res.ASSIGNEE_STATUS) {
							res.ASSIGNEE_STATUS_SHOW = e.name
						}
					})

					this.isLoadOK = true
					this.stepsPage = res.STATUS
					this.model = res
				})
			},


			//下拉数据加载
			async handleShowSheet(p) {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "", 0, p.pageIndex ?? 1)
				let dataSelect = []
				var defaultChecked = []
				if (p.name == "ItemEnumNAME") {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					json.Sort = "NAME"
					json.Order = 1
					dataSelect = await OrderPagedItemEnum(json).then(res => {
						if (res != undefined && res.length > 0) {
							for (let i = 0; i < res.length; i++) {
								res[i].name = res[i].NAME
								// res[i].code = res[i].CODE
							}
						}
						return res
					})
				} else if (p.name == "Nav_DoUserNAME" || p.name == "Nav_AssigneeNAME") {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					// if (this.model.DO_DEPARTMENT_ID) {
					// 	extendRule(json, 'DEPARTMENT_ID', 1, this.model.DO_DEPARTMENT_ID);
					// }

					json.Sort = "NAME"
					json.Order = 0
					json.PageIndex = p?.pageIndex ?? 1
					json.Limit = 20
					if (p?.pageIndex) {
						json.Start = (p?.pageIndex - 1) * json.Limit;
					}
					dataSelect = await UserPagedLimit(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res != undefined && res.Data != undefined && res.Data.length > 0) {

							this.selectorInfo.dataLists = res.Data.map(i => {
								return {
									...i,
									name: i.NAME,
									code: i.CODE
								}
							})
						}
						return this.selectorInfo.dataLists
					})
				} else if (p.name == "Nav_DoDepartmentNAME") {
					if (p.val) {
						extendRule(json, 'NAME', 9, p.val);
					}
					json.Sort = "NAME"
					json.Order = 0
					json.PageIndex = p?.pageIndex ?? 1
					json.Limit = 20
					if (p?.pageIndex) {
						json.Start = (p?.pageIndex - 1) * json.Limit;
					}
					dataSelect = await getDepartmentLists(json).then(res => {
						this.selectorInfo.totalCount = res.TotalCount
						if (res != undefined && res.Data != undefined && res.Data.length > 0) {

							this.selectorInfo.dataLists = res.Data.map(i => {
								return {
									...i,
									name: i.NAME,
									code: i.CODE
								}
							})
						}
						return this.selectorInfo.dataLists
					})
				}

				if (defaultChecked == null)
					defaultChecked = []
				if (dataSelect.length) {
					this.selectorInfo = {
						itemData: p.itemData != undefined ? p.itemData : this.selectorInfo
							.itemData, //itemData: p.itemData,  搜索的时候  p.itemData为空  赋值修改 为 她自己本身 this.selectorInfo.itemData
						// isMultiple: this.selectorInfo.isMultiple,
						totalCount: this.selectorInfo.totalCount,
						showPopup: true,
						title: p.title,
						name: p.name,
						dataLists: dataSelect,
						defaultChecked: defaultChecked
					}
				} else {
					// 暂无数据
				}
			},
			handleSelected(e) {
				this.selectorInfo.showPopup = false
				if (this.selectorInfo.name == "ItemEnumNAME") {
					this.model.ItemEnumNAME = e.NAME
					this.model.Nav_ItemEnum = e
					this.model.TYPE_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_DoUserNAME") {
					this.model.Nav_DoUser = e
					this.model.DO_USER_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_AssigneeNAME") {
					this.model.Nav_Assignee = e
					this.model.ASSIGNEE_ID = e.ID
				} else if (this.selectorInfo.name == "Nav_DoDepartmentNAME") {
					this.model.Nav_DoDepartment = e
					this.model.DO_DEPARTMENT_ID = e.ID
				}
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},

			// //显示组件
			// handleChange() {
			// 	this.selectorInfo.showPopup = true
			// 	this.handleSearch()
			// },

			//数据加载
			handleSearch(val, pageIndex) {
				var p = {
					name: this.selectorInfo.name,
					title: this.selectorInfo.title,
					val: val,
					pageIndex: pageIndex
				}
				this.handleShowSheet(p)
			},

			submit() {
				const ele = this.$refs
				this.modelEdit = JSON.parse(JSON.stringify(this.model))
				this.modelEdit.Nav_ItemEnum = null

				this.modelEdit.Nav_Assignee = null
				this.modelEdit.Nav_DoDepartment = null
				this.modelEdit.Nav_DoUser = null

				ele['wForm'].validate().then(res => {
					if (this.TaskID != null && this.TaskID != '' && this.TaskID.length > 0)
						this.modelEdit.TaskID = this.TaskID
					this.modelEdit.PUBLISH = "SaveAndNotify";
					FullUpdateOpinion(this.modelEdit).then(res => {
						uni.$showMsgFunc('操作成功!', () => {
							uni.navigateBack()
						}, 'success', 1000)
					})
				}).catch(err => {
					if (err.length > 0) {
						uni.$showErrorInfo(err[0].message)
					} else {
						uni.$showErrorInfo('校验失败！')
					}
				})
			},




			handleChange(item) {
				var dataSour = []
				if (item.name == 'OGAssigneeStatus') {
					dataSour = this.listEnumStatusText
				} else if (item.name == 'OGSatisfactionStatus') {
					dataSour = this.listEnumSatisfactionText
				}
				this.comPickerInfo = {
					showSheet: true,
					columns: [dataSour],
					dataItem: item.data,
					title: item.title,
					name: item.name
				}
			},
			onConfirmPicker(e) {
				//赋枚举值
				if (this.comPickerInfo.name == 'OGAssigneeStatus') {
					this.comPickerInfo.dataItem.ASSIGNEE_STATUS_SHOW = e.value[0]
					this.comPickerInfo.dataItem.ASSIGNEE_STATUS = this.listEnumStatus[e.indexs[0]].code
				} else if (this.comPickerInfo.name == 'OGSatisfactionStatus') {
					this.comPickerInfo.dataItem.SATISFACTION_STATUS_SHOW = e.value[0]
					this.comPickerInfo.dataItem.SATISFACTION_STATUS = this.listEnumSatisfactionText[e.indexs[0]].code
				}

				this.comPickerInfo = {
					showSheet: false,
					dataItem: this.comPickerInfo.dataItem,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			}
		}
	}
</script>

<style scoped>
	@import url("../../../../style/css/newTemplate.css");

	.todo-page>>>.u-steps-item__line {
		width: 187.6px !important;
	}
</style>