import {Component, OnInit} from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import {LayoutPlanTimeDetailComponent} from '../layout-plan-time-detail/layout-plan-time-detail.component';

@Component({
  selector: 'app-layout-plan-time',
  templateUrl: './layout-plan-time.component.html',
  styleUrls: ['./layout-plan-time.component.scss']
})
export class LayoutPlanTimeComponent extends FramePageComponent implements OnInit {

  group: any;
  groupArray = [];
  timeGroupArray = [];

  constructor(private modalService: BsModalService) {
    super(modalService);
    this.modalComponent = LayoutPlanTimeDetailComponent;
    this.group = {};
  }

  ngOnInit() {
    setTimeout(() => {
      this.requestData();
    }, 1000);

  }

  onModalClose() {
    this.requestData();
  }

  requestData() {
    AjaxService.requestArray('layout/plan/time/group/list.htm', {}, (array) => {
      this.groupArray = array;
      if (array.length === 0) {
        return;
      }
      if (this.group['time_group'] === undefined) {
        this.group = array[0];
      }
      if (this.group['time_group'] !== undefined) {

        AjaxService.requestArray('./layout/plan/time/list.htm', {time_group: this.group['time_group']}, (vArray) => {
          for (let i = 0; i < vArray.length; i++) {
            vArray[i].time_order = i + 1;
          }
          this.timeGroupArray = vArray;
        });
      }
    });
  }

  deleteGroup() {
    MsgService.confirmAlert(`确定要删除分组【${this.group['time_group']}】吗？`, () => {
      AjaxService.request('layout/plan/time/delete.htm', this.group, () => {
        MsgService.successToastr(`分组【${this.group['time_group']}】已删除！`);
        this.requestData();
      });
    });
  }

}
