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;

    isLoading = true;
    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 message:NzMessageService,private systemSer:SystemService) { }

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

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

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

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

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

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

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

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

    //删除父分类
    deleteParentType(){
        if(this.obj.type == "all"){
            this.message.warning("请选择需要删除的分类");
            return false;
        }
        if(this.childrenList.length > 0){
            this.message.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.message.success("删除成功");
                        this.getList();
                    }else{
                        this.message.error(res.errMsg);
                    }
                }
            )
        })
    }

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

    //删除子级分类
    deleteChildType(item){
        this.workSer.findInventory(item.id).subscribe(
            (res) => {
               if(res.data.length > 0 ){
                   this.message.warning("删除失败，请先清除资产！");
               }else{
                   const data = {
                       inventoryTypeIds:[]
                   };
                   data.inventoryTypeIds.push(item.id);
                   this.commonSer.confirmThing("删除","确定删除该子级分类",()=>{
                       this.workSer.deleteType(data).subscribe(
                           (res)=>{
                               if(res.errCode == 10000){
                                   this.message.success("删除成功");
                                   this.changeType();
                               }else{
                                   this.message.error(res.errMsg);
                               }
                           }
                       )
                   })
               }
            }
        );
    }

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

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