import {Component, OnInit, ViewChild} from '@angular/core';
import {WorkService} from '../work.service';
import {pageSize, SERVER_API_URL} from '../../app.constants';
import {Router} from '@angular/router';
import {TypeAsetsComponent} from '../modal/type-asets/type-asets.component';
import {NzMessageService} from 'ng-zorro-antd';
import {UploadComponent} from '../modal/upload/upload.component';
import {CommonService} from '../../shared/common/common.service';
import {SystemService} from '../../system/system.service';

@Component({
  selector: 'smart-asset-part',
  templateUrl: './asset-part.component.html',
  styles: []
})
export class AssetPartComponent implements OnInit {
    @ViewChild('smartTypeAsets') smartTypeAsets:TypeAsetsComponent;
    @ViewChild('smartUpload') smartUpload:UploadComponent;

    timeFormat = 'yyyy-MM-dd';
    parentList;
    childrenList;
    timeBegin;
    timeEnd;
    pageCount = pageSize;
    pageNum = 1;
    totalNum;

    obj = {
        pageCount:null,
        pageNum:null,
        searchStr:'',
        type:'all',
        status:null,
        source:null,
        startTime:'',
        endTime:'',
    };
    constructor(private workSer:WorkService,private router:Router,private commonSer:CommonService,
                private messge:NzMessageService,private systemSer:SystemService) { }

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

    getTypeList(){
        this.getList();
        this.changeType();
    }

    //父级分类
    getList(){
        this.workSer.findByParentType().subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.parentList = res.data;
                }else{
                    this.messge.error(res.errMsg);
                }
            }
        )
    }

    //子级分类
    changeType(){
        this.workSer.findByParentidCount(this.obj.type).subscribe(
            (res)=>{
                this.childrenList = res.data;
            }
        )
    }

    change(e){
        this.pageNum = e;
        this.getList();
    }

    goToChild(data){
        this.router.navigate(['app/main/assetChild'],
            {
                queryParams:{
                    id:data.id
                }
            })
    }

    showAddModal(){
        this.smartTypeAsets.showAddModal("新增分类");
    }

    //编辑父级分类
    showPEditModal(){
        if(!this.obj.type){
            this.messge.error("请选择需要编辑的分类");
            return false;
        }
        this.smartTypeAsets.showEditModal("编辑分类",this.obj.type);
    }

    //删除父分类
    deleteParentType(){
        if(this.obj.type == "all"){
            this.messge.warning("请选择需要删除的分类");
            return false;
        }
        const data = {
            inventoryTypeIds:[]
        };
        data.inventoryTypeIds.push(this.obj.type);
        this.commonSer.confirmThing("删除","确定删除该父级分类",()=>{
            this.workSer.deleteType(data).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.messge.success("删除成功");
                        this.getList();
                    }else{
                        this.messge.error(res.errMsg);
                    }
                }
            )
        })
    }

    //编辑子级分类
    showCEditModal(id){
        this.smartTypeAsets.showEditModal("编辑分类",id);
    }

    //删除子级分类
    deleteChildType(item){
        const data = {
            inventoryTypeIds:[]
        };
        data.inventoryTypeIds.push(item.id);
        this.commonSer.confirmThing("删除","确定删除该子级分类",()=>{
            this.workSer.deleteType(data).subscribe(
                (res)=>{
                    if(res.errCode == 10000){
                        this.messge.success("删除成功");
                        this.changeType();
                    }else{
                        this.messge.error(res.errMsg);
                    }
                }
            )
        })
    }

    //导入资产
    showUploadModal(){
        this.smartUpload.showModal("上传资产文件");
    }

    //下载模版
    downLoad(){
        this.systemSer.downloadTemplate("inventory",SERVER_API_URL + "/api/template/download/").subscribe(
            (data)=>{
                this.commonSer.downloadFile("资产模版1",data);
            }
        )
    }
}
