import {Component, OnInit} from '@angular/core';
import {LayoutAgentDetailComponent} from '../layout-agent-detail/layout-agent-detail.component';
import {CookieService} from 'ngx-cookie-service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { BsModalService } from 'ngx-bootstrap/modal';
import { MsgService } from '../../../../../core/service/msg.service';
import { FuncService } from '../../../../../core/service/func.service';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { ModalService } from '../../../../../core/service/modal.service';

@Component({
  selector: 'app-layout-agent-page',
  templateUrl: './layout-agent-page.component.html',
  styleUrls: ['./layout-agent-page.component.scss']
})
export class LayoutAgentPageComponent extends FramePageComponent implements OnInit {
  currentAgentId: any;

  constructor(private modalService: BsModalService, private cookies: CookieService) {
    super(modalService);
    this.modalComponent = LayoutAgentDetailComponent;
    if (this.cookies.get('CurrentAgentId').length > 0) {
      this.currentAgentId = parseInt(this.cookies.get('CurrentAgentId'), 10);
    }
  }


  ngOnInit() {
  }

  chooseAgent(v) {
    if (v.ly_agent_id === undefined) {
      return;
    }
    this.currentAgentId = v.ly_agent_id;
    this.cookies.set('CurrentAgentId', v.ly_agent_id);
    MsgService.successToastr(`已将考点【${v.ly_agent_name}】选中为默认操作考点`);
  }

  /*********************
   * 检核缴费时间是否超出
   */
  checkPayTime(agent) {
    let pay_end_time = agent.enrol_end_time;
    if (agent.pay_end_time !== undefined) {
      pay_end_time = agent.pay_end_time;
    }
    if (pay_end_time === undefined) {
      return false;
    }
    if (pay_end_time < FuncService.dateToLongString(new Date())) {
      return true;
    }
    return false;
  }

  onModalClose() {
    this.ajax.reload();
  }

  refreshAgent(agent) {
    MsgService.confirmAlert('确定要去刷新编排数据吗？需要比较长的时间，如无必要，请勿进行此操作!', () => {
      agent.ly_ticket_explain = undefined;
      AjaxService.requestEntity('layout/agent/refresh.htm', agent, (entity) => {
        ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
      });
    });
  }

  resetTicket(agent) {
    MsgService.confirmAlert('确定要重新生成该考点的准考证号吗？并需要重新编排，该操作不可回撤，请注意进行数据备份！', () => {
      AjaxService.requestEntity('layout/agent/ticket/reset.htm', {ly_agent_id: agent.ly_agent_id}, (entity) => {
        ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
      },{RequestWithLoading: true});
    });
  }

}
