import {Component, OnInit, ViewChild} from '@angular/core';
import {WorkService} from '../work.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ParamsTypeComponent} from '../modal/params-type/params-type.component';
import {CommonService} from '../../shared/common/common.service';

@Component({
  selector: 'smart-pararms',
  templateUrl: './pararms.component.html',
  styles: [
      `
          :host ::ng-deep .content-params .ant-row,
          .handle{
              margin: 10px 0;
          }
      `
  ]
})
export class PararmsComponent implements OnInit {
  @ViewChild('smartParamstype') smartParamstype:ParamsTypeComponent;

    eventList;
    planList;
    isDisabled = true;
    eventPre = {
        eventNoPre:"",
        id:""
    };
  constructor(private workSer:WorkService,private message:NzMessageService,
              private commomSer:CommonService) { }

  ngOnInit() {
    this.getList();
    this.getEventPre();
  }

  //获取事件前缀
   getEventPre(){
      this.workSer.getEventNoPre().subscribe(
          (res)=>{
              if(res.data){
                  this.eventPre = res.data;
              }else{
                  this.message.warning("暂无事件前缀");
              }
          }
      )
   }

  //获取分类列表
  getList(){
    const data = {
        type:0
    };
    this.workSer.findParamsByType(data).subscribe(
        (res)=>{
            if(res.data){
                this.eventList = res.data;
            }else{
                this.message.warning("暂无事件分类")
            }
        }
    );
      data.type = 1;
      this.workSer.findParamsByType(data).subscribe(
          (res)=>{
              if(res.data){
                  this.planList = res.data;
              }else{
                  this.message.warning("暂无计划分类");
              }
          }
      )
  }

  //0=事件分类, 1=计划分类
  showAddModal(title,type){
    this.smartParamstype.showAddModal(title,type);
  }

    showEditModal(title,type,item){
        this.smartParamstype.showEditModal(title,type,item.id,item);
    }

    //删除分类
  deleteItem(item){
    this.commomSer.confirmThing("删除","确定删除该分类?",()=>{
      const data = {
          ids:[]
      };
      data.ids.push(item.id);
      this.workSer.deleteTypeByparams(data).subscribe(
          (res)=>{
            if(res.errCode == 10000){
              this.message.success("删除成功");
            }else{
              this.message.error(res.errMsg);
            }
              this.getList()
          }
      )
    })
  }

    editType(){
      this.isDisabled = false;
    }

    //保存修改的事件前缀
    saveType(){
      this.isDisabled = true;
      const data = {
          eventNoPre:this.eventPre.eventNoPre
      }
      this.workSer.updateEventNoPre(data).subscribe(
          (res)=>{
              if(res.errCode == 10000){
                  this.message.success("修改成功");
                  this.getEventPre()
              }else{
                  this.message.error(res.errMsg);
              }
          }
      )
    }

}
