import { ItemComp } from './ItemComp'
import { MeetingItemModel,MeetingModel,MeetingModels,ItemModel } from '../model/ItemModel'
import { HdList, HdListController,BasicConstant,hdHttp, HdResponse ,logger} from '@itcast/basic/Index'
import { promptAction, router } from '@kit.ArkUI'
import { BusinessError } from '@kit.BasicServicesKit';

import HashMap from '@ohos.util.HashMap';


@Component
export struct ListComp {
  @State
  list: ItemModel[] = []
  controller = new HdListController()
  @State
  page: number = 1
  @State
  keyword: string = ''
  @Prop
  @Watch('onUpdate')
  sort: number = 30
  timer: number = -1
  hashMap: HashMap<string, string> = new HashMap();

  onUpdate() {
    if (this.timer) clearTimeout(this.timer)
    this.timer = setTimeout(() => {
      this.controller.reload()
      this.onRefresh()
    }, 500)
  }

  onRefresh() {
    this.page = 1
    this.initData(0)
    // InterviewService.findInterviewList({
    //   questionBankType: 9,
    //   page: this.page,
    //   sort: this.sort,
    //   keyword: this.keyword
    // }).then(res => {
    //   this.list = res.data.rows
    //   this.controller.refreshed()
    //   if (this.page >= res.data.pageTotal) {
    //     this.controller.finished()
    //   } else {
    //     this.page++
    //   }
    //   promptAction.showToast({ message: '已更新' })
    // })
  }

  initData(type:number)
  {
    this.hashMap.clear();
    this.hashMap.set('page', this.page+"")
    hdHttp.httpReq<string>(BasicConstant.meetingListV2,this.hashMap).then(async (res: HdResponse<string>) => {
      logger.info('Response meetingListV2'+res);
      let json:MeetingModels = JSON.parse(res+'') as MeetingModels;

      if(type==0)
      {
        this.controller.refreshed()
      }
      else
      {
        this.controller.loaded()
      }
      if(this.page==1&&json.data.list!=null&&json.data.list.length>0)
      {
        this.list = json.data.list
      }
      else if(this.page>1)
      {
        this.list.push(...json.data.list)
      }
      this.getPosition()
        if (this.page >= json.data.pages) {
          this.controller.finished()
        } else {
          this.page++
        }

    }).catch((err: BusinessError) => {


    })
  }
  getPosition() {
  let DatasList:string[] = [];
  let count = 0;
  for (let i = 0; i <this.list.length; i++) {
    if (!DatasList.includes(this.list[i].begin_date
      .substring(0, 7))) {
      DatasList.push(this.list[i].begin_date.substring(0, 7));
      count = 0;
      this.list[i].count=count;
    } else {
      count++;
      this.list[i].count=count;
    }
  }

}
  build() {
    HdList({
      lw: 1,
      controller: this.controller,
      onRefresh: () => {
        this.onRefresh()
      },
      onLoad: () => {
        this.initData(1)

      }
    }) {
      ForEach(this.list, (item: MeetingItemModel) => {
        ListItem() {
          ItemComp({ item })
        }
      })
    }

  }
}