<template>
  <view class="incidentList">
    <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">
        <view class="body_item_head ellipsis-multiline" @click="attachmentClick(data)">
					{{data.dTMFA}}
					<uni-icons v-if="data.dTMFA && data.dTMFB" type="arrow-right" class="arrow-right" :size="18" :color="primaryColor"></uni-icons>
					{{data.dTMFB}}
					<uni-icons v-if="data.path" type="mic-filled" class="mic-filled" :size="18" :color="primaryColor"></uni-icons>
        </view>
				 
        <view class="body_item_content">
					<view class="body_item_content_p">
						<text class="name ellipsis">开始时间：{{data.responseTime}}</text>
					</view>
					<view class="body_item_content_p">
						<text class="name ellipsis">结束时间：{{data.overTime}}</text>
					</view>
					<view class="body_item_content_p">
						<text class="name ellipsis">通话时长：{{data.longTime || 0}} 秒</text>
						<view class="type">
							{{data.callState == 1?'已接':'未接'}}/
							{{data.callType == 1?'呼入':'呼出'}}
						</view>
					</view>
        </view>
      </view>
    </view>
    <view class="zanwu" v-else>
      <text class="newicon newicon-zanwu"></text>
    </view>
    <callFilter v-if="dataInfo.isFilter" @cancelEmit="cancelFilter" @confirmEmit="conformFilter" :evt="dataInfo.evtFilter"></callFilter>
    <IncidentAttachment v-if="dataInfo.isAttachment" @knowEmit="knowAttachment" :incidentData="dataInfo.incidentData"></IncidentAttachment>
    <RequireAttachment v-if="dataInfo.isRequireAttachment" @knowEmit="knowRequireAttachment" :incidentData="dataInfo.incidentData"></RequireAttachment>
  </view>
</template>

<script setup>
  import callFilter from '@/components/callFilter.vue';
  import IncidentAttachment from '@/components/IncidentAttachment.vue';
  import RequireAttachment from '@/components/RequireAttachment.vue';
  import { subMonths, startOfMonth, endOfMonth, format, add, setDate } from 'date-fns'
  import { ref, reactive, computed } from 'vue'
  import { onLoad, onUnload, onHide, onPullDownRefresh, onReachBottom, onTabItemTap } from '@dcloudio/uni-app'
  import { api_callLog, api_getDictionary, api_incident, api_incident_count, api_incidentTask } from "@/http/api.js"
  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 { useSetTabbar } from '@/share/useSetTabbar.js'
  import { useLoginUserStore } from '@/stores/loginUser'
  import { useIncidentNumStore } from '@/stores/incidentNum'
  import { useIncidentListSearchStore } from '@/stores/incidentListSearch'
	import { useHandlerStore } from '@/stores/handler'

  useSetTitle();
  const loginUserStore = useLoginUserStore();
  const incidentNumStore = useIncidentNumStore();
  const incidentListSearchStore = useIncidentListSearchStore();
  const { formatDate }  = filterFormatDate();
  const { priorityStyle }  = computedPriorityStyle();
  const { stateStyle }  = computedStateStyle();
  const { currentLogOverTime }  = computedCurrentLogOverTime();
  const { makePhoneCall }  = useMakePhoneCall();
  const { setTabbar }  = useSetTabbar();
	const handlerStore = useHandlerStore();

  // 主题颜色
  const primaryColor = ref(defaultColor)

  // 数据
  const dataInfo = reactive({
    tabs: [{id: 0, name: '全部', value: 'all', num: ''}],
    tabActiveId: 0,//当前选择的tab
    list: [],//工单列表
    idx: 0,//页码
    hasMore: true,//是否有更多数据
    isFilter: false,//筛选框开关
    isAttachment: false,//图片和通话录音开关
    isRequireAttachment: false,//报修录音开关
    incidentId: undefined,
    evtFilter: {
      called: null,
      dialing: null,
      datetimerange:[],
      selected1: 'all',
      selected2: 'all'
    },//筛选框数据
  })
	
	// 设置默认时间
	function setDefaultDate(){
		// 获取当前日期
		const today = new Date();
		// 获取上个月的第一天
		const startOfLastMonth = startOfMonth(subMonths(today, 1));
		// 获取上个月的最后一天
		const endOfLastMonth = endOfMonth(subMonths(today, 1));
		
		dataInfo.evtFilter.datetimerange = [format(startOfLastMonth, 'yyyy-MM-dd HH:mm:ss'),format(endOfLastMonth, 'yyyy-MM-dd HH:mm:ss')]
	}
	
  // 点击筛选
  function filterClick(){
    dataInfo.isFilter = true;
  }

  // 确认筛选
  function conformFilter(evtFilter){
		console.log(444, evtFilter)
    dataInfo.evtFilter = evtFilter;
    dataInfo.isFilter = false;
    getList(0);
  }

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

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

  // 返回图片和录音
  function knowAttachment(){
    dataInfo.isAttachment = false;
  }

  // 点击报修录音
  function attachmentRequireClick(incidentData){
    dataInfo.incidentData = incidentData;
    dataInfo.isRequireAttachment = true;
  }

  // 返回报修录音
  function knowRequireAttachment(){
    dataInfo.isRequireAttachment = false;
  }

  // 获取列表信息
  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": 10,
        "callLog": {
          dTMFA: dataInfo.evtFilter.dialing || undefined,
					dTMFB: dataInfo.evtFilter.called || undefined,
					callState: dataInfo.evtFilter.selected1 || undefined,
					callType: dataInfo.evtFilter.selected2 || undefined,
					startTime: dataInfo.evtFilter.datetimerange[0] || undefined,
					endTime: dataInfo.evtFilter.datetimerange[1] || undefined,
        }
    }
		if(dataInfo.evtFilter.selected1!='all'){
			postData.callLog.callState = dataInfo.evtFilter.selected1
		}else{
			delete postData.callLog.callState
		}
		if(dataInfo.evtFilter.selected2!='all'){
			postData.callLog.callType = dataInfo.evtFilter.selected2
		}else{
			delete postData.callLog.callType
		}
    api_callLog(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 || '请求数据失败！'
        });
      }
    })
  }


  onLoad((option) => {
		setDefaultDate();
    getList(0);
  })

	onUnload(() => {
    dataInfo.isFilter = false;
  })

	onHide(() => {
	  dataInfo.isFilter = false;
	})

  onTabItemTap(e => {
    // onLoadFn();
  })

  onPullDownRefresh(() => {
    getList(0)
  })

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

<style lang="scss" scoped>
page{
  height: calc(100vh - var(--window-bottom));
}
.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;
			position: absolute;
			right: 0;
			top: 30rpx;
      .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;
      .body_item_head{
        word-break: break-all;
        text-align: justify;
        text-align: left;
        margin: 24rpx;
        font-size: 30rpx;
      }
      .body_item_content{
				// display: flex;
        border-top: 1rpx solid #D8D8D8;
        padding: 24rpx;
				.type{
					margin-left: 40rpx;
				}
        .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;
          }
        }
      }
    }
  }
  .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;
    }
  }
}
</style>
