
import { MeetingItemModel,ItemModel } from '../model/ItemModel'
import {  hdHttp, HdResponse,BasicConstant } from '@itcast/basic'
import { BusinessError } from '@kit.BasicServicesKit';
import { router } from '@kit.ArkUI'
import promptAction from '@ohos.promptAction';
import { TypeuuidModel } from '../model/TypeuuidModel';

@Preview
@Component
export struct ItemCompBack {
  item: MeetingItemModel = new MeetingItemModel({} as ItemModel)
  @State timeColor:ResourceStr=$r('app.color.ee432f')
  @State status:string=''
  aboutToAppear(): void {
    this.getMeetingStatus()
  }

  build() {
    Column() {
      Text().height(1).width('100%')
      if(this.item.count==0)
      {
        Text(this.item.begin_date.substring(0, 4)
          + "年"
          + this.item.begin_date.substring(5, 7)
          + "月").fontSize(14).fontColor($r('app.color.333333')).height(35)
      }
      RelativeContainer()
      {
          Text().width(23).backgroundColor(this.timeColor).borderRadius(5).height('100%')
        RelativeContainer(){


           Column()
           {
             Text(this.item.title) .maxLines(2).fontSize(15).fontColor(Color.Black).textAlign(TextAlign.Start)
               .textOverflow({ overflow: TextOverflow.Ellipsis }).id('title').width('100%')

               RelativeContainer()
               {
                 Stack()
                 {
                   Image(BasicConstant.urlHtml+this.item.liveimg).width('100%').height(162)
                     .objectFit(ImageFit.Fill)
                     Image($r('app.media.bofang')).width(40).height(40)
                 }

               }
               .height(162).backgroundColor(Color.White)
               .margin({top:10,right:5}).clip(true).id('rr')
             Row(){
               Image($r('app.media.meetingtime')).width(13).height(13)
               Text(this.getTime(this.item.begin_date,this.item.end_date)).fontSize(14)
                 .fontColor($r('app.color.999999')).margin({left:10})
               Blank()
               Image($r('app.media.meetinglocation')).width(15).height(15)
               Text(this.item.location).fontSize(14)
                 .fontColor($r('app.color.999999')).margin({left:10})
             }
             .width('100%')
             .margin({top:6})
           }.height(247).justifyContent(FlexAlign.Center).margin({left:20,right:5})


          }
          .height('100%').clip(true)
        .margin({left:20,right:5}).backgroundColor(Color.White)
        Text(this.item.begin_date.substring(8,10)).width(20).height(20) .alignRules({

          bottom: { anchor: "__container__", align:VerticalAlign.Center}
        })
          .margin({left:10})
          .fontSize(14)
          .fontColor(this.timeColor)
          .textAlign(TextAlign.Center)
          .backgroundColor(Color.White)
          .borderRadius(20)

      }.width('100%').height(247).clip(true).
      backgroundColor(Color.White).borderRadius(5)



    }.backgroundColor($r('app.color.e4e4e4'))
    .width('100%').padding({left:10,right:10}).clip(true)
    .onClick(() => {
      this.getTypeUuidByName(this.item.title)
    })
  }

  getTypeUuidByName(name:string)
  {
    hdHttp.post<string>(BasicConstant.getTypeUuidByName, {
      name: name,
    } as extraData).then(async (res: HdResponse<string>) => {
      let json:TypeuuidModel = JSON.parse(res+'') as TypeuuidModel;
      if(json.code == '1') {
        if(json.type_uuid==null)
        {
          promptAction.showToast({ message: '该会议视频暂不支持回放' })
        }
        else
        {
          router.pushUrl({url:'pages/VideoPage/VideoMorePage',
            params: { uuid: json.type_uuid ,title:this.item.title}})
        }
      } else {
        promptAction.showToast({ message: json.message, duration: 1000 })
      }
    }).catch((err: BusinessError) => {

    })
  }

  getTime(str1:string, str2:string) {
  if (str1.substring(0, 10)==(str2.substring(0, 10))) {
      return str1.replaceAll("-", ".").substring(0, 10).toString();
       }
     return str1.replaceAll("-", ".").substring(0, 10)+"-"+str2.replaceAll("-", ".").substring(0, 10)
}
  getMeetingStatus()
  {
    if(this.item.count%4==0)
    {
      this.timeColor=$r('app.color.ee432f')
    }
    else if(this.item.count%4==1)
    {
      this.timeColor=$r('app.color.f9b928')
    }
    else if(this.item.count%4==2)
    {
      this.timeColor=$r('app.color.63ccc7')
    }
    else
    {
      this.timeColor=$r('app.color.99d750')
    }

    if('1'==this.item.status)
    {
      this.status='预告'
    }
    else if('2'==this.item.status)
    {
      this.status='直播中'
    }
    else if('3'==this.item.status)
    {
      this.status='已结束'
    }
    else if('4'==this.item.status)
    {
      this.status='回放中'
    }
    else
    {
      this.status=''
    }
  }
}

interface extraData {
  name: string
}