import { Component, OnInit, Output, Input } from '@angular/core';
import { EventEmitter } from '@angular/core';
import { MainService } from 'src/app/services/main.service';
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-display-form',
  templateUrl: './display-form.component.html',
  styleUrls: ['./display-form.component.less']
})
export class DisplayFormComponent implements OnInit {
	@Input() hsmsData:any;
  @Output() confirmModal = new EventEmitter();
  @Output() cancelModal = new EventEmitter();
  constructor(
    private mainService: MainService,
    private tool: ToolService,
    private message: NzMessageService,
  ) { }

  controlView = {}; //展示形式
  patientCareCol: number = 7; //顶部工单列数(4列-8人，7列-14人，6列-单排 ）
  hurseInfoHiding = 1; //护工信息隐藏
  txtLabelCol: number = 1; //工单信息列数
  orderRefreshTime: number = 30; //工单刷新倒计时
  orderInfoTime: number = 60; //工单刷新时间
  workerRefreshTime: number = 30; //配送人员刷新倒计时
  workerInfoTime: number = 60; //配送人员刷新时间
  audioNotDispatched: boolean = true; //是否开启定时播报未派出工单提示音
  orderType: string = 'priority'; //排序方式
	leftMenuLocation: any = 2; //左侧菜单
	rightMenuLocation: any = 2; //右侧菜单
	showSource: any = 2; //是否展示来源
	searchDay: any = 1; //查询时间范围
  ngOnInit() {
		console.log(778778, this.hsmsData)
    this.initControlView();
  }

  isLoading:boolean = false;
  initControlView() {
    this.isLoading = true;
    let postData = {
      controlView: {},
      idx: 0,
      sum: 1,
    };
    this.mainService
      .coopTypeConfig("fetchDataList", "controlView", postData)
      .subscribe((data) => {
        this.isLoading = false;
        this.controlView = data.list[0] ? data.list[0] : [];
        if (!data.list.length) return;
        // switch (data.list[0].workerNum) {
        //   case 1:
        //     this.patientCareCol = 6;
        //     break;
        //   case 2:
        //     this.patientCareCol = 7;
        //     break;
        //   case 3:
        //     this.patientCareCol = 4;
        //     break;
        // }
        this.patientCareCol = 7;
        this.hurseInfoHiding = data.list[0].hurseInfoHiding ? 1 : 0;
        // this.txtLabelCol = data.list[0].labelNum;
        this.txtLabelCol = 1;
        this.orderRefreshTime = this.orderInfoTime = data.list[0].orderInfoTime;
        this.workerRefreshTime = this.workerInfoTime = data.list[0].workerInfoTime;
        this.audioNotDispatched = data.list[0].unsendOrderVoice !== false;
        this.orderType = data.list[0].orderType || 'priority';
				this.leftMenuLocation = data.list[0].leftMenuLocation || 2;
				this.rightMenuLocation = data.list[0].rightMenuLocation || 2;
				this.showSource = data.list[0].showSource || 2;
				this.searchDay = data.list[0].searchDay || 1;
      });
  }
	
  // 保存展示形式
  saveLoading: boolean = false; //保存按钮loading状态
  saveOrderScope() {
    if(!this.workerInfoTime){
      this.message.warning('配送人员刷新时间必填！')
      return;
    }
    if(!this.orderInfoTime){
      this.message.warning('工单刷新时间必填！')
      return;
    }
		if(this.searchDay % 1 !== 0){
			this.message.warning('查询时间范围必须是整数！')
			return
		}
    this.saveLoading = true;
    let wn;
    switch (this.patientCareCol) {
      case 6:
        wn = 1;
        break;
      case 7:
        wn = 2;
        break;
      case 4:
        wn = 3;
        break;
    }

    let postData = {
      controlView: {
        workerNum: wn,
        labelNum: this.txtLabelCol,
        workerInfoTime: this.workerInfoTime,
        orderInfoTime: this.orderInfoTime,
        unsendOrderVoice: this.audioNotDispatched,
        hurseInfoHiding: this.hurseInfoHiding == 1,
        orderType: this.orderType,
				leftMenuLocation: this.leftMenuLocation,
				rightMenuLocation: this.rightMenuLocation,
				showSource: this.showSource,
				searchDay: this.searchDay
      },
    };
    if (this.controlView["id"]) {
      postData.controlView["id"] = this.controlView["id"];
    }

    console.log(postData);
    this.mainService
      .coopTypeConfig("addData", "controlView", postData)
      .subscribe((data) => {
        this.saveLoading = false;
        this.confirmModal.emit(data);
      });
  }

  // 关闭弹窗
  cancelInit() {
    this.cancelModal.emit();
  }
}
