<template>
  <view class="container" @touchmove.stop.prevent v-if="pageData.pageRouter === 'default'">
    <view class="container_form">
      <view class="hospital">
        <text class="name">标题</text>
        <text class="value ellipsis">
					<input class="uni-input" v-model="searchData.title" placeholder="请输入标题" />
				</text>
      </view>
      <view class="category" v-if="props.viewType!='repairs'">
        <text class="name">故障现象</text>
        <text class="value">
					<uni-data-picker @change="categoryChange" v-model="searchData.category" :localdata="pageData.categoryList" popup-title="请选择故障信息" :map="{text:'category',value:'id'}">
						
					</uni-data-picker>
				</text>
      </view>
    </view>
    <view class="container_foot">
      <view class="clear" @click="clear">清除选项</view>
      <view class="foot_btns">
        <view class="cancel" @click="cancel">取消</view>
        <view class="confirm" @click="confirm">确认</view>
      </view>
    </view>
  </view>
  <view class="mask" @touchmove.stop.prevent></view>
  <view class="line" @touchmove.stop.prevent></view>
  
</template>

<script setup>
  import { defineEmits, ref, reactive, defineProps } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { useLoginUserStore } from '@/stores/loginUser'
	import { repositoryListSearchStore } from '@/stores/repositorySearch'
  import { api_area, api_incidentcategory } from "@/http/api.js"
  import { transform } from '@/utils/index.js'
		
  const emit = defineEmits(['cancelEmit', 'confirmEmit']);
  const loginUserStore = useLoginUserStore();
	const repositorySearchStore = repositoryListSearchStore();
  
	const props = defineProps({
		viewType:null
	})
	
  // 页面数据
  const pageData = reactive({
    pageRouter: 'default',
    areaList: [],
    categoryList: [],
  });
	
  const searchData = reactive({
		title:'',
    hospital: {},
    selected: 'todoingAll',
    area: {id: 0, area: '全部'},
    category: {},
    acceptDate: [],
		categoryChangeData:[]
  })
  
  // 清空
  function clear(){
    searchData.category = {};
    searchData.acceptDate = [];
		searchData.title = '';
		searchData.categoryChangeData = [];
		repositorySearchStore.clearRepositoryListSearchData()
  }
  
  // 取消
  function cancel(){
    emit('cancelEmit')
  }
  
  // 确认
  function confirm(){
		repositorySearchStore.setRepositoryListSearchData(searchData)
    emit('confirmEmit', searchData);
  }

  function categoryChange(val){
		searchData.categoryChangeData = val.detail.value
	}
	
  // 获取故障现象列表
  function getCategoryList(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    
	 let postData = {
			hasThird:true,
			category: {
				dutyIds: loginUserStore.loginUser.user.currentHospital.id,
			},
		}
    api_incidentcategory(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        let list = res.data || [];
				list = list.map(i=> ({...i, parentid: i.parent?.id}))
				pageData.categoryList = transform(list, 'id', 'parentid')
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败!'
        });
      }
    })
  }
	
  // 页面路由跳转
  function clickPageRouter(type){
    pageData.pageRouter = type;
    switch(type){
      case 'category':
        getCategoryList('one');
      break;
    }
  }
	
  onLoad((option) => {
		getCategoryList()
		let data = repositorySearchStore.repositorySearch.data
    searchData.category = data && data.category;
		searchData.title = data && data.title;
		searchData.categoryChangeData = data && data.categoryChangeData
  })
</script>

<style lang="scss" scoped>
.mask{
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 999;
}
.line{
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  height: 8rpx;
  width: 100%;
  background-color: #EBEBEB;
}
.container{
  position: fixed;
  left: 125rpx;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  background-color: #F7F7F7;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  
  .container_form{
    height: 100%;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
		.item{
			padding: 32rpx;
		}
  }
  
  .hospital{
    display: flex;
		align-items: center;
    padding: 32rpx 24rpx 24rpx;
    background-color: #fff;
    .name{
      font-size: 28rpx;
      flex-shrink: 0;
      margin-right: 24rpx;
    }
    .value{
			width: 100%;
      font-size: 22rpx;
			.uni-input{
				font-size: 26rpx;
			}
    }
  }
  
  .areas{
    flex: 1;
    min-height: 0;
    margin-top: 16rpx;
    background-color: #fff;
    .areas_item{
      padding: 24rpx;
      border-bottom: 1rpx solid #DEDEDE;
    }
  }
  
  .categorys{
    flex: 1;
    min-height: 0;
    margin-top: 16rpx;
    background-color: #fff;
    .categorys_item{
      padding: 24rpx;
      border-bottom: 1rpx solid #DEDEDE;
    }
  }
  
  .tabs{
    margin-top: 16rpx;
    background-color: #FBFBFB;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 16rpx 0;
    padding: 24rpx 16rpx;
    .tab{
      width: 180rpx;
      height: 60rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #F7F7F7;
      font-size: 28rpx;
      position: relative;
      .newicon-xuanzejiaobiao{
        opacity: 0;
        position: absolute;
        right: 0;
        bottom: 0;
        font-size: 38rpx;
        color: #53B9BB;
      }
      &.active{
        background-color: rgba(149, 220, 231, 0.30);
        .newicon-xuanzejiaobiao{
          opacity: 1;
        }
      }
    }
  }
  
  .area,
  .category,
  .acceptDate{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24rpx;
    background-color: #fff;
    margin-top: 24rpx;
    .name{
      font-size: 28rpx;
      flex-shrink: 0;
      margin-right: 24rpx;
    }
		.value{
			flex:1;
		}
  }
  
  .container_foot{
    .clear{
      padding: 24rpx;
      font-size: 28rpx;
      background-color: #fff;
      margin: 0 16rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .foot_btns{
      margin-top: 24rpx;
      display: flex;
      border-top: 1rpx solid #BFBFBF;
      .cancel{
        flex: 1;
        background-color: #fff;
        font-size: 32rpx;
        padding: 24rpx;
        display: flex;
        justify-content: center;
      }
      .confirm{
        flex: 1;
        font-size: 32rpx;
        padding: 24rpx;
        background-color: $uni-primary;
        display: flex;
        justify-content: center;
        color: #fff;
      }
    }
  }
}
</style>