<template>
  <view class="container" @touchmove.stop.prevent>
    <view class="container_form">
      <view class="hospital">
        <view class="name">开始通话时间区间</view>
				<view>
					<uni-datetime-picker v-model="searchData.datetimerange" 
					type="datetimerange" rangeSeparator="至" @change="changeDate($event)"/>
				</view>
      </view>

      <view class="hospital">
        <view class="name">已接/未接</view>
        <view class="tabs">
          <view class="tab" :class="{active: tab.value === searchData.selected1}" v-for="tab in tabs1" :key="tab.value" @click="clickTab(tab,1)">{{tab.name}}<text class="newicon newicon-xuanzejiaobiao"></text></view>
        </view>
      </view>
			
			<view class="hospital">
			  <view class="name">呼入/呼出</view>
			  <view class="tabs">
			    <view class="tab" :class="{active: tab.value === searchData.selected2}" v-for="tab in tabs2" :key="tab.value" @click="clickTab(tab,2)">{{tab.name}}<text class="newicon newicon-xuanzejiaobiao"></text></view>
			  </view>
			</view>
			
			<view class="category">
			  <text class="name">主叫号码</text>
				<input class="uni-input" v-model="searchData.dialing"  placeholder="请输入主叫号码" />
			</view>
			
			<view class="category">
			  <text class="name">被叫号码</text>
			  <input class="uni-input" v-model="searchData.called"  placeholder="请输入被叫号码" />
			</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>
  </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 { subMonths, startOfMonth, endOfMonth, format, add, setDate } 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 tabs1 = reactive([
    { name: '全部', value: 'all' },
    { name: '已接', value: '1' },
    { name: '未接', value: '0' },
  ])
	
	const tabs2 = reactive([
	  { name: '全部', value: 'all' },
	  { name: '呼入', value: '1' },
	  { name: '呼出', value: '0' },
	])
	
  // 页面数据
  const pageData = reactive({
    pageRouter: 'default',
    areaList: [],
    categoryList: [],
		deptList:[],
		selectBuildingList:[],
  });
	
	// 是否开启跨科查看
	const showDept = ref(false);

  const searchData = reactive({
    called: null,
		dialing: null,
		datetimerange:[],
    selected1: '-1',
		selected2: '-1'
  })

  // 显示登记时间
  function showDatechange(){
    isShowDate.value = !isShowDate.value;
  }

  // 登记时间确定
  function bindDateChange(e){
    console.log(e);
    searchData.acceptDate = e;
  }

  // 点击tab
  function clickTab(tab, type){
		if(type==1){
			searchData.selected1 = tab.value;
		}else{
			searchData.selected2 = tab.value;
		}
  }
	
	function changeDate(e){
		console.log(123, e)
	}
	
  // 清空
  function clear(){
    searchData.called = null
    searchData.dialing = null
    setDefaultDate()
    searchData.selected1 = 'all'
    searchData.selected2 = 'all'
  }

  // 取消
  function cancel(){
    emit('cancelEmit')
  }

  // 确认
  function confirm(){
    emit('confirmEmit', searchData);
  }

  // 页面路由跳转
  function clickPageRouter(type){
    pageData.pageRouter = type;
    switch(type){
      case 'area':
        getAreaList();
      break;
    }
  }

  // 显示登记时间
  function changeIsShowDate(type){
    isShowDate.value = true;
  }
	
	// 设置默认时间
	function setDefaultDate(){
		// 获取当前日期
		const today = new Date();
		// 获取上个月的第一天
		const startOfLastMonth = startOfMonth(subMonths(today, 1));
		// 获取上个月的最后一天
		const endOfLastMonth = endOfMonth(subMonths(today, 1));
		
		searchData.datetimerange = [format(startOfLastMonth, 'yyyy-MM-dd HH:mm:ss'),format(endOfLastMonth, 'yyyy-MM-dd HH:mm:ss')]
	}
	
  onLoad((option) => {
		console.log(123, evt)
		if(evt.datetimerange.length>0){
			searchData.datetimerange = evt.datetimerange
		}else{
			setDefaultDate()
		}
		let data = loginUserStore.loginUser.infoPermission
		pageData.deptList = data.dutyList
		searchData.selected1 = evt.selected1;
		searchData.selected2 = evt.selected2;
		searchData.called = evt.called;
		searchData.dialing = evt.dialing;
  })
</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{
    padding: 32rpx 24rpx 24rpx;
    background-color: #fff;
    .name{
      font-size: 30rpx;
      flex-shrink: 0;
			margin-bottom: 20rpx;
    }
    .value{
      font-size: 26rpx;
      color: #5DAAB6;
    }
  }

  .areas{
    flex: 1;
    min-height: 0;
    margin-top: 20rpx;
		padding-top: 20rpx;
    background-color: #fff;
		.view-box{
			display: flex; 
			flex-wrap: wrap;
			.areas_item{
				width: 30%;
				height: 80rpx;
				background: #F6F6F6;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 10rpx;
				margin: 0 10rpx 20rpx 10rpx;
			}
			.ActiveClass{
				background: #FFEAE7;
				color: #D95D4D;
				border: 1px solid #A13728;
				box-sizing: border-box;
			}
		}
    
  }

  .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;
      }
			.submit{
				color: #49b856;
				border-left: 1px solid #BFBFBF;
			}
      .confirm{
        flex: 1;
        font-size: 32rpx;
        padding: 24rpx;
        background-color: $uni-primary;
        display: flex;
        justify-content: center;
        color: #fff;
      }
    }
  }
}
</style>