import {Component, OnInit, Output, EventEmitter} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {InfomationService} from '../../infomation.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Asset_Location, asset_type, data_type} from '../../information.constans';
import {MonitorService} from '../../../monitor/monitor.service';

@Component({
    selector: 'smart-create-property',
    templateUrl: './create-property.component.html',
    styles: []
})
export class CreatePropertyComponent implements OnInit {
    @Output() done = new EventEmitter<any>();

    modal = {
        isVisiable: false,
        title: '',
        id: '',
        isOkLoading:false,
    };

    validateForm: FormGroup;
    controlArray = [];
    controlArrayDemo = [];
    assetType = asset_type;
    parentTypsList = Asset_Location;
    dataTypeList = data_type;
    assetTypeForHtml;
    classification;

    constructor(private fb: FormBuilder, private infoSer: InfomationService,
                private message: NzMessageService,
                private monitorSer: MonitorService) {
    }

    ngOnInit() {
        this.initForm();
    }

    initForm() {
        this.controlArrayDemo = [];
        this.controlArray = [];
        this.controlArrayDemo.push(
            {label: '设备编码', value: 'equipmentCode', type: 'input', isRequire: false,},
            {label: '资产名称', value: 'name', type: 'input', isRequire: true, isDiff: false},
            {label: '资产分类', value: 'classification', type: 'select', isRequire: true,},
            {label: '父对象编号', value: 'parentId', type: 'select', isRequire: true,},
            {label: '资产模型编号', value: 'dataTypeId', type: 'select', isRequire: true,},
            {label: '版本型号', value: 'versionMode', type: 'input', isRequire: false},
            {label: '购买合同日期', value: 'buyDate', type: 'date', isRequire: false},
            {label: '资产单价', value: 'unitPrice', type: 'input', isRequire: false},
            {label: '资产总价', value: 'totalPrice', type: 'input', isRequire: false},
            {label: '归属项目', value: 'attributionProject', type: 'input', isRequire: false},
            {label: '归属部门', value: 'attributionDepartments', type: 'input', isRequire: false},
            {label: '供应商', value: 'supplier', type: 'input', isRequire: false},
            {label: '联系人', value: 'contacts', type: 'input', isRequire: false},
            {label: '操作系统', value: 'operatingSystem', type: 'input', isRequire: false},
            {label: '保修到期时间', value: 'expirationDate', type: 'date', isRequire: false},
            {label: '序列号', value: 'serialNumber', type: 'input', isRequire: false},
            {label: 'cpu型号', value: 'cpuModel', type: 'input', isRequire: false},
            {label: 'cpu核数', value: 'cpuMultiNumber', type: 'input', isRequire: false},
            {label: '初始内存数量', value: 'memoryCount', type: 'input', isRequire: false},
            {label: '内存变化时间', value: 'memoryChangeDate', type: 'date', isRequire: false},
            {label: '虚拟化软件', value: 'virtualizationSoftware', type: 'input', isRequire: false},
            {label: '安装软件', value: 'installedSoftware', type: 'input', isRequire: false},
            {label: '已使用的软件授权', value: 'softwareAuthorization', type: 'input', isRequire: false},
            {label: '内部ip地址', value: 'ipInternalAddress', type: 'input', isRequire: false},
            {label: '外部IP地址', value: 'ipExternalAddress', type: 'input', isRequire: false},
            {label: '所在网络', value: 'locatedNetwork', type: 'input', isRequire: false},
            {label: '维修情况', value: 'maintenanceSituation', type: 'input', isRequire: false},
            {label: '备注', value: 'remark', type: 'textarea', isRequire: false},
            {label: '机柜名称', value: 'cabinetName', type: 'input', isRequire: false},
            {label: '存放地点', value: 'place', type: 'input', isRequire: false},
            {label: '登录方式', value: 'loginMode', type: 'input', isRequire: false},
            {label: '登录密码', value: 'loginPassword', type: 'input', isRequire: false},
            {label: '新密码', value: 'newPassword', type: 'input', isRequire: false},
        );
        this.validateForm = this.fb.group({
            equipmentCode: [null], name: [null, [Validators.required]],
            classification: ['1', [Validators.required]], versionMode: [null],
            parentId: [null, [Validators.required]], dataTypeId: [null, [Validators.required]],
            buyDate: [null], unitPrice: [null],
            totalPrice: [null], attributionProject: [null],
            attributionDepartments: [null], supplier: [null],
            contacts: [null], operatingSystem: [null],
            expirationDate: [null], serialNumber: [null],
            cpuNumber: [null], cpuModel: [null],
            cpuMultiNumber: [null], hyperthreads: [null],
            memoryCount: [null], memoryChangeCount: [null],
            memoryChangeDate: [null], disksNumber: [null],
            disksCapacity: [null], disksRaid: [null],
            disksAvailableCapacity: [null], isVirtualization: [null],
            virtualizationSoftware: [null], disksMountArrays: [null],
            installedSoftware: [null], softwareAuthorization: [null],
            ipInternalAddress: [null], ipExternalAddress: [null],
            networkPortConnection: [null], locatedNetwork: [null],
            domainName: [null], relatedApplicationProcess: [null],
            maintenanceSituation: [null], remark: [null],
            cabinetName: [null], place: [null],
            relatedEntityServer: [null], diskSystemCapacity: [null],
            diskDataCapacity: [null], relatedStorage: [null],
            changeSituation: [null], deploymentDomain: [null],
            uses: [null],
            loginMode: [null], loginPassword: [null],
            newPassword: [null],
        });

        this.assetTypeForHtml = {
            one: [
                {label: '超线程数', value: 'hyperthreads', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '内存变化数量', value: 'memoryChangeCount', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '磁盘数量', value: 'disksNumber', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '磁盘容量', value: 'disksCapacity', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '磁盘raid', value: 'disksRaid', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '可用磁盘容量', value: 'disksAvailableCapacity', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '是否虚拟化', value: 'isVirtualization', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '挂载磁盘阵列', value: 'disksMountArrays', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '网络端口连接', value: 'networkPortConnection', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '网络域名', value: 'domainName', type: 'input', isRequire: false},  //实体服务器独有属性
                {label: '关联开通服务器申请流程', value: 'relatedApplicationProcess', type: 'textarea', isRequire: false},  //实体服务器独有属性
            ],  //实体服务器
            two: [
                {label: '关联实体服务器', value: 'relatedEntityServer', type: 'input', isRequire: false},  //虚拟服务器
                {label: '关联存储', value: 'relatedStorage', type: 'input', isRequire: false},  //虚拟服务器
                {label: '系统盘容量', value: 'diskSystemCapacity', type: 'input', isRequire: false}, //政务云服务器&&虚拟服务器
                {label: '数据盘容量', value: 'diskDataCapacity', type: 'input', isRequire: false},  //政务云服务器&&虚拟服务器
                {label: '变化情况', value: 'changeSituation', type: 'input', isRequire: false},  //政务云服务器&&虚拟服务器
            ],  //虚拟服务器
            three: [
                {label: '系统盘容量', value: 'diskSystemCapacity', type: 'input', isRequire: false}, //政务云服务器&&虚拟服务器
                {label: '数据盘容量', value: 'diskDataCapacity', type: 'input', isRequire: false},  //政务云服务器&&虚拟服务器
                {label: '变化情况', value: 'changeSituation', type: 'input', isRequire: false},  //政务云服务器&&虚拟服务器
                {label: '部署域', value: 'deploymentDomain', type: 'input', isRequire: false},  //政务云服务器
                {label: '用途', value: 'uses', type: 'input', isRequire: false}, //政务云服务器
            ],  //政务云服务器
        };
        this.controlArray = this.controlArray.concat(this.controlArrayDemo);
        console.log(this.controlArray);
    }

