import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { BsModalService } from "ngx-bootstrap/modal";
import { AppComponent } from '../../../../..';
import {FrameFunction} from '../../../../../shared/frame/frame_function';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';

@Component({
  selector: 'app-by-sub-package',
  templateUrl: './by-sub-package.component.html',
  styleUrls: ['./by-sub-package.component.scss']
})
export class BySubPackageComponent extends FlexTableComponent implements OnInit {

  query = {
    exam_time_id: "",
    major_level: "",
    ticket_no: "",
    diaplma_no: ""
  }

  QUERY_URL = "by/byApply/subPage.htm";
  EXPORT_URL = "by/byApply/exportSub.htm";

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    this.formModel = fb.group({
      exam_time_id: [null, Validators.compose([Validators.required])],
      amount: [300, Validators.compose([Validators.required])],
      least: [250, Validators.compose([Validators.required])],
    });
  }

  ngOnInit() {
  }

  // 批次的毕业分装表，点击这个按钮应该弹出提示“确定归档吗？归档之后不能在
  // 改动毕业分装信息，将按照这个分装入盒”，然后锁定本批次的毕业分装信息。第二次点击提示“本批次的毕业分装表已经入盒不能再次归档”

  // “生成毕业分装表”点击生成毕业分装表，如果第二次点击应该提示“本批次已经生成毕业分装表，确认再次生成吗？确认生成将会覆盖前一次生成的毕业分装表信息”
  // 如果已经生成了毕业分装表，还没点击“归档毕业分装表”可以在再次生成，只是要提示生成就会把之前的覆盖。
  // “生成毕业分装表”弹出窗口展示最多装入张数、最少装入张数让他自己输入然后点击确定生成，要不就点击取消。默认就是最多装入张数：300、最少装入张数:250

  lock() {
    if (!this.query.exam_time_id) {
      AppComponent.GobalComponent.errorMsg("请选择毕业批次!");
      return;
    }
    FrameRequest.requestEntity("by/byApply/subStatus.htm",  { exam_time_id: this.query.exam_time_id }, (ret) => {
      if (ret&&ret.status == 0) {
        AppComponent.confirm("确定归档吗？归档之后不能在改动毕业分装信息，将按照这个分装入盒?", () => {
          AppComponent.GobalComponent.setLoading(true);
          FrameRequest.request("by/byApply/subLock.htm", { exam_time_id: this.query.exam_time_id }, () => {
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("毕业生分装表归档成功");
          });
        });
      } else  if (ret&&ret.status == 1) {
        AppComponent.GobalComponent.errorMsg("本批次的毕业分装表已经入盒不能再次归档");
      }else  if (!ret) {
        AppComponent.GobalComponent.errorMsg("本批次的毕业分装表信息不存在");
      }
    });
  }
  //参数处理
  async preGoEditEvent(entity?){
    // var p = new Promise(function(resolve, reject){
    //     resolve(true);
    // });
    this.item={}
    this.item.amount=300;
    this.item.least=250;
    return true;
  }
  build() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if (parseInt(this.item.amount) < parseInt(this.item.least)) {
      AppComponent.GobalComponent.errorMsg("最少装入张数需不大于最多装入张数需!");
      return;
    }
    FrameRequest.requestEntity("by/byApply/subStatus.htm", this.item, (ret) => {
      if (ret&&ret.status == 1) {
        AppComponent.GobalComponent.errorMsg("毕业生分装表已归档，不允许再次生成。");
      }
      else if (ret&&ret.status == 0) {
        AppComponent.confirm("本批次已经生成毕业分装表，确认再次生成吗？确认生成将会覆盖前一次生成的毕业分装表信息?", () => {
          AppComponent.GobalComponent.setLoading(true);
          FrameRequest.request("by/byApply/subBuild.htm", this.item, () => {
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("毕业生分装表生成成功");
          });
        });
      } else {
        AppComponent.confirm("确认生成毕业分装表吗?", () => {
          AppComponent.GobalComponent.setLoading(true);
          FrameRequest.request("by/byApply/subBuild.htm", this.item, () => {
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("毕业生分装表生成成功");
          });
        });

        
      }

    });


  }


  //导出毕业生统计表
  export() {
    if (!this.query.exam_time_id) {
      AppComponent.GobalComponent.errorMsg("请选择毕业批次!");
      return;
    }

    if (!this.query.major_level) {
      this.query.major_level = "";
    }

    FrameRequest.requestEntity("by/byApply/subStatus.htm", {exam_time_id: this.query.exam_time_id}, (ret) => {
      
      if (!ret) {
        AppComponent.GobalComponent.errorMsg("请先生成毕业生分装表再导出");
      }else {
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download(this.EXPORT_URL, {
          exam_time_id: this.query.exam_time_id
          //major_level:this.query.major_level
        }, () => {
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("毕业生分装表导出成功");
        });
      }

    });

  }

}
