import {Component, OnInit, ViewChild} from '@angular/core';
import {FramePageComponent} from "../../../frame/core/page/frame.page";
import {BsModalService} from "ngx-bootstrap/modal";
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePager } from '../../../../core/frame/pager/frame-pager/frame-pager';
import { Console } from 'console';
import { FinalReviewPdfComponent } from '../final-review-pdf/final-review-pdf.component';
import { FinalReviewVideoComponent } from '../final-review-video/final-review-video.component';
import { InitialReviewImageComponent } from '../../initial-review/initial-review-image/initial-review-image.component';
import { FinalReviewSignComponent } from '../final-review-sign/final-review-sign.component';
import { ReviewPromiseComponent } from '../review-promise/review-promise.component';


@Component({
  selector: 'app-cf-final-review-page',
  templateUrl: './final-review-page.component.html',
  styleUrls: ['./final-review-page.component.scss']
})
export class FinalReviewPageComponent extends FramePageComponent implements OnInit {
  uploadList = [];
  rankList = [];
  query = {
    click_from : '',
    main_id : '',
    status : ''
  };
  reviewEntity = {review_level: '',review_remark: '', review_score: ''};
  curStep = "std";
  curBatch : any;
  curMainInfo = {std_edu: "",std_name: "",exam_id: "",apply_id: "",std_id: "",aspect_name: "",review_score: "", review_remark: "", std_image: ""};//当前考生信息
  curProgressInfo = {total_cnt: "",done_cnt: "",to_be_cnt: "",pass_cnt: "",sign_flag: 0,pass_percent: "",initial_pass_cnt:""};//进度信息

