<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">{{searchData.hospital ? searchData.hospital.hosName : ''}}</text>
      </view>
      
      <view class="tabs">
        <view class="tab" :class="{active: tab.value === searchData.selected}" v-for="tab in tabs" :key="tab.value" @click="clickTab(tab)">{{tab.name}}<text class="newicon newicon-xuanzejiaobiao"></text></view>
      </view>
      
      <view class="area" @click="clickPageRouter('area')">
        <text class="name">楼栋</text>
        <text class="value ellipsis">{{searchData.area ? searchData.area.buildingName : ''}}</text>
      </view>
      
      <view class="category">
        <text class="name">故障现象</text>
        <text class="value flex-1">
					<uni-data-picker @change="categoryChange" v-model="searchData.categoryValue" :localdata="pageData.categoryList" popup-title="请选择故障信息" :map="{text:'category',value:'id'}">
						
					</uni-data-picker>
				</text>
      </view>
      
      <view class="acceptDate" @click="changeIsShowDate()">
        <text class="name">登记时间</text>
        <text class="value ellipsis" v-if="searchData.acceptDate.length">{{searchData.acceptDate[0] || ''}}至{{searchData.acceptDate[1] || ''}}</text>
        <text class="value ellipsis" v-else>全部</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>
    <PyhRdtpicker
        :show="isShowDate"
        :start="start"
        :end="end"
        @showchange="showDatechange"
        :value="searchData.acceptDate"
        @change="bindDateChange"
        :themeColor="primaryColor"
    ></PyhRdtpicker>
  </view>
  <view class="container" @touchmove.stop.prevent v-else-if="pageData.pageRouter === 'area'">
    <view class="container_form">
      <view class="hospital">
        <text class="name">楼栋</text>
      </view>
      
      <scroll-view scroll-y class="areas">
        <view class="areas_item" v-for="area in pageData.areaList" :key="area.id" @click="clickArea(area)">{{area.buildingName}}</view>
      </scroll-view>
    </view>
    <view class="container_foot">
      <view class="foot_btns">
        <view class="cancel" @click="clickPageRouter('default')">取消</view>
      </view>
    </view>
  </view>
<!--  <view class="container" @touchmove.stop.prevent v-else-if="pageData.pageRouter === 'category'">
    <view class="container_form">
      <view class="hospital">
        <text class="name">故障现象</text>
      </view>
      
      <scroll-view scroll-y class="categorys">
        <view class="categorys_item" v-for="category in pageData.categoryList" :key="category.id" @click="clickCategory(category)">{{category.category}}</view>
      </scroll-view>
    </view>
    <view class="container_foot">
      <view class="foot_btns">
        <view class="cancel" @click="clickPageRouter('default')">取消</view>
      </view>
    </view>
  </view> -->
  <view class="mask" @touchmove.stop.prevent></view>
  <view class="line" @touchmove.stop.prevent></view>
  
</template>

