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

@Component({
  selector: 'app-weixin-menu-page',
  templateUrl: './weixin-menu-page.component.html',
  styleUrls: ['./weixin-menu-page.component.scss']
})
export class WeixinMenuPageComponent 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 = WeixinMenuDetailComponent;
  }

  ngOnInit() {
  }

  makeMenuFromConfig() {
    AjaxService.requestEntity('weixin/menu/make.htm', {wx_id: this.wxDefine.wx_id}, (entity) => {
      this.showComponentModal(WeixinMenuViewComponent, {ResponseEntity: entity, arrayUrl: 'weixin/tag/all.htm', arrayParam:{wx_id: this.wxDefine.wx_id}}, {
        class: 'modal-75p'});
    });
  }

  makeMenuFromServer() {
    AjaxService.requestEntity('weixin/menu/fetch.htm', {wx_id: this.wxDefine.wx_id}, (entity) => {
      this.showComponentModal(WeixinMenuViewComponent, {ResponseEntity: entity.menu, serverMenu: entity, arrayUrl: 'weixin/tag/all.htm', arrayParam:{wx_id: this.wxDefine.wx_id}}, {
        class: 'modal-75p'});
    }, {ResponseWithLoading: true});
  }

  /*******************
   * 删除菜单
   */
  deleteMenu() {
    MsgService.confirmAlert('确定要删除主菜单吗？所有菜单将被移除！！！不可恢复，请谨慎进行此操作！', () => {
      AjaxService.request('weixin/menu/delete.htm', {
        wx_id: this.wxDefine.wx_id
      }, () => {
        MsgService.successAlert('个菜单已删除，如微信无变化，请退出该公众中重新进入，部分手机有5分钟延迟');
      });
    });
  }

}