    //
    showAddModal(title) {
        this.modal.isVisiable = true;
        this.modal.title = title;
    }

    showEditModal(title, id) {
        this.modal.isVisiable = true;
        this.modal.title = title;
        this.modal.id = id;
        this.getDetail();
    }

    handleCancel() {
        this.modal.isVisiable = false;
        this.modal.isOkLoading = true;
        this.initForm();
    }

    //查询详情
    getDetail() {
        this.infoSer.propertyFindById(this.modal.id).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.validateForm.patchValue(res.data);
                }
            }
        );
    }

    //改变分类
    changeType(e) {
        this.controlArray = [];
        if (e == '1') this.controlArray = this.controlArray.concat(this.controlArrayDemo, this.assetTypeForHtml.one);
        if (e == '2') this.controlArray = this.controlArray.concat(this.controlArrayDemo, this.assetTypeForHtml.two);
        if (e == '3') this.controlArray = this.controlArray.concat(this.controlArrayDemo, this.assetTypeForHtml.three);
    }

    //判断
    handleOk() {
        for (let i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
        if (this.validateForm.invalid) {
            return false;
        }
        this.modal.isOkLoading = true;
        if (this.validateForm.value.buyDate) this.validateForm.patchValue({'buyDate': new Date(this.validateForm.value.buyDate).getTime()});
        if (this.validateForm.value.expirationDate) this.validateForm.patchValue({'expirationDate': new Date(this.validateForm.value.expirationDate).getTime()});
        if (this.validateForm.value.memoryChangeDate) this.validateForm.patchValue({'memoryChangeDate': new Date(this.validateForm.value.memoryChangeDate).getTime()});
        if (this.modal.title.includes('新增')) this.create();
        if (this.modal.title.includes('编辑')) this.update();
    }

    //新增
    create() {
        console.log(this.validateForm.value);
        this.infoSer.propertyAdd(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisiable = false;
                    this.message.success('新增成功');
                    this.monitorProperty(res.data);
                }
                this.modal.isOkLoading = true;
            }
        );
    }

    //3D模型增加
    monitorProperty(id) {
        const data = {
            module: 'PEMS',
            data: [
                {
                    id: id,
                    name: this.validateForm.value.name, //资产名称
                    description: '',
                    parentId: this.validateForm.value.parentId,   //required
                    dataTypeId: this.validateForm.value.dataTypeId,  //required
                }
            ]
        };
        this.monitorSer.asset(data).subscribe(
            (res) => {
                if (res.value == 'fail') {
                    this.message.error(`错误码:${res.code},${res.error}`);
                }
            }
        );
    }

    //更新
    update() {
        this.validateForm.addControl('id', new FormControl(this.modal.id));
        this.infoSer.propertyUpdate(this.validateForm.value).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.initForm();
                    this.done.emit();
                    this.modal.isVisiable = false;
                    this.updateMonitor(res.data);
                    this.message.success('更新成功');
                }
                this.modal.isOkLoading = true;
            }
        );
    }

    //3D模型更新
    updateMonitor(id) {
        const data = {
            module: 'PEMS',
            data: {
                id: id,
                name: this.validateForm.value.name, //资产名称
                description: '',
                parentId: this.validateForm.value.parentId,   //required
                dataTypeId: this.validateForm.value.dataTypeId,  //required
            }
        };
        this.monitorSer.assetPut(id, data).subscribe(
            (res) => {

            }
        );
    }

}