  constructor(private modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit() {
    AjaxService.requestEntity("cf/batch/current/get.htm",{},(entity)=>{
      this.curBatch =  entity;
      debugger;
      //是否需要承诺书
      if (this.curBatch.need_promise == 1) {
        let parm = {batch_id: this.curBatch.batch_id, biz_type: "final"}
        AjaxService.requestMap("review/promise/find.htm",parm ,(map)=>{
          debugger;
          if (map && !map.sgin) {
            //没有签
            this.onModalClose = () => {
              this.queryNextInfo();//下一个考生信息和 进度
              this.queryRankList();//左边排名
              this.queryProgressInfo();//
            };
            map.promise.batch_id = this.curBatch.batch_id;
            this.showComponentModal(ReviewPromiseComponent, { 'ResponseEntity': map.promise }, {class: 'modal-7p'});
          } else {
            this.queryNextInfo();//下一个考生信息和 进度
            this.queryRankList();//左边排名
            this.queryProgressInfo();//
          }
        });
      } else {//不需要签
          //考生数据
          this.queryNextInfo();//下一个考生信息和 进度
          this.queryRankList();//左边排名
          this.queryProgressInfo();//
      }
    });
  }

  

  //左边排名列表
  queryRankList() {
    if (this.curBatch) {
      AjaxService.requestArray("final/review/rank/list.htm",{batch_id: this.curBatch.batch_id},(array)=>{
        //debugger;
        this.rankList = array;
      });
    }
  }

  //查询进度信息
  queryProgressInfo() {
    let param = {
      batch_id: this.curBatch.batch_id, 
      aspect_id: ''
    };
    AjaxService.requestMap("final/review/progress/info.htm",param,(map)=>{
      this.curProgressInfo =  map;
      //debugger;
    }); 
  }
  
  //下一个考生信息和进度
  queryNextInfo(callback?: Function) {
    let param = {
      click_from: this.query.click_from,
      main_id: this.query.main_id, 
      batch_id: this.curBatch.batch_id, 
      status: "InitialAppr"
    };
    AjaxService.requestEntity("final/review/std/next/total.htm",param,(entity)=>{
      //debugger;
      console.log("----------->" + this.curMainInfo.apply_id);
      if (!entity) {
        //debugger;
        MsgService.successToastr("已全部评审完成!");
        return;
      } else {
        this.curMainInfo =  entity;
      }
     //debugger;
      if (callback) {
        callback.call(undefined);
      }
    });    
  }

  //当前选中考生信息
  queryChooseLeftInfo(callback?: Function) {
    let param = {
      main_id: this.query.main_id, 
      batch_id: this.curBatch.batch_id
    };
    AjaxService.requestEntity("final/review/std/curr/total.htm",param,(entity)=>{
      this.curMainInfo =  entity;
    //左边选后带出当前考生评分和评语
    this.reviewEntity.review_score = this.curMainInfo.review_score;
    this.reviewEntity.review_remark = this.curMainInfo.review_remark;
      if (callback) {
        callback.call(undefined);
      }
    });    
  }

  //左边已评审考生 选择
  chooseLeftStd(std) {
    //debugger;
    this.query.click_from = "left";
    this.query.main_id = std.apply_id;
    this.curMainInfo["apply_id"] = std.apply_id;
    
    let fn = function() {
      //debugger;
      this.pager.pagerParams["click_from"] = "left";//左边排名点击查询
      this.pager.pagerParams["main_id"] = std.apply_id;
      this.pager.reload();
    }
    this.queryChooseLeftInfo(fn.bind(this));
  }

  //点击右边下一个 未评审考生
  clickNextStd() {
    //限定下一个
    //debugger;
    if (!this.curMainInfo) {
      
      return;
    }
    this.query.click_from = "next";
    this.query.main_id = this.curMainInfo.apply_id;
    
    let fn = function() {
      debugger;
      this.pager.pagerParams["status"] = "InitialAppr";//查询初审结束的 待专业评审
      this.pager.pagerParams["click_from"] = "next";//点击下一个未评审
      this.pager.pagerParams["main_id"] = this.curMainInfo.apply_id;
      this.pager.reload();
    }
    this.queryNextInfo(fn.bind(this));
    this.reviewEntity.review_score = "";
    this.reviewEntity.review_remark = "";
  }

  //材料查看---pdf
  showMaterilModal(v) {
    //debugger;
    this.onModalClose = () => {
      this.pager.reload();
    };
    let imageList = [];
    //查询当前考生材料要展示的
    AjaxService.requestArray("std/apply/upload/list.htm",{detail_id: v.detail_id},(array)=>{
      debugger;
      this.uploadList = array;
      for (const u of this.uploadList) {
        if (u.file_type.toLowerCase().indexOf('jpg') > 0 || u.file_type.toLowerCase().indexOf('png') > 0) {
          imageList.push(u.file_path);
          
        } else if (u.file_type.toLowerCase() == 'pdf') {
          v.file_path = u.file_path;
          this.showComponentModal(FinalReviewPdfComponent, { 'ResponseEntity': v }, {class: 'modal-9'});
          break;

        } else if (u.file_type.toLowerCase() == 'mp4') {
          v.file_path = u.file_path;
          this.showComponentModal(FinalReviewVideoComponent, { 'ResponseEntity': v }, {class: 'modal-7p'});
          break;

        }
      }
      //图片显示
      if (imageList.length > 0) {
        v.imageList = imageList;
        this.showComponentModal(InitialReviewImageComponent, { 'ResponseEntity': v }, {class: 'modal-7p'});
      }
    }); 
    
  }

  //保存排名
  save() {
    if (!this.reviewEntity.review_score) {
      MsgService.errorAlert("请输入评分");
      return;
    }
    let param = {
      apply_id: this.curMainInfo.apply_id,
      review_score: this.reviewEntity.review_score,
      review_remark: this.reviewEntity.review_remark
    };
    debugger;
    let _this = this;
    MsgService.showLoading();
    AjaxService.request('final/review/save.htm', param, (entity) => {
        MsgService.hideLoading();
        _this.queryRankList();//更新排名
        _this.queryProgressInfo();//更新统计
        _this.clickNextStd();//下一个考生
        MsgService.successToastr("操作成功!");
    });
  }

  //确认提交---验证未通过考生的评语是否为空
  doConfirm() {
    AjaxService.requestEntity("final/review/submit/check.htm",{batch_id: this.curBatch.batch_id},(entity)=>{
      if (entity && entity == true) {
        this.onModalClose = () => {
          this.queryProgressInfo();//刷新进度和按钮保存控制
          this.pager.pagerParams["click_from"] = "next";
          this.pager.reload();
        };
        this.showComponentModal(FinalReviewSignComponent, { 'ResponseEntity': this.rankList[0] });
      }
    });
  }

  formateReviewTime() {
    if (this.curBatch) {
      return "导师评审时间：" + this.curBatch.profes_review_start + " - " + this.curBatch.profes_review_end;
    }
    return "";
  }

}
