import { ItemCompBack } from './ItemCompBack'
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 { EmptyViewComp,HdLoadingDialog } from '@itcast/basic'
import HashMap from '@ohos.util.HashMap';


@Component
export struct ListCompBack {
  @State isEmptyViewVisible: boolean = false; // 控制显隐的状态变量

  @Prop
  @Watch('onUpdate')
  year:string=''
  @Prop
  @Watch('onUpdate')
  type:string=''
  @State
  list: ItemModel[] = []
  controller = new HdListController()
  @State
  page: number = 1
  @State
  keyword: string = ''
  // timer: number = -1
  hashMap: HashMap<string, string> = new HashMap();

  dialog: CustomDialogController = new CustomDialogController({
    builder: HdLoadingDialog({ message: '加载中...' }),
    customStyle: true,
    alignment: DialogAlignment.Center
  })

  onUpdate() {
    // console.info('Response onUpdate')
    // if (this.timer) clearTimeout(this.timer)
    // this.timer = setTimeout(() => {
    //   this.controller.reload()
      this.onRefresh()
    // }, 500)

  }

  onRefresh() {
    this.page = 1
    this.initData(0)

  }

  initData(type:number)
  {
    this.dialog.open()
    console.info('Response type'+type)
    this.hashMap.clear();
    this.hashMap.set('page', this.page+"")
    if(this.year=='会议时间')
    {
      this.hashMap.set('year','')
    }
    else
    {
      this.hashMap.set('year',this.year)
    }

    this.hashMap.set('type',this.type)
    hdHttp.httpReq<string>(BasicConstant.meetingV2Video,this.hashMap).then(async (res: HdResponse<string>) => {
      logger.info('Response meetingListV2'+res);
      let json:MeetingModels = JSON.parse(res+'') as MeetingModels;
      this.dialog.close();
      if(type==0)
      {
        this.controller.refreshed()
      }
      else
      {
        this.controller.loaded()
      }
      // if(this.page==1&&json.data.list!=null&&json.data.list.length>0)
      if(this.page==1)
      {
        this.list=[]
        if(json.data!=null&&json.data.list!)
        {
          this.list = json.data.list
        }

      }
      else if(this.page>1&&json.data!=null&&json.data.list!=null&&json.data.list.length>0)
      {
        this.list.push(...json.data.list)
      }
      this.getPosition()
        if (this.page >= json.data.pages) {
          this.controller.finished()
        } else {
          this.page++
        }
      if (this.list.length > 0) {
        this.isEmptyViewVisible = false;
      } else  {
        this.isEmptyViewVisible = true;
      }
    }).catch((err: BusinessError) => {
      this.dialog.close();
      if (this.list.length > 0) {
        this.isEmptyViewVisible = false;
      } else  {
        this.isEmptyViewVisible = true;
      }
    })
  }
  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() {
    if (this.isEmptyViewVisible){
      EmptyViewComp({promptText:'暂无回放',isVisibility:this.isEmptyViewVisible})
        .width('100%')
        .height('100%')
    } else {
      HdList({
        lw: 1,
        controller: this.controller,
        onRefresh: () => {
          this.onRefresh()
        },
        onLoad: () => {
          this.initData(1)
        }
      }) {
        ForEach(this.list, (item: MeetingItemModel) => {
          ListItem() { ItemCompBack({ item }) }
        })
      }
    }
  }
}