import {Component, OnInit} from '@angular/core';
import {CookieService} from 'ngx-cookie-service';
import {Router} from '@angular/router';
import {WeixinTemplateDetailComponent} from '../weixin-template-detail/weixin-template-detail.component';
import {WeixinTemplateSendComponent} from '../weixin-template-send/weixin-template-send.component';
import { FramePageComponent } from '../../../frame/core/page/frame.page';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../core/service/ajax.service';

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

  wxDefine: any;

  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 = WeixinTemplateDetailComponent;
  }


  ngOnInit() {
  }

  showSend() {
    this.showComponentModal(WeixinTemplateSendComponent, {
      ResponseEntity: this.wxDefine,
      arrayUrl: 'weixin/template/all.htm',
      arrayParam: {wx_id: this.wxDefine.wx_id}
    });
  }

}
