import { ItemCompVideo } from './ItemCompVideo'
import { VideoMoreModel,VideoMore } from '../model/VideoMoreModel'
import { HdGrid, HdListController,BasicConstant,hdHttp, HdResponse ,logger} from '@itcast/basic/Index'
import { promptAction, router } from '@kit.ArkUI'
import { BusinessError } from '@kit.BasicServicesKit';
import { HdLoadingDialog,EmptyViewComp } from '@itcast/basic'
import HashMap from '@ohos.util.HashMap';


@Component
export struct ListCompGandan {
  @State isEmptyViewVisible: boolean = false; // 控制显隐的状态变量
  @Prop@Watch('onUpdate') sort:string='2'
  @Prop@Watch('onUpdate')
  type_uuid:string=''
  @Prop@Watch('onUpdate') keywords:string=''
  @State
  list: VideoMore[] = []
  controller = new HdListController()
  @State
  page: number = 1

  hashMap: HashMap<string, string> = new HashMap();

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


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

  initData(type:number)
  {
    this.dialog.open()
    this.hashMap.clear();
    this.hashMap.set('keywords',this.keywords)
    this.hashMap.set('page', this.page+"")
    this.hashMap.set("sort", this.sort);
    this.hashMap.set('typeUuid', this.type_uuid)
    hdHttp.httpReq<string>(BasicConstant.videoByKeyWordsNew,this.hashMap).then(async (res: HdResponse<string>) => {
      logger.info('Response videoByTypeNew'+res);
      let json:VideoMoreModel = JSON.parse(res+'') as VideoMoreModel;
      this.dialog.close()
      if(type==0)
      {
        this.controller.refreshed()
      }
      else
      {
        this.controller.loaded()
      }
      if(this.page==1)
      {
        this.list=[]
        if(json.data!=null&&json.data.list!)
        {
          this.list = json.data.list
        }

      }
      else if(this.page>1)
      {
        this.list.push(...json.data.list)
      }

        if (this.page >= json.data.totalPage) {
          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;
      }
    })
  }

  build() {
    if (this.isEmptyViewVisible){
      EmptyViewComp({promptText:'暂无数据',isVisibility:this.isEmptyViewVisible})
        .width('100%')
        .height('100%')
    } else {
      HdGrid({
        lw: 1,
        controller: this.controller,
        onRefresh: () => {
          this.onRefresh()
        },
        onLoad: () => {
          this.initData(1)
        }
      }) {
        ForEach(this.list, (items: VideoMore) => {
          GridItem() {
            ItemCompVideo({ item:items })
          }
        })
      }
    }
  }
}