<template>
	<view class="home">
		<view class="incidentList" v-if="tabsIndex1==1">
		  <view class="head">
		    <view class="tab" :class="{active: tab.id === dataInfo.tabActiveId}" v-for="tab in dataInfo.tabs" :key="tab.id" @click="clickTab(tab.id)">
		      {{tab.name}}<text v-if="tab.num !== ''">({{tab.num}})</text>
		    </view>
		    <view class="filter" @click="filterClick">
		      <text class="newicon newicon-shaixuan"></text>
		    </view>
		  </view>
		  <view class="body" v-if="dataInfo.list.length">
		    <view class="body_item" v-for="data in dataInfo.list" :key="data.id" @click.stop="toIncidentDetail(data)">
		      <view class="body_item_head ellipsis-multiline">
		        {{data.description}}
		      </view>

		      <view class="body_item_content">
						<view class="body_item_content_p" v-if="data.place || data.houseNumber">
						  <text class="name ellipsis">详细地址：{{data.place ? data.place.building.buildingName : ''}}{{data.place ? data.place.floorName : ''}}<text v-if="data.place && data.place.floorName">层</text> {{data.houseNumber}}</text>
							<view class="status" :style="stateStyle(data.state)">{{data.state ? data.state.name : ''}}</view>
						</view>
		        <view class="body_item_content_p" v-if="data.rsPrice">
		          <text class="name ellipsis">维修总价：{{data.rsPrice}}</text>
		        </view>
		        <view class="body_item_content_p">
							 <view class="name ellipsis" @click.stop="makePhoneCall(data.contactsInformation)">维修人员电话：{{data.contactsInformation || '暂无'}}<uni-icons v-if="data.contactsInformation" type="phone-filled" class="phone-filled" :size="18" :color="primaryColor"></uni-icons></view>
		        </view>
						<view class="body_item_content_p" v-if="data.currentLog && data.currentLog.logType.value=='overtime'">
						  <text class="name ellipsis">维修说明：{{data.overtimeRemark}}</text>
						</view>
		      </view>

		      <view class="body_item_foot">
		        <view class="foot_info">
		          <view class="name">维修人员：{{data.assigneeName || '暂无'}}</view>
		          <text class="date">{{formatDate(data.startDate, 'yyyy-MM-dd HH:mm')}}</text>
		        </view>
		        <view class="btns">
		          <button v-if="!data.wxdegree && (data.state.name=='待评价'|| data.state.name=='已关闭')" @click.stop="handler('changeUser', data)" type="default" class="primaryButton btn">评价</button>
							<button v-if="data.state.value=='accept'" @click.stop="recall(data)" type="default" class="primaryButton btn">撤销</button>
						</view>
		      </view>
					<div class="sign-style" v-if="deptRepair && publicRepair">
						<view class="img-box" v-if="data.repairIncidentType && data.repairIncidentType.value=='dept'">
							<view class="img-url1"></view>
							<view class="img-name">科</view>
						</view>
						<view class="img-box" v-if="data.repairIncidentType && data.repairIncidentType.value=='public'">
							<view class="img-url2"></view>
							<view class="img-name">公</view>
						</view>
					</div>
		    </view>
		  </view>
		  <view class="zanwu" v-else>
		    <text class="newicon newicon-zanwu"></text>
		  </view>
			<view class="body-height"></view>
		  <repairsFilter v-if="dataInfo.isFilter" @cancelEmit="cancelFilter" @confirmEmit="conformFilter" :evt="dataInfo.evtFilter"></repairsFilter>
		  <IncidentAttachment v-if="dataInfo.isAttachment" @knowEmit="knowAttachment" :incidentData="dataInfo.incidentData"></IncidentAttachment>
			<uni-popup ref="popup" background-color="#fff" type="center" :before-close="true">
				<view class="popup-content">
					<view class="form_item column">
						<view class="title"><text class="required newicon newicon-bitian"></text>星级：</view>
						<uni-rate class="value" v-model="rate" />
					</view>
					<view class="form_item column">
						<view class="title"><text class="required newicon newicon-bitian"></text>内容：</view>
						<uni-easyinput class="value" type="textarea" v-model="wxdegreeremark" placeholder="请输入评价内容"/>
					</view>
				</view>
				<view class="foot_common_btns">
				  <button @click="closePop" type="default" class="cancelButton btn">取消</button>
				  <button @click="submit" type="default" class="primaryButton btn">提交</button>
				</view>
			</uni-popup>
			<!-- 撤销 -->
			<uni-popup ref="inputDialog" background-color="#fff" type="center" :before-close="true">
				<view class="ch-class">撤销原因</view>
				<view class="popup-content">
					<input type="text" focus placeholder="请输入撤销原因" v-model="recallValue">
				</view>
				<view class="foot_common_btns">
					<button @click="closeDialog" size="mini" type="default" class="primaryButton btn">取消</button>
				  <button @click="dialogInputConfirm" size="mini" type="default" class="primaryButton btn">确定</button>
				</view>
			</uni-popup>
		</view>

		<view v-if="tabsIndex0==1">
			<view class="home_item" v-if="isDept.valueconfig==1">
				<view class="title">科室报修</view>
				<view class="content">
					<view @click="repairsView(1,'pending,handler,reassign')">
						<view class="con-title">处理中</view>
						<view class="con-value">{{repairData.deptHandlerCount}}</view>
					</view>
					<view @click="repairsView(1,'close0')">
						<view class="con-title">待评价</view>
						<view class="con-value">{{repairData.deptCloseCount}}</view>
					</view>
					<view>
						<view class="con-title">本月维修费用（元）</view>
						<view class="con-value-gr">{{repairData.deptCurrentMonthPrice || 0}}</view>
					</view>
				</view>
			</view>
			<view class="home_item">
				<view class="title">我的报修</view>
				<view class="content">
					<view @click="repairsView(0,'pending,handler,reassign')">
						<view class="con-title">处理中</view>
						<view class="con-value">{{repairData.userHandlerCount}}</view>
					</view>
					<view @click="repairsView(0,'close0')">
						<view class="con-title">待评价</view>
						<view class="con-value">{{repairData.userCloseCount}}</view>
					</view>
				</view>
			</view>
	<!-- 		<view class="home_item">
				<uni-notice-bar show-icon scrollable background-color="#ffffff" color="#000"
				:text="noticeData" />
			</view> -->
			<view class="home_item home-disp">
				<view class="bottom-left" @click="addRepairs">
					<view class="bottom-left-box">
						<text class="newicon newicon-kuaisubaoxiu icon"></text>
						<view>快速报修</view>
					</view>
				</view>
				<view class="bottom-right mar-t-20">
					<view class="bottom-right-item" v-if="isRepair.valueconfig==1" @click="scanCodes">
						<text class="newicon newicon-saoma icon"></text>
						<view class="name1">扫资产报修</view>
					</view>
					<view class="bottom-right-item" @click="repository">
						<text class="newicon newicon-zhishiku1 icon"></text>
						<view class="name2">知识库</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="tabsIndex2==1" class="mine">
			<view class="body">
			  <view class="bottom">
			    <view class="bottom_name">个人信息</view>
			    <view class="bottom_list">
			      <view class="bottom_list_item">
			        <view class="name"><text class="required newicon newicon-bitian"></text>工号</view>
			        <view class="value no-mar">{{dataForm.account}}</view>
			      </view>
						<view class="bottom_list_item">
						  <view class="name"><text class="required newicon newicon-bitian"></text>姓名</view>
						  <view class="value no-mar">{{dataForm.name}}</view>
						</view>
						<view class="bottom_list_item">
						  <view class="name"><text class="required newicon newicon-bitian"></text>院区名称</view>
							<uni-data-picker class="value" placeholder="请选择院区名称"
								v-model="dataForm.branch" :localdata="branchData" @change="branchChange"
								:clear-icon="false" :class="{formRed: isSubmit && !dataForm.branch}">
							</uni-data-picker>
							<text class="newicon newicon-youjiantou icon"></text>
						</view>
						<view class="bottom_list_item" v-if="deptRepair.valueconfig==1" @click="selectDepartment">
	<!-- 					  <view class="name"><text class="required newicon newicon-bitian"></text>科室名称</view>
							<uni-data-picker class="value" placeholder="请选择报修科室"
								v-model="dataForm.dept" :localdata="deptData"
								:clear-icon="false" :class="{formRed: isSubmit && !dataForm.dept}">
							</uni-data-picker> -->
							<text class="newicon newicon-youjiantou icon"></text>
							<!-- <view class="form_item"> -->
								<view class="title select"><text class="required newicon newicon-bitian"></text>报修科室：</view>
								<view class="value category">
									<text class="categoryName ellipsis-multiline">{{dataForm.departmentName}}</text>
									<!-- <text class="newicon newicon-weibiaoti2010104"></text> -->
								</view>
							<!-- </view> -->
						</view>
						<view class="bottom_list_item" v-if="deptRepair.valueconfig==1" @click="deptSelect">
						  <view class="name">常用科室</view>
						  <view class="value">{{commonDeptName}}</view>
							<text class="newicon newicon-youjiantou icon"></text>
						</view>
			      <view class="bottom_list_item">
			        <view class="name"><text class="required newicon newicon-bitian"></text>联系电话</view>
			        <view class="value no-mar" @click="makePhoneCall(dataForm.phone)">{{dataForm.phone}}</view>
			      </view>
			    </view>
			  </view>
			</view>
			<view class="foot_common_btns fixed-btn">
				<button @click="addInfo" type="default" class="primaryButton btn">保存</button>
			</view>
		</view>
		<view class="bottom-tabs">
			<view class="tabs-list" @click="tabsClick0">
				<img src="/static/img/icon_index.png" v-if="tabsIndex0==0" alt="">
				<img src="/static/img/icon_index_active.png" v-if="tabsIndex0==1" alt="">
				<view class="tabs-title" :class="tabsIndex0==1?'active-class':''">首页 </view>
			</view>
			<view class="tabs-list" @click="tabsClick1">
				<img src="/static/img/icon_incidentList.png" v-if="tabsIndex1==0" alt="">
				<img src="/static/img/icon_incidentList_active.png" v-if="tabsIndex1==1" alt="">
				<view class="tabs-title" :class="tabsIndex1==1?'active-class':''">我的报修</view>
			</view>
			<view class="tabs-list" @click="tabsClick2">
				<img src="/static/img/icon_my.png" v-if="tabsIndex2==0" alt="">
				<img src="/static/img/icon_my_active.png" v-if="tabsIndex2==1" alt="">
				<view class="tabs-title" :class="tabsIndex2==1?'active-class':''">设置</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { SM } from "@/http/http.js"
	import { api_getDictionary, api_incident, api_incident_count, api_incidentTask, api_taskresolve, api_systemConfiguration, api_userSave, api_user, api_department, api_repairScanCode, api_getNotice, api_getCount, api_repairCancel } from "@/http/api.js"
	import repairsFilter from '@/components/repairsFilter.vue';
	import IncidentAttachment from '@/components/IncidentAttachment.vue';
	import { startOfDay, endOfDay, format, add } from 'date-fns'
	import { ref, reactive, computed } from 'vue'
	import { onLoad, onShow, onHide, onPullDownRefresh, onReachBottom, onTabItemTap } from '@dcloudio/uni-app'
	import { filterFormatDate } from '@/filters/filterFormatDate.js'
	import { computedPriorityStyle } from '@/filters/computedPriorityStyle.js'
	import { computedStateStyle } from '@/filters/computedStateStyle.js'
	import { computedCurrentLogOverTime } from '@/filters/computedCurrentLogOverTime.js'
	import { defaultColor } from '@/static/js/theme.js'
	import { useSetTitle } from '@/share/useSetTitle.js'
	import { useMakePhoneCall } from '@/share/useMakePhoneCall.js'
	import { useLoginUserStore } from '@/stores/loginUser'
	import { useIncidentNumStore } from '@/stores/incidentNum'
	import { useIncidentListSearchStore } from '@/stores/incidentListSearch'
	import { repositoryListSearchStore } from '@/stores/repositorySearch'
	import { useSetTabbar } from '@/share/useSetTabbar.js'
	useSetTitle();

	// 数据
	const incidentListSearchStore = useIncidentListSearchStore();
	const { formatDate }  = filterFormatDate();
	const { priorityStyle }  = computedPriorityStyle();
	const { stateStyle }  = computedStateStyle();
	const { currentLogOverTime }  = computedCurrentLogOverTime();

	const { setTabbar }  = useSetTabbar();

	// 主题颜色
	const primaryColor = ref(defaultColor)
	const popup = ref(null);//弹框
	const assignFlag = ref(false);//指派权限
	const qiangdan = ref(false);//接单权限
	const publicRepair = ref({});//公共报修
	const recallValue = ref(null) //撤销原因
	const inputDialog = ref(null)
	const dataItem = ref(null)
	const degreeDictionary = ref(null); //星级字典
	const rowData = ref(null); //选择的数据
	const rate = ref(3);//星级
	const wxdegreeremark = ref(null);//评价内容
	const repairIncident = ref(null);

	// 数据
	const dataInfo = reactive({
	  tabs: [{id: 0, name: '我的报修', value: 'all', num: ''},],
	  tabActiveId: 0,//当前选择的tab
	  list: [],//工单列表
	  idx: 0,//页码
	  hasMore: true,//是否有更多数据
	  isFilter: false,//筛选框开关
	  isAttachment: false,//图片和录音开关
	  incidentId: undefined,
		stateValue:0,
	  evtFilter: {
	    hospital: {},
	    selected: 'todoingAll',
	    area: {id: 0, area: '全部'},
	    category: {id: 0, category: '全部'},
	    acceptDate: [],
	  },//筛选框数据
	})

	const isDept = ref({})
	const isRepair = ref({})
	const isPublic = ref({})
	const noticeData = ref('')
	const repairData = ref({})
	const tabsIndex0 = ref(1)
	const tabsIndex1 = ref(0)
	const tabsIndex2 = ref(0)
	const repositorySearchStore = repositoryListSearchStore();


	const loginUserStore = useLoginUserStore();
	const incidentNumStore = useIncidentNumStore();
	const { makePhoneCall }  = useMakePhoneCall();

	const deptRepair = ref(null)

	const deptData = ref([])

	const branchData = ref([])

	const commonDeptName = ref(null)

	const commonDeptData = ref(null)

	const dataForm = reactive({
		name:loginUserStore.loginUser.user.name,
		account:loginUserStore.loginUser.user.account,
		phone:loginUserStore.loginUser.user.phone,
	  dept: '',
		departmentName:'',
		branch:'',
		commonDeptName:'',
		commonDeptData:'',
		dataType:false
	})

	const operationType = ref(null)

	const userTypes = ref(null)

	const userData = reactive(loginUserStore.loginUser.user)

	const commonDeptDTO = ref([])

	// 是否提交
	const isSubmit = ref(false)

	// 数据
	const dataConfigInfo = reactive({
	  num: 0,
	})

	function tabsClick0(){
		tabsIndex0.value=1
		tabsIndex1.value=0
		tabsIndex2.value=0
		getCount()
		repositorySearchStore.clearRepositoryListSearchData()
	}

	function tabsClick1(){
		tabsIndex0.value=0
		tabsIndex2.value=0
		tabsIndex1.value=1
		// dataInfo.tabActiveId = 0
		dataInfo.stateValue = 0
		getTabs();
	}

	function tabsClick2(){
		tabsIndex2.value=1
		tabsIndex1.value=0
		tabsIndex0.value=0
		getUserInfo()
		repositorySearchStore.clearRepositoryListSearchData()
	}

	// 工单详情
	function toIncidentDetail(data){
	  uni.navigateTo({
	    url: `/pages/repair/repairsDetail?incidentId=${data.id}`
	  })
	}

	// 获取tab选项
	function getTabs(){
	  uni.showLoading({
	    title: "加载中",
	    mask: true,
	  });
		let postData = {
		  "key": 'repair_incident_type',
		  "type": "list",
		};
		if(deptRepair.value.valueconfig==1){
			api_getDictionary(postData).then(res => {
			  res = res || [];
			  repairIncident.value = res.find(i=>i.value=='dept')
				getList(0);
			})
			 dataInfo.tabs = [
				 {id: 0, name: '我的报修', value: 'all', num: ''},
				 {id: 1, name: '科内报修', value: '2', num: ''}
			 ]
		}else{
			getList(0);
			dataInfo.tabs = [
				{id: 0, name: '我的报修', value: 'all', num: ''},
			]
		}
	}

	// 点击tab
	function clickTab(tabId){
	  dataInfo.tabActiveId = tabId;
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 300 // 动画时长，默认300ms
		});
		dataInfo.idx=0
	  getList(0);
	}

	// 点击筛选
	function filterClick(){
	  dataInfo.isFilter = true;
	}

	// 确认筛选
	function conformFilter(evtFilter){
	  dataInfo.stateValue = evtFilter.category;
	  dataInfo.isFilter = false;
	  getList(0);
	}

	// 关闭筛选
	function cancelFilter(){
	  dataInfo.isFilter = false;
	}

	// 点击图片和录音
	function attachmentClick(incidentData){
	  dataInfo.incidentData = incidentData;
	  dataInfo.isAttachment = true;
	}

	// 返回图片和录音
	function knowAttachment(){
	  dataInfo.isAttachment = false;
	}
	
	// 关闭撤销
	function closeDialog(){
		recallValue.value = null
		inputDialog.value.close()
	}
	
	// 确定撤销
	function dialogInputConfirm(){
		if(!recallValue.value){
			uni.showToast({
				icon: 'none',
			  title: '撤销原因不能为空'
			});
			return
		}
		uni.showLoading({
		  title: "加载中",
		  mask: true,
		});
		let query={
			incident: {...dataItem.value, ...{cancelRemark: recallValue.value}}
		}
		api_repairCancel(query).then(res=>{
			uni.hideLoading();
			if(res.state==200){
				inputDialog.value.close()
				recallValue.value = null
				uni.showToast({
					icon: 'none',
				  title: '撤销成功'
				});
				getList(0);
			}else{
				uni.showToast({
					icon: 'none',
				  title: res.msg
				});
			}
		})
	}
	
	// 撤销
	function recall(data){
		dataItem.value = data
		recallValue.value = null
		inputDialog.value.open()
	}
	
	// 评价
	function handler(type, data){
		rowData.value = data
		popup.value.open()
		let postData = {
		  key: "incident_degree",
		  type: "list"
		};
		api_getDictionary(postData).then(res => {
		  res = res || [];
			degreeDictionary.value = res
		})
	}

	function closePop(){
		rate.value = 3
		wxdegreeremark.value = null
		popup.value.close()
	}

	function submit(){
		if(!rate.value){
			uni.showToast({
				icon: 'none',
			  title: '请选择星级'
			});
			return;
		}
		if(!wxdegreeremark.value){
			uni.showToast({
				icon: 'none',
			  title: '请输入评价内容'
			});
			return;
		}
		let rateData = null
		if(rate.value==0){
		   rateData=degreeDictionary.value[0].id
		}else if(rate.value==1){
		   rateData=degreeDictionary.value[4].id
		}else if(rate.value==2){
		   rateData=degreeDictionary.value[3].id
		}else if(rate.value==3){
		   rateData=degreeDictionary.value[2].id
		}else if(rate.value==4){
		   rateData=degreeDictionary.value[1].id
		}else if(rate.value==5){
		   rateData=degreeDictionary.value[0].id
		}
		let postData = {
			...rowData.value,
			wxdegree: rateData==0?degreeDictionary.value[0]:{id: rateData},
			wxdegreeremark: wxdegreeremark.value
		}
		api_taskresolve({incident:postData}).then(res => {
		  uni.hideLoading();
		  if(res.state == 200){
		    getList(0);
		    uni.showToast({
		    	icon: 'none',
		      title: '评价成功',
		    });
		  }else{
		    uni.showToast({
		      icon: 'none',
		      title: res.msg || '请求数据失败！'
		    });
		  }
		})
		closePop()
	}


	// 获取列表信息
	function getList(idx){
	  uni.showLoading({
	    title: "加载中",
	    mask: true,
	  });
	  dataInfo.idx = idx === undefined ? dataInfo.idx : idx;
	  if(dataInfo.idx === 0){
	    dataInfo.list = [];
	  }
	  let postData = {
			"idx": dataInfo.idx,
			"sum": 20,
			incident: {
				requester: dataInfo.tabActiveId == 0 ? { id: loginUserStore.loginUser.user.id } : undefined,
				department: dataInfo.tabActiveId == 1 ? { id: loginUserStore.loginUser.user.dept.id } : undefined,
				statesValues: (dataInfo.stateValue === 'close0' || dataInfo.stateValue === 'close1') ? 'close' : (dataInfo.stateValue || undefined),
				hasWxdegree: dataInfo.stateValue === 'close0' ? 0 : (dataInfo.stateValue === 'close1' ? 1 : undefined),
				repairIncidentType:{
					id:''
				}
			}
	  }
		if(dataInfo.tabActiveId == 1){
			postData.incident.repairIncidentType.id = repairIncident.value.id
		}
		if(JSON.stringify(postData.incident.statesValues) === '{}'){
			delete postData.incident.statesValues
		}
	  api_incident(postData).then(res => {
	    uni.hideLoading();
	    uni.stopPullDownRefresh();
	    if(res.status == 200){
	      let list = res.list || [];
	      if(list.length){
	        dataInfo.hasMore = true;
	        dataInfo.list = dataInfo.idx === 0 ? list : dataInfo.list.concat(list);
	      }else{
	        dataInfo.hasMore = false;
	      }
	    }else{
	      uni.showToast({
	        icon: 'none',
	        title: res.msg || '请求数据失败！'
	      });
	    }
	  })
	  // getCount(postData.incident);
	}

	// 获取列表数量
	// function getCount(incident = {}){
	//   let postData = {
	//     wxCount: 'true',
	//     incidentList: [],
	//   }
	//   dataInfo.tabs.forEach(v => {
	//       postData.incidentList.push({...incident, ...{statusId: v.id || undefined}});
	//   })

	//   api_incident_count(postData).then(res => {
	//     if(res.state == 200){
	//       let myData = res.data || [];
	//       dataInfo.tabs.forEach((v, i) => {
	//           v.num = myData[i];
	//       })
	//     }else{
	//       uni.showToast({
	//         icon: 'none',
	//         title: res.msg || '请求数据失败！'
	//       });
	//     }
	//   })
	// }


	// 知识库
	function repository(){
		uni.navigateTo({
		  url: `/pages/repository/repository?type=view&entranceType=repairs`
		})
	}

	// 报修列表
	function repairsView(type,value){
		tabsIndex0.value = 0
		tabsIndex1.value = 1
		tabsIndex2.value = 0
		dataInfo.tabActiveId = type
		dataInfo.stateValue = value
		dataInfo.evtFilter.category = value
		repositorySearchStore.setRepositoryListSearchData(dataInfo.evtFilter)
		getTabs()
	}

	// 获取报修数量
	function getCount(){
		uni.showLoading({
		  title: "加载中",
		  mask: true,
		});
		api_getCount({}).then(res=>{
			uni.hideLoading();
			repairData.value = res.data
		})
	}

	// 快速报修
	function addRepairs(){
		// if(isDept.value.valueconfig==0 && isPublic.value.valueconfig==0){
		// 	uni.showToast({
		// 		icon: 'none',
		// 	  title: '请先开启科室报修或公共报修'
		// 	});
		// 	return
		// }
		uni.setStorageSync('repairsType','subpage')
		uni.navigateTo({
		  url: '/pages/repair/rapidRep'
		})
	}

	// 扫资产报修
	function scanCodes(){
		uni.showLoading({
		  title: "加载中",
		  mask: true,
		});
		SM().then((res) => {
			api_repairScanCode({
				code:res
			}).then((res2) => {
			  uni.hideLoading();
			  if (res2.state == 200) {
					uni.setStorageSync('repairsType','subpage')
					uni.navigateTo({
					  url: `/pages/repair/rapidRep?property=${res2.data.name}&assetId=${res2.data.id}`
					})
			  } else {
			    uni.showToast({
			      icon: 'none',
			      title: res2.msg || '请求数据失败！'
			    });
			  }
			});
		})
	}


	// 保存
	function addInfo(){
		if(!dataForm.branch){
			uni.showToast({
			  icon: 'none',
			  title: '院区不能为空'
			});
			return
		}

		if(!dataForm.dept){
			uni.showToast({
			  icon: 'none',
			  title: '报修科室不能为空'
			});
			return
		}

		uni.showLoading({
		  title: "加载中",
		  mask: true,
		});
	  isSubmit.value = true
		userData.currentHospital.id = dataForm.branch
		userData.commonDeptName = commonDeptName.value
		userData.dept={
			id:dataForm.dept
		}
		let arr = []
		for(let i of commonDeptDTO.value){
			arr.push({
				dept:i
			})
		}
		let postData = {
			...loginUserStore.loginUser.user,
			// ...userData,
			hospital:{
				id:dataForm.branch
			},
			dept:{
				id:dataForm.dept
			},
			commonDept:commonDeptData.value,
			commonDeptDTO:arr,
			upType: "changeDept"
		}
		api_userSave({
		  user: postData,
		}).then(res => {
		  uni.hideLoading();
			if(res.status == 200){
				loginUserStore.setLoginUser(postData);
				uni.showToast({
				  icon: 'none',
				  title: '保存成功'
				});
			}else{
				uni.showToast({
				  icon: 'none',
				  title: res.msg || '请求数据失败！'
				});
			}
		})
	}

	// 选择院区
	function branchChange(){
		dataForm.dept = null
		dataForm.departmentName = null
		dataForm.commonDeptName = null
		dataForm.commonDeptData = null
		commonDeptName.value = null
		commonDeptData.value = null
		// getRepairTypes()
	}

	// 跳转到科室
	function selectDepartment(){
		if(commonDeptName.value){
			dataForm.dataType = true
		}else{
			dataForm.dataType = false
		}
		uni.setStorageSync('configData',JSON.stringify(dataForm))
	  uni.navigateTo({
	    url: `/pages/searchDept/searchDept?type=config&branchId=${dataForm.branch}`
	  })
	}

	// 获取科室列表
	function getRepairTypes(){
	  uni.showLoading({
	    title: "加载中",
	    mask: true,
	  });
	  let postData = {
			department: {
			  hospital: dataForm.branch?dataForm.branch:loginUserStore.loginUser.user.currentHospital.id,
			},
			idx:0,
			sum:9999
		}
	  api_department(postData).then(res => {
	    uni.hideLoading();
	    res = res.list || [];
	    deptData.value = res.map(v => ({
	      text: v.dept,
	      value: v.id,
	    }));
	  })
	}

	// 获取院区列表
	function getBranch(){
		let arr = loginUserStore.loginUser.hospital.filter(i=>i.parent==undefined)
		branchData.value = arr.map(v => ({
		  text: v.hosName,
		  value: v.id,
		}));
	}

	// 选择常用科室
	function deptSelect(){
		uni.setStorageSync('configData',JSON.stringify(dataForm))
		let data = commonDeptData.value
		// ? commonDeptData.value : loginUserStore.loginUser.user.commonDept
		if(!data){
			data = 'none'
		}
		let deptId = dataForm.branch?dataForm.branch:loginUserStore.loginUser.user.currentHospital.id
		uni.navigateTo({
		  url: `/pages/repair/deptSelect?data=${JSON.stringify(data)}&commonDeptName=${commonDeptName.value}&deptId=${deptId}&type=home`
		})
	}

	function getUserInfo(){
		let user = loginUserStore.loginUser.user
		let userId = null
		if(user.currentHospital.parent){
			userId = user.currentHospital.parent.id
		}else{
			userId = user.currentHospital.id
		}
		let postData = {
		  idx: 0,
		  sum: 999,
		  user: {
				hospital:{
					id:userId
				},
		    name: user.name,
				userTypeIds: userTypes.value ? String(userTypes.value.map(v => v.id)) : undefined
		  }
		}
		api_user(postData).then(res => {
		  if(res.status == 200){
		    let item = res.list.find(i=>i.id==user.id)
				let data = uni.getStorageSync('configData')
				if(operationType.value=='selectDept'){
					if(data){
						data = JSON.parse(data)
						dataForm.name = data.name
						dataForm.account = data.account
						dataForm.phone = data.phone
						dataForm.dept = data.dept
						dataForm.departmentName = data.departmentName
						dataForm.branch = Number(data.branch)
						dataForm.commonDeptName = data.commonDeptName
						dataForm.commonDeptData = data.commonDeptData
						if(data.dataType){
							commonDeptData.value = data.commonDeptData
							commonDeptName.value = data.commonDeptName
						}
					}
				}else if(item){
					if(item.hospital.parent){
						dataForm.branch = item.hospital.parent.id
					}else{
						dataForm.branch = item.hospital.id
					}
					if(item.dept){
						dataForm.dept = item.dept.id
						dataForm.departmentName = item.dept.dept
					}
					// getRepairTypes()
					let id = []
					let name = []
					if(item.commonDeptDTO){
						for(let i of item.commonDeptDTO){
							id.push(i.id)
							name.push(i.dept)
						}
						commonDeptData.value = id.join(',')
						commonDeptName.value = name.join('/')
						dataForm.commonDeptName = commonDeptName.value
						dataForm.commonDeptData = commonDeptData.value
					}
		    }else{
					// let data = uni.getStorageSync('configData')
					// if(data){
					// 	data = JSON.parse(data)
					// 	dataForm.dept = data.dept
					// 	dataForm.branch = data.branch
					// }else{
						if(user.currentHospital.parent){
							dataForm.branch = user.currentHospital.parent.id
						}else{
							dataForm.branch = user.currentHospital.id
						}
						if(user.dept){
							dataForm.dept = user.dept.id
							dataForm.departmentName = user.dept.dept
						}
					// }
					if(user.commonDeptDTO){
						let name = []
						for(let i of user.commonDeptDTO){
							name.push(i.dept)
						}
						commonDeptData.value = user.commonDept
						commonDeptName.value = name.join('/')
						dataForm.commonDeptName = commonDeptName.value
						dataForm.commonDeptData = commonDeptData.value
					}
					// getRepairTypes()
				}
		  }else{
		    uni.showToast({
		      icon: 'none',
		      title: res.msg || '请求数据失败！'
		    });
		  }
		})
	}

	// 获取配置项
	function getConfig(option){
		api_systemConfiguration({
			idx: 0,
			sum: 9999,
		}).then(res=>{
			let data = res.list
			uni.setStorageSync('sysData',JSON.stringify(res.list))
			isDept.value = data.find(i=>i.keyconfig=='deptRepair')
			isRepair.value = data.find(i=>i.keyconfig=='cmdbRepair')
			isPublic.value = data.find(i=>i.keyconfig=='publicRepair')
			deptRepair.value = data.find(i=>i.keyconfig=='deptRepair')
			publicRepair.value = data.find(i=>i.keyconfig=='publicRepair')
			getBranch()
			if(option.type==2){
				tabsIndex0.value = 0
				tabsIndex1.value = 0
				tabsIndex2.value = 1
				if(option.data){
					let data = JSON.parse(option.data)
					commonDeptData.value = data.data.join(',')
					commonDeptName.value = data.name.join('/')
					commonDeptDTO.value = data.name
					dataForm.commonDeptData = data.data.join(',')
					dataForm.commonDeptName = data.name.join('/')
					let data2 = uni.getStorageSync('configData')
					if(data2){
						data2 = JSON.parse(data2)
						dataForm.dept = data2.dept
						dataForm.departmentName = data2.departmentName
						dataForm.branch = data2.branch
					}
				}else{
					getUserInfo()
				}
			}else if(option.type==1){
				tabsIndex0.value = 0
				tabsIndex1.value = 1
				tabsIndex2.value = 0
				getTabs()
			}else{
				tabsIndex0.value = 1
				tabsIndex1.value = 0
				tabsIndex2.value = 0
				getCount()
			}
		})
	}

	onHide(opt=>{
		dataConfigInfo.num = 0
		dataForm.branch = null
	})

	onLoad((option) => {
		console.log(777,option)
		getConfig(option)
		if(uni.getStorageSync('groupData')){
			userTypes.value = JSON.parse(uni.getStorageSync('groupData'))
		}
		dataConfigInfo.num = 1
		if(option && option.configType){
			operationType.value = option.configType
		}

	})

	onReachBottom(() => {
		if(tabsIndex1.value==1){
			dataInfo.idx += 1;
			if (dataInfo.hasMore) {
			  getList(); // 当触底时加载更多数据
			}
		}
	})

	onPullDownRefresh(_=>{
		// uni.stopPullDownRefresh();
	})

	onShow((option) => {
		if(uni.getStorageSync('groupData')){
			userTypes.value = JSON.parse(uni.getStorageSync('groupData'))
		}
		if(dataConfigInfo.num==0){
			getUserInfo()
		}
		repositorySearchStore.clearRepositoryListSearchData()
	})