<script setup>
  import PyhRdtpicker from '@/components/PyhRdtpicker/PyhRdtpicker.vue';
  import { defineEmits, ref, reactive, defineProps } from 'vue'
  import { startOfYear, endOfYear, format, add } from 'date-fns'
  import { onLoad } from '@dcloudio/uni-app'
  import { useLoginUserStore } from '@/stores/loginUser'
  import { api_area, getFetchDataList, api_incidentcategory } from "@/http/api.js"
  import { defaultColor } from '@/static/js/theme.js'
  import { transform } from '@/utils/index.js'
	
  const emit = defineEmits(['cancelEmit', 'confirmEmit']);
  const { evt } = defineProps({
    evt: {
      type: Object,
      required: true,
    },
  });
  const loginUserStore = useLoginUserStore();
  
  // 主题颜色
  const primaryColor = ref(defaultColor)
  
  // 登记时间
  const isShowDate = ref(false)
  
  const start = ref(format(startOfYear(add(new Date(), { years: -5})), 'yyyy-MM-dd'));
  const end = ref(format(endOfYear(add(new Date(), { years: 0})), 'yyyy-MM-dd'));
  
  const tabs = reactive([
    // { name: '全部事件', value: 'all' },
    { name: '待我处理', value: 'todoingAll' },
    { name: '与我关联', value: 'owns' },
    { name: '由我解决', value: 'resolve' },
  ])
  
  // 页面数据
  const pageData = reactive({
    pageRouter: 'default',
    areaList: [],
    categoryList: [],
  });
  
  const searchData = reactive({
    hospital: {},
    selected: 'todoingAll',
    area: {id: 0, area: '全部'},
    category: {},
		categoryValue:{},
    acceptDate: [],
  })
  
  // 显示登记时间
  function showDatechange(){
    isShowDate.value = !isShowDate.value;
  }
  
  // 登记时间确定
  function bindDateChange(e){
    console.log(e);
    searchData.acceptDate = e;
  }
  
  // 点击tab
  function clickTab(tab){
    searchData.selected = tab.value;
  }
  
  // 点击区域
  function clickArea(area){
    pageData.pageRouter = 'default';
    searchData.area = area;
  }
  
  // 点击故障现象
  function clickCategory(category){
    pageData.pageRouter = 'default';
    searchData.category = category;
  }
  
  // 清空
  function clear(){
    setHospital();
    searchData.selected = 'todoingAll';
    searchData.area = {id: 0, area: '全部'};
    searchData.category = {};
    searchData.acceptDate = [];
		searchData.categoryValue = {};
    console.log(searchData.acceptDate)
  }
  
  // 取消
  function cancel(){
    emit('cancelEmit')
  }
  
  // 确认
  function confirm(){
    emit('confirmEmit', searchData);
  }
  
  // 设置院区
  function setHospital(){
    if (loginUserStore.loginUser.user.duty) {
      // 当前的所属责任科室
      searchData.hospital = {
        id: loginUserStore.loginUser.user.duty.branch,
        hosName: loginUserStore.loginUser.user.duty.branchName
      };
    } else if (loginUserStore.loginUser.user.branch) {
      // 当前的所属院区
      searchData.hospital = {
        id: loginUserStore.loginUser.user.branch.id,
        hosName: loginUserStore.loginUser.user.branch.hosName
      };
    }
  }
  
	// 选择故障现象
	function categoryChange(val){
		searchData.category = val.detail.value[val.detail.value.length-1]
	}
	
  // 获取楼栋列表
  function getAreaList(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    
    // let postData = {
    //   idx: 0,
    //   sum: 9999,
    //   area: {
    //     branch: searchData.hospital.id,
    //   },
    // }
    // api_area(postData).then(res => {
    //   uni.hideLoading();
    //   if(res.status == 200){
    //     let list = res.list || [];
    //     pageData.areaList = [{ id: 0, area: '全部' }, ...list];
    //   }else{
    //     uni.showToast({
    //       icon: 'none',
    //       title: res.msg || '请求数据失败！'
    //     });
    //   }
    // })
		
		let postData = {
		  idx: 0,
		  sum: 9999,
			building:{
				hosId: null
			}
		};
		if(loginUserStore.loginUser.user.currentHospital.parent){
			postData.building.hosId = loginUserStore.loginUser.user.currentHospital.parent.id
		}else{
			postData.building.hosId = loginUserStore.loginUser.user.currentHospital.id
		}
		getFetchDataList("simple/data", "building", postData)
		  .then((res) => {
				uni.hideLoading();
		    let list = res.list || [];
		    pageData.areaList = [{ id: 0, area: '全部' }, ...list];
		  });
  }
  
  // 获取故障现象列表
  function getCategoryList(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    
    let postData = {
    		hasThird:true,
    		category: {
    			dutyIds: loginUserStore.loginUser.user.currentHospital.id,
    		},
    	}
    // if(loginUserStore.loginUser.user.duty){
    //   postData.incidentcategory.dutyToCategory = loginUserStore.loginUser.user.duty.id;
    // }else if(loginUserStore.loginUser.user.branch){
    //   postData.incidentcategory.selectType = 'one';
    // }
    
    // api_incidentcategory(postData).then(res => {
    //   uni.hideLoading();
    //   if(res.status == 200){
    //     let list = res.list || [];
    //     pageData.categoryList = [{ id:0, category: '全部' }, ...list];
    //   }else{
    //     uni.showToast({
    //       icon: 'none',
    //       title: res.msg || '请求数据失败！'
    //     });
    //   }
    // })
		
		
		 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 'area':
        getAreaList();
      break;
    }
  }
  
  // 显示登记时间
  function changeIsShowDate(type){
    isShowDate.value = true;
  }
  
  onLoad((option) => {
		getCategoryList()
    // searchData.hospital = evt.hospital;
    for (let i = 0; i < loginUserStore.loginUser.menu.length; i++) {
      if (loginUserStore.loginUser.menu[i].link == "all") {
        tabs.splice(0, 0 , { name: '全部事件', value: 'all' });
      }
    }
    setHospital();
    searchData.selected = evt.selected;
    searchData.area = evt.area;
    searchData.category = evt.category;
		searchData.categoryValue = evt.categoryValue;
    searchData.acceptDate = evt.acceptDate;
  })
</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;
  }
  
  .hospital{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 32rpx 24rpx 24rpx;
    background-color: #fff;
    .name{
      font-size: 30rpx;
      flex-shrink: 0;
      margin-right: 24rpx;
    }
    .value{
      font-size: 26rpx;
      color: #5DAAB6;
    }
  }
  
  .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;
    }
		.flex-1{
			flex: 3;
		}
  }
  
  .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>