import {Component, OnInit, ViewChild} 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 {ExamJudgeRuleDetailComponent} from '../exam-judge-rule-detail/exam-judge-rule-detail.component';

@Component({
  selector: 'app-exam-judge-rule',
  templateUrl: './exam-judge-rule.component.html',
  styleUrls: ['./exam-judge-rule.component.scss']
})
export class ExamJudgeRuleComponent extends FramePageComponent implements OnInit {

  agent: any;

  constructor(private router: Router, private cookies: CookieService, modalService: BsModalService) {
    super(modalService);
    this.modalComponent = ExamJudgeRuleDetailComponent;
    // 获取缓存中的微信编号
    const agent_id = this.cookies.get('CurrentAgentId');
    if (agent_id !== undefined && agent_id.length > 0) {
      // 去获取
      AjaxService.requestEntity('layout/agent/find.htm', {ly_agent_id: agent_id}, (entity) => {
        this.agent = entity;
        setTimeout(() => {
          if (this.ajax) {
            this.ajax.reload({ly_agent_id: agent_id});
          }
        }, 1);
      });
    } else {
      this.router.navigate(['/art/layout/agent/page']);
    }
  }

  onModalClose() {
    this.ajax.reload({ly_agent_id: this.agent.ly_agent_id});
  }

  ngOnInit() {

  }




  addRule(group) {
    if (group.ly_judge_total == undefined || group.room_judge_num - group.ly_judge_total > 0) {
      this.showModal({mapUrl:'exam/judge/rule/init.htm',mapParam:{ly_group_id:group.ly_group_id,ly_agent_id:group.ly_agent_id}});
    }
  }

  deleteRule(rule) {
    MsgService.confirmAlert('确定要删除该规则吗？', () => {
      AjaxService.request('exam/judge/rule/delete.htm',{rule_id:rule.rule_id}, () => {
        MsgService.successToastr('规则已删除');
        this.ajax.reload({ly_agent_id:this.agent.ly_agent_id});
      })
    })
 }
}
