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

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

    @ViewChild('smartDict') smartDict: DictComponent;

    eventList;
    levelList;
    isDisabled = true;
    eventPre = {
        eventNoPre: '',
        id: ''
    };

    constructor(private systemChangeSer: SystemChangeService, private message: NzMessageService,
                private commomSer: CommonService) {
    }

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

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

    //获取分类列表
    getList() {
        this.systemChangeSer.findByType("sys_change_type").subscribe(
            (res) => {
                if (res.data) {
                    this.eventList = res.data;
                } else {
                    this.message.warning('暂无变更类型');
                }
            }
        );
        this.systemChangeSer.findByType('sys_change_level').subscribe(
            (res) => {
                if (res.data) {
                    this.levelList = res.data;
                } else {
                    this.message.warning('暂无变更级别');
                }
            }
        );
    }

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

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

    //删除分类
    deleteItem(item) {
        this.commomSer.confirmThing('删除', '确定删除该分类?', () => {
            const data = {
                ids: []
            };
            data.ids.push(item.id);
            this.systemChangeSer.sysdictDelete(data).subscribe(
                (res)=>{
                  if(res.errCode == 10000){
                    this.message.success("删除成功");
                  }
                  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);
        //       }
        //     }
        // )
    }
}
