import { Component, OnInit } from '@angular/core';
import {CookieService} from 'ngx-cookie-service';
import {Router} from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import {WeixinUserDetailComponent} from '../weixin-user-detail/weixin-user-detail.component';
import {WeixinUserMsgComponent} from '../weixin-user-msg/weixin-user-msg.component';
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePageComponent } from '../../../frame/core/page/frame.page';
import { ModalService } from '../../../../core/service/modal.service';

@Component({
  selector: 'app-weixin-user-page',
  templateUrl: './weixin-user-page.component.html',
  styleUrls: ['./weixin-user-page.component.scss']
})
export class WeixinUserPageComponent extends FramePageComponent implements OnInit {

  wxDefine: any;
  WeixinUserMsgComponent = WeixinUserMsgComponent;
  constructor(private router: Router, private cookies: CookieService,  modalService: BsModalService) {
    super(modalService);
    // 获取缓存中的微信编号
    const wx_id = this.cookies.get('CurrentWxId');
    if (wx_id !== undefined && wx_id.length > 0 ) {
      // 去获取
      AjaxService.requestEntity('weixin/define/find.htm', {wx_id: wx_id}, (entity) => {
        this.wxDefine = entity;
        setTimeout( () => {
          if (this.pager) {
            this.pager.reload({wx_id: this.wxDefine.wx_id});
          }
        }, 1);
      });
    } else {
      this.router.navigate(['/weixin/define/page']) ;
    }
    this.modalComponent = WeixinUserDetailComponent;
  }


  ngOnInit() {
  }

  /********************
   * 同步用户
   */
  syncUser() {
    MsgService.confirmAlert('确定要同步所有用户吗？根据关注用户，该操作所需时间可能会很长！', () => {
      AjaxService.request('weixin/user/sync/all.htm', {wx_id: this.wxDefine.wx_id}, (ajax) => {
        ModalService.showThreadWindow(this.bsService, ajax.entity, {threadTitle: '请求已提交，正在进行同步操作'} );
      });
    });
  }

  openSendMsg(user) {
    this.showComponentModal(WeixinUserMsgComponent, {ResponseEntity: user});
  }
}
