import { Component, OnInit } from '@angular/core';
import { ProjectService } from '../../project.service'
import { filter } from 'core-js/fn/array';
import { log } from 'core-js/fn/log';
import { NzMessageService, NzNotificationService } from 'ng-zorro-antd';

@Component({
  selector: 'smart-node-project',
  templateUrl: './project-node.component.html',
  styles: [`
    .edit_hover {
      cursor: pointer;
    }
    .edit_hover:hover {
      color: #365ff5;
    }
  `]
})
export class ProjectNodeComponent implements OnInit {

  // @Output() done =  new EventEmitter<any>();

  // 项目节点参数
  isVisiable = false;
  modelLoad = false;
  panels = [
    {
      active: true,
      name: '通用文档',
      value: '1',
      childPanel: {
        name: 'This is panel header 1',
        list: [
          { label: '办会', value: '办会' },
          { label: '招投标', value: '招投标' },
          { label: '合同', value: '合同' }
        ]
      }
    },
    {
      active: false,
      name: '项目建设过程',
      value: '2',
      childPanel: {
        name: 'This is panel header 2',
        list: [
          { label: '立项申报', value: '立项申报' },
          { label: '需求设计', value: '需求设计' },
          { label: '实施过程', value: '实施过程' },
          { label: '试运行验收', value: '试运行验收' },
          { label: '项目监理', value: '项目监理' },
          { label: '系统测评', value: '系统测评' },
          { label: '审计审价', value: '审计审价' }
        ]
      }
    },
    {
      active: false,
      name: '运行维护',
      value: '3',
      childPanel: {
        name: 'This is panel header 3',
        list: [
          { label: '招标', value: '招标' },
          { label: '合同', value: '合同' },
          { label: '实施', value: '实施' },
          { label: '验收', value: '验收' }
        ]
      }
    },
    {
      active: false,
      name: '绩效',
      value: '4',
      childPanel: {
        name: 'This is panel header 4',
        list: [
          { label: '信息化绩效', value: '信息化绩效' },
          { label: '财务绩效', value: '财务绩效' }
        ]
      }
    },
    {
      active: false,
      name: '政务云',
      value: '5',
      childPanel: {
        name: 'This is panel header 5',
        list: [
          { label: '资源申请', value: '资源申请' },
          { label: '账号管理', value: '账号管理' },
          { label: '安全及备份', value: '安全及备份' },
          { label: '日常维护', value: '日常维护' },
          { label: '上线割接', value: '上线割接' }
        ]
      }
    }
  ];
  nodeType = {
    node1: '办会',
    node2: '立项申报',
    node3: '实施',
    node4: '信息化绩效',
    node5: '资源申请'
  };
  listOfData = [];
  listOfDataList = {
    node1: [],
    node2: [],
    node3: [],
    node4: [],
    node5: []
  }
  currData = null;
  jsonDataMid = {
    maintainProjectId: "",
    node1Name: "",
    node2Name: "",
    status: "0" // 1 删除 | 0 新增
  }
  jsonData = null;

  constructor(private projectSer: ProjectService, private message: NzMessageService) {
  }

  ngOnInit() {
  }

  // 项目节点函数
  showModal(data) {
    this.currData = data;
    this.jsonDataMid.maintainProjectId = this.currData.id;
    this.isVisiable = true;
    this.modelLoad = true;
    this.getList();
  }
  handleOk() {
    this.isVisiable = false;
  }
  nzChange(res, index) {
    if (res.type === "success") {
      console.log('nzChange', res);
      this.modelLoad = true;
      this.getList(this.jsonDataMid.node2Name, index);
    } else if(res.type !== "start" && res.type !== "progress") {
      this.modelLoad = false;
    }
  }
  handleCancel() { this.isVisiable = false; }
  beforeUpload = () => {
    this.modelLoad = true;
  }
  confirmJSON = (inx) => {
    this.jsonDataMid.node1Name = this.panels[inx].name;
    this.jsonDataMid.node2Name = this.nodeType['node' + (inx + 1)];
    this.jsonData = JSON.stringify(this.jsonDataMid);
    
    // console.log(this.jsonData);
    // return true;
  }
  selectChange(key, item) {
    console.log(key, item);

    this.listOfDataList['node' + key] = this.listOfData.filter(items =>
      items.node2Name === item
    )
  }
  viewFile(data) {
    // console.log('查看')
    this.modelLoad = true;
    this.projectSer.getFileDocument({
      id: data.id
    }).subscribe(
      (res) => {
        this.downloadFile(data.fileName, res);
      },error => {
        this.modelLoad = false;
        console.log('error:', error);
      }
    )
  }

  delFile(data, index) {
    // console.log('删除', data, index);
    this.modelLoad = true;
    this.projectSer.delProNodeFileList({
      id: data.id
    }).subscribe(
      (res) => {
        if (res.errCode === 10000) {
          this.message.success('删除成功！');
          this.getList(data.node2Name, index);
        }
      }
    )
  }

  editData(data, index) {
    console.log(data);
    this.modelLoad = true;
    let formdata = new FormData();
    if(!data.beiz || data.beiz===''|| data.beiz===null){
      this.message.info('备注不能为空！');
      this.modelLoad = false;
    }else {
      formdata.append('json', JSON.stringify({
        id: data.id,
        // node1Name: data.node1Name,
        // node2Name: data.node2Name,
        // status: data.status,
        beiz: data.beiz
      }))
      this.projectSer.putProNodeFileList(formdata).subscribe(
          (res) => {
            if (res.errCode === 10000) {
              this.modelLoad = false;
              data.edit = false;
              this.message.success('编辑成功！');
              this.getList(data.node2Name, index);
            }
          }
      )
    }
  }

  getList(node2Name ?: string, node2Index ?: string | number) {
    let initType = {
      node1: '办会',
      node2: '立项申报',
      node3: '招标',
      node4: '信息化绩效',
      node5: '资源申请'
    }
    initType['node' + node2Index] = node2Name;
    this.nodeType = initType;
    this.projectSer.getProNodeFileList({
      maintainProjectId: this.currData.id
    }).subscribe(
      (res) => {
        this.modelLoad = false;
        if (res.errCode === 10000) {
          // this.message.success('节点文件请求成功！');
          this.listOfData = res.data;

          Object.keys(this.panels).map((key, index) => {
            let node2ArrName = (index + 1) === node2Index ? node2Name :
                                  this.panels[key].childPanel.list[0].label;
            let arr = this.listOfData.filter(item => {
              if (item.node1Name === this.panels[key].name &&
                item.node2Name === node2ArrName) {
                item.edit = false;
                return item;
              }
            }
            )
            this.listOfDataList['node' + (index + 1)] = arr;
          })
          // console.log(this.listOfDataList);
        }
      },
      error => {
        this.modelLoad = false;
        console.log(error);
        
        this.message.success('节点文件请求失败，请检查网络是否通畅！');
      }
    )
  }

  getTarList() { }


  downloadFile(title, data) {
    this.modelLoad = true;
    const blob = new Blob([data]);
    const ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),
      ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
      ieEDGE = navigator.userAgent.match(/Edge/g),
      ieVer = (ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));
    if (ie && ieVer < 10) {
      this.message.error('No blobs on IE<10');
      return;
    }
    if (ieVer > -1) {
      window.navigator.msSaveBlob(blob);
    } else {
      this.modelLoad = false;
      const url = window.URL.createObjectURL(blob);
      let link = document.createElement('a');
      link.setAttribute('href', url);
      link.setAttribute('download', title);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      this.message.success('下载成功！');
    }
  }
}
