import { Component, OnInit } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../frame/core/detail/frame.detail';
import { AjaxService } from '../../../../core/service/ajax.service';
import { FuncService } from '../../../../core/service/func.service';
import { MsgService } from '../../../../core/service/msg.service';

declare var $: any;

@Component({
  selector: 'app-cf-initial-review-detail',
  templateUrl: './initial-review-detail.component.html',
  styleUrls: ['./initial-review-detail.component.scss']
})
export class InitialReviewDetailComponent extends FrameDetailComponent implements OnInit {

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
  }

  uploadFile : any;
  initial_remark = '';
  currReviewMsg = '';// 常用审核
  reviewMsgArray = [];

 // 检查批次模式对应的功能按钮
  // ****************批次模式对应的功能按钮*******************start*************************************
  authDtlDurSubmit = false;// 提交期间-是否明细审核按钮
  authMainDurSubmit = false;// 提交期间-是否显示审核主记录按钮
  authDtlDurAuth = false;// 是否审核期间能审核明细
  authMainDurAuth = false;// 是否审核期间能审核主记录
  msgDurSubmit = false;// 提交期间-审核主记录是否发送消息通知
  msgDurAuth = false;// 资格审核期间-审核主记录是否发送消息通知
  enableAuthLable = false;// 资格审核期间-是否开启标签

  ngOnInit() {
    this.initial_remark= this.ResponseEntity.remark||'';
    if (this.ResponseEntity.main_status === 'Reject' || this.ResponseEntity.main_status === 'InitialAppr') {
      $('#detailApprovBtn').prop('disabled', true);
      $('#detailRejectBtn').prop('disabled', true);
    }
    this.checkPattern();
    AjaxService.requestArray('cf/material/def/msg/list.htm', {material_def_id: this.ResponseEntity.material_def_id}, (array)=>{

      this.reviewMsgArray = array;
    });
  }

  changeReviewMsg() {
    this.initial_remark = this.initial_remark + this.currReviewMsg;
  }

  handleImageEvent(event) {

  }

  // 明细通过
  doApproveDetail() {
    if (this.initial_remark && this.initial_remark.trim().length > 0) {
      MsgService.errorAlert('审核意见有内容不允许“审核通过”!');
      return;
    }
    const _this = this;
    const param = {detail_ids: [this.ResponseEntity.detail_id], remark: this.initial_remark};
    AjaxService.request('review/initial/detail/approve.htm', param, (entity) => {
      MsgService.hideLoading();
      MsgService.successToastr('初审通过!');
      _this.closeWindow();
    });
  }
  // 明细退回
  doRejectDetail() {
    if (!this.initial_remark || this.initial_remark.trim().length === 0) {
      MsgService.errorAlert('请输入审核意见');
      return;
    }
    const _this = this;
    const param = {detail_ids: [this.ResponseEntity.detail_id], remark: this.initial_remark};
    MsgService.showLoading();
    AjaxService.request('review/initial/detail/reject.htm', param, (entity) => {
      MsgService.hideLoading();
      MsgService.successToastr('已退回!');
      _this.closeWindow();
    });
  }
  // ****************批次模式对应的功能按钮********************end*************************************
  checkPattern() {
    // 获取当前服务器时间
    AjaxService.requestEntity('cf/pattern/now/get.htm',{},(data)=>{
      const now_time = Date.parse(data);

      if (this.FrameService.PatternMap) {
        const batch = this.FrameService.BatchMap[this.ResponseEntity.batch_id];
        const pattern = this.FrameService.PatternMap[batch.pattern_id];
        const uploadStartTime = Date.parse(batch.upload_start_time);
        const uploadEndTime = Date.parse(batch.upload_end_time);
        const intialReviewStart = Date.parse(batch.intial_review_start);
        const intialReviewEnd = Date.parse(batch.intial_review_end);
        // ******是在提交期间
        console.log('now_time: ' + now_time + ',uploadStartTime: ' + uploadStartTime + ',uploadEndTime: ' + uploadEndTime);
        if (now_time >= uploadStartTime && now_time <= uploadEndTime) {
          if (pattern.is_auth_dtl_dur_submit) {// 提交期间-审核明细 开启
            this.authDtlDurSubmit = true;
          }
          if (pattern.is_auth_main_dur_submit) {// 提交期间-审核主记录 开启
            this.authMainDurSubmit = true;
          }
          if (pattern.is_msg_dur_submit) {// 提交期间-审核主记录是否实时发送消息通知
            this.msgDurSubmit = true;
          }
        }
        // ******是在审核期间
        if (now_time >= intialReviewStart && now_time <= intialReviewEnd) {
          if (pattern.is_msg_dur_auth) {// 资格审核期间-审核主记录是否实时发送消息通知
            this.msgDurAuth = true;
          }
          if (pattern.is_enable_auth_lable) {// 资格审核期间-是否开启主记录标签
            this.enableAuthLable = true;
          }
          this.authDtlDurAuth = true;// 审核期间可以审核明细和主
          this.authMainDurAuth = true;
        }
        // ******
      }
    });
  }

}