</script>

<style scoped>
	>>> .uni-popup__wrapper{
		width: 80%;
		padding: 40rpx;
	}
	>>> .uni-data-tree-input{
		width: 100% !important;
	}
	>>> .input-value-border{
		border: none !important;
	}
	>>> .input-value{
		padding:0 !important;
		flex-direction: row-reverse !important;
	}
	>>> .selected-list{
		flex-direction: row-reverse !important;
	}
	>>> .arrow-area{
		display: none !important;
	}
	>>>.uni-data-tree-dialog{
		z-index: 9999;
	}
</style>
<style lang="scss" scoped>
	page{
	  height: calc(100vh - var(--window-bottom));
	}
	.ch-class{
		text-align: center;
		font-size: 32rpx;
	}
	.popup-content{
		padding: 40rpx;
	}
	.incidentList{
	  display: flex;
	  flex-direction: column;
	  justify-content: space-between;
	  .head{
	    height: 88rpx;
	    display: flex;
	    position: fixed;
	    z-index: 99;
	    width: 100%;
	    background-color: #fff;
	    font-size: 30rpx;
	    .tab{
	      flex: 1;
	      display: flex;
	      justify-content: center;
	      align-items: center;
	      border-bottom: 4rpx solid transparent;
	      &.active{
	        color: $uni-primary;
	        border-color: $uni-primary;
	      }
	    }
	    .filter{
	      width: 84rpx;
	      display: flex;
	      justify-content: center;
	      align-items: center;
	      .newicon-shaixuan{
	        font-size: 36rpx;
	        color: #2C2C2C;
	      }
	    }
	  }
	  .body{
	    margin-bottom: var(--window-bottom);
	    margin-top: 88rpx;
	    border-top: 6rpx solid #EBEBEB;
	    .body_item{
	      border-bottom: 8rpx solid #EBEBEB;
				position: relative;
				.sign-style{
					position: absolute;
					right: 0;
					top: -25rpx;
					.img-box{
						position: relative;
						.img-url1{
							width: 70rpx;
							height: 70rpx;
							background-image: url('../../static/img/jiaobiao1.png');
						}
						.img-url2{
							width: 70rpx;
							height: 70rpx;
							background-image: url('../../static/img/jiaobiao2.png');
						}
						.img-name{
							position: absolute;
							right: 8rpx;
							top: 0;
							color: #fff;
							font-size: 24rpx;
						}
					}
				}
	      .body_item_head{
	        word-break: break-all;
	        text-align: justify;
	        text-align: left;
	        margin: 24rpx;
	        font-size: 30rpx;
	      }
	      .body_item_content{
	        border-top: 1rpx solid #D8D8D8;
	        padding: 24rpx 24rpx 24rpx 48rpx;
	        .body_item_content_p{
	          color: #6A6A6A;
	          font-size: 26rpx;
	          display: flex;
	          justify-content: space-between;
	          align-items: center;
	          margin-bottom: 24rpx;
	          &:last-of-type{
	            margin-bottom: 0;
	          }
	          .name{
	            flex: 1;
	          }
	          .status{
	            padding: 4rpx 10rpx;
	            border-radius: 20rpx;
	            background-color: #DBE8FE;
	            font-size: 22rpx;
	            color: #006CF9;
	          }
	          .icon_all{
	            .mic-filled,
	            .image-filled
	            {
	              margin-left: 16rpx;
	            }
	          }
	        }
	      }
	      .body_item_foot{
	        border-top: 1rpx solid #D8D8D8;
	        font-size: 26rpx;
	        padding: 24rpx;
	        .foot_info{
	          display: flex;
	          justify-content: space-between;
	          align-items: center;
	          .phone-filled{
	            margin-left: 5rpx;
	          }
	        }
	      }
	    }
	  }
		.body-height{
			height: 88rpx;
		}
	  .zanwu{
	    box-sizing: border-box;
	    margin-bottom: var(--window-bottom);
	    margin-top: 88rpx;
	    border-top: 6rpx solid #EBEBEB;
	    height: calc(100vh - var(--window-bottom) - 88rpx);
	    display: flex;
	    justify-content: center;
	    background-color: #F7F7F7;
	    .newicon-zanwu{
	      font-size: 256rpx;
	      color: #D6D6D6;
	      margin-top: 140rpx;
	    }
	  }
		.popup-content{
			.form_item{
			  display: flex;
			  align-items: center;
			  padding-top: 24rpx;
			  min-height: 86rpx;
				position: relative;
				.chunk{
					width: 100%;
					height: 70rpx;
					line-height: 70rpx;
					text-align: center;
					background: #F7F8FA;
					box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
					border-radius: 10rpx;
				}
			  &.column{
			    height: auto;
			    flex-direction: column;
			    align-items: flex-start;
					.import-rep{
						padding: 5rpx 10rpx;
						border-radius: 50rpx;
						background: #d1fcd5;
						color: #49b856;
						font-size: 24rpx;
					}
			    .title{
			      margin-right: 0;
			    }
					.title-width{
						width: 100%;
					}
					.title-fl-sb{
						display: flex;
						justify-content: space-between;
						width: 100%;
					}
			    .value{
			      margin-top: 10rpx;
			      // padding-left: 20rpx;
			      box-sizing: border-box;
			    }
			    .tips{
			      padding: 24rpx;
			      text-align: center;
			      font-size: 22rpx;
			      color: #909399;
			      width: 100%;
			      box-sizing: border-box;
			    }
			  }
			  .title{
			    font-size: 26rpx;
			    display: flex;
			    align-items: center;
			    margin-right: 12rpx;
			    flex-shrink: 0;
			    &.select{
			      width: calc(5em + 20rpx);
			    }
			  }
			  .value{
			    width: 100%;
			    &.category{
			      width: 100%;
			      display: flex;
			      justify-content: space-between;
			      align-items: center;
			      .categoryName{
			        font-size: 26rpx;
			        color: #555;
			        flex: 1;
			      }
			      .newicon-weibiaoti2010104{
			        color: $uni-primary;
			        margin-left: 24rpx;
			      }
			    }
			    .imgTips{
			      color: #909399;
			      font-size: 22rpx;
						margin-top: 10rpx;
			    }
			  }
				.item-input{
					border: 1px solid #DBDBDB;
					height: 70rpx;
					line-height: 70rpx;
					padding: 0 10rpx;
					border-radius: 4rpx;
					width: 100%;
					font-size: 14px;
				}
				.icon{
					position: absolute;
					right: 20rpx;
					color: #49b856;
				}
			}
		}

	}

	.home{
		// height: 100vh;
		// padding: 20rpx;
		background: #fff;
		.home_item{
			padding: 20rpx;
			box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
			border-radius: 10rpx;
			margin-bottom: 30rpx;
			.title{
			  font-size: 26rpx;
			  color: $uni-primary;
			  padding-left: 18rpx;
			  position: relative;
				margin-bottom: 20rpx;
			  &:before{
			    content: '';
			    width: 8rpx;
			    height: 25rpx;
			    background-color: $uni-primary;
			    position: absolute;
			    left: 0;
			    top: 50%;
			    transform: translateY(-50%);
			  }
			}
			.content{
				display: flex;
				align-items: center;
				justify-content: space-around;
				text-align: center;
				.con-title{
					color: #949494;
					font-size: 24rpx;
					margin-bottom: 15rpx;
				}
				.con-value{
					color: #000;
					font-size: 50rpx;
				}
				.con-value-gr{
					color: #49B856;
					font-size: 50rpx;
				}
			}
			.uni-noticebar{
				margin: 0 !important;
				padding: 0 !important;
			}
		}
		.home-disp{
			display: flex;
			justify-content: space-around;
			.bottom-left{
				height: 380rpx;
				background: linear-gradient( 269deg, #54B99C 0%, #7AC481 100%);
				border-radius: 10rpx;
				flex: 1.5;
				margin-right: 20rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #FFFFFF;
				margin-top: 20rpx;
				.bottom-left-box{
					text-align: center;
					.icon{
						font-size: 100rpx;
						position: relative;
						top: -20rpx;
					}
				}
			}
			.mar-t-20{
				margin-top: 20rpx;
			}
			.bottom-right{
				flex: 2;
				.bottom-right-item{
					height: 175rpx;
					background: #FFFFFF;
					border-radius: 10rpx;
					border: 2rpx solid #6FC073;
					margin-bottom: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: relative;
					.icon{
						font-size: 50rpx;
						color: #6FC073;
						position: absolute;
						left: 60rpx;
					}
					.name1{
						font-size: 32rpx;
						position: absolute;
						right: 50rpx;
					}
					.name2{
						font-size: 32rpx;
						position: absolute;
						right: 80rpx;
					}
				}
			}
		}
		.bottom-tabs{
			border-top:1px solid rgba(0, 0, 0, 0.33);
			background: #fff;
			position: fixed;
			width: 100%;
			left: var(--window-left);
			right: var(--window-right);
			display: flex;
			bottom: 0;
			.tabs-list{
				height: 90rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				flex: 1;
				font-size: 0;
				text-align: center;
				-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
				img{
					width: 48rpx;
					height: 48rpx;
				}
				.tabs-title{
					color: rgb(136, 136, 136);
					font-size: 20rpx;
					line-height: normal;
				}
				.active-class{
					color: #49B856;
				}
			}
		}
	}
	.mine{
	  height: 100%;
	  display: flex;
	  flex-direction: column;
	  justify-content: space-between;
	  .phone-filled{
	    margin-right: 5rpx;
	  }
	  .newicon-xinjian2,
		.newicon-zhishiku{
	    margin-right: 10rpx;
	  }
	  .body{
	    width: 714rpx;
	    height: 100%;
	    margin: 16rpx auto var(--window-bottom) auto;
	    box-sizing: border-box;
	    border-radius: 8rpx;
	    .top{
	      padding: 30rpx;
	      background-color: #fff;
	      .top_name{
	        font-size: 28rpx;
	        font-weight: bold;
	      }
	      .top_count{
	        margin-top: 45rpx;
	        display: flex;
	        align-items: center;
	        justify-content: space-between;
	        .top_count_item{
	          text-align: center;
	          .name{
	            color: #949494;
	            font-size: 22rpx;
	          }
	          .value{
	            font-size: 50rpx;
	            font-weight: bold;
	            margin-top: 15rpx;
	          }
	        }
	      }
	    }

	    .bottom{
	      background-color: #fff;
	      margin-top: 15rpx;
	      .bottom_name{
	        font-size: 26rpx;
	        color: $uni-primary;
	        padding: 21rpx 24rpx;
	      }
	      .bottom_list{
	        .bottom_list_item{
	          border-top: 1rpx solid #DEDEDE;
	          padding: 30rpx 30rpx 30rpx 30rpx;
	          display: flex;
	          justify-content: space-between;
	          align-items: center;
	          font-size: 24rpx;
						position: relative;
	          .value{
	            max-width: 380rpx;
	            color: #333;
	            display: flex;
	            align-items: center;
	            text-align: justify;
							margin-right: 30rpx;
	          }
						.no-mar{
							margin-right:0 !important;
						}
						.icon{
							position: absolute;
							right: 20rpx;
						}
	        }
	      }
	    }
	  }
		.fixed-btn{
			position: fixed;
			bottom: 100rpx;
			left: 0;
		}
	}
</style>