<template>
  <view class="workHourManagementTwo">
    <view class="head">
      <text class="one">{{dataInfo.parentName}}<uni-icons class="right" type="right" :size="16" :color="primaryColor"></uni-icons></text>
      <text class="two ellipsis">{{dataInfo.workHourManagementSelectedList.filter(v => v.parent.id == dataInfo.parentId).map(v => v.workName).join('；')}}</text>
    </view>
    <view class="body" v-if="dataInfo.list.length">
      <uni-data-checkbox v-model="dataInfo.workHourManagementList" multiple :localdata="dataInfo.list" mode="list" wrap icon="right" :map="{text:'workName',value:'id'}" @change="selectItem"></uni-data-checkbox>
    </view>
    <view class="zanwu" v-else>
      <text class="newicon newicon-zanwu"></text>
    </view>
    <view class="foot_common_btns">
      <button @click="goBack" type="default" class="primaryButton btn">上一级</button>
      <button @click="confirm" type="default" class="primaryButton btn">确认</button>
    </view>
  </view>
</template>

<script setup>
  import { ref, reactive} from 'vue'
  import { onLoad, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app'
  import { api_workHourManagement, getFetchDataList, api_querySummaryDoc, api_addSummaryDoc } from "@/http/api.js"
  import { defaultColor } from '@/static/js/theme.js'
  import { useSetTitle } from '@/share/useSetTitle.js'
  import { useLoginUserStore } from '@/stores/loginUser'
  import { useGoBack } from '@/share/useGoBack.js'
  
  useSetTitle();
  const loginUserStore = useLoginUserStore();
  const { goBack }  = useGoBack();
  
  // 主题颜色
  const primaryColor = ref(defaultColor)
  
  // 数据
  const dataInfo = reactive({
    list: [],//工单列表
    idx: 0,//页码
    hasMore: true,//是否有更多数据
    incidentId: undefined,//事件ID
    summaryId: undefined,//汇总单ID
    parentId: undefined,//一级工时管理id
    parentName: '',//一级工时管理名称
    workHourManagementList: [],//二级工时管理列表-回显
    workHourManagementSelectedList: [],//二级工时管理列表-已选择
		hosId:null
  })
  
  // 获取汇总单信息
  function getQuerySummaryDoc(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    let postData = {
      incidentId: dataInfo.incidentId,
    };
    api_querySummaryDoc(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        dataInfo.workHourManagementSelectedList = res.workHourManagementList || [];
        dataInfo.workHourManagementList = dataInfo.workHourManagementSelectedList.map(v => v.id);
        getList(0);
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败！'
        });
      }
    })
  }
  
  // 选择
  function selectItem(e){
    let workHourManagementSelectedList = dataInfo.workHourManagementSelectedList.filter(v => v.parent.id != dataInfo.parentId);
    dataInfo.workHourManagementSelectedList = workHourManagementSelectedList.concat(e.detail.data);
  }
  
  // 确认
  function confirm(){
    if(dataInfo.workHourManagementSelectedList.length === 0){
      uni.showToast({
      	icon: 'none',
        title: '请选择二级工时管理'
      });
      return;
    }
    uni.showLoading({
      title: "加载中",
      mask: true,
    });

    let postData = {
      "workHourManagementList": dataInfo.workHourManagementSelectedList.map(v => ({workHourId: v.id, workHourNum: v.workHourNum2 || 1})),
      "summaryId": dataInfo.summaryId
    };
    api_addSummaryDoc(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        uni.showToast({
        	icon: 'none',
          title: '添加工时成功',
          mask: true,
        });
        setTimeout(() => {
          uni.navigateTo({
            url: `/pages/handler/handler?incidentId=${dataInfo.incidentId}`,
          })
        }, 1500)
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败！'
        });
      }
    })
  }
  
  // 获取列表信息
  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: 9999,
      workHourManagement: {
				treeLevel:2,
				hosId:dataInfo.hosId,
        parent: {
          id: dataInfo.parentId,
        }
      }
    }
    
    getFetchDataList("simple/data", "workHourManagement", postData).then(res => {
      uni.hideLoading();
      uni.stopPullDownRefresh();
      if(res.status == 200){
        let list = res.list || [];
				dataInfo.list = 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) => {
    dataInfo.incidentId = option.incidentId;
    dataInfo.summaryId = option.summaryId;
    dataInfo.parentId = option.parentId;
    dataInfo.parentName = option.parentName;
		dataInfo.hosId = option.hosId;
    getQuerySummaryDoc();
  })
  
  onPullDownRefresh(() => {
    getList(0)
  })
  
  onReachBottom(() => {
    // dataInfo.idx += 1;
    // if (dataInfo.hasMore) {
    //   getList(); // 当触底时加载更多数据
    // }
  })
</script>

<style lang="scss" scoped>
.workHourManagementTwo{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .head{
    height: 88rpx;
    display: flex;
    align-items: center;
    padding: 0 24rpx;
    position: fixed;
    z-index: 99;
    width: 100%;
    box-sizing: border-box;
    background: #fff;
    font-size: 26rpx;
    color: $uni-primary;
    .right{
      margin-top: 2rpx;
    }
    .two{
      flex: 1;
    }
  }
  .body{
    border-top: 1rpx solid #DEDEDE;
    margin-bottom: 140rpx;
    margin-top: 88rpx;
    font-size: 26rpx;
    .body_item{
      border-bottom: 1rpx solid #DEDEDE;
      padding: 24rpx;
    }
  }
  .zanwu{
    margin-bottom: 140rpx;
    margin-top: 88rpx;
    display: flex;
    justify-content: center;
    .newicon-zanwu{
      font-size: 256rpx;
      color: #D6D6D6;
      margin-top: 140rpx;
    }
  }
  .foot_common_btns{
    position: fixed;
    left: 0;
    bottom: 0;
    background-color: #fff;
  }
}
</style>
