import { Component, OnInit, ViewChild } from '@angular/core';
import { EventComponent } from '../../work/work-handle/Event/event.component';
import { TransforComponent } from '../../work/modal/transfor/transfor.component';
import { SelectPersonComponent } from '../../modal/select-person/select-person.component';
import { pageSize } from '../../app.constants';
import { WorkService } from '../../work/work.service';
import { NzMessageService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { CommonService } from '../../shared/common/common.service';
import { DatePipe } from '@angular/common';
import { ProjectService } from '../project.service';
import { project_status } from '../project.constants';
import { CreateProjectComponent } from '../modal/create-project/create-project.component';
import { LocalStorageService } from 'ngx-webstorage';
import { LookProjectComponent } from '../modal/look-project/look-project.component';
import { ProjectNodeComponent } from '../modal/project-node/project-node.component';

@Component({
  selector: 'app-opr-project',
  templateUrl: './opr-project.component.html',
  styleUrls: ['./opr-project.component.css']
})
export class OprProjectComponent implements OnInit {
  @ViewChild('smartCreateProject') smartCreateProject: CreateProjectComponent;
  @ViewChild('smartLookProject') smartLookProject: LookProjectComponent;
  @ViewChild('smartProjectNode') smartProjectNode: ProjectNodeComponent;

  timeFormat = 'yyyy';

  statusList = [{ label: '全部', value: null }, ...project_status];

  page = {
    list: [],
    isLoading: false,
    pageCount: pageSize,
    pageNum: 1,
    totalNum: null
  };

  obj = {
    yearly: null,
    officeName: null,
    status: null,
    companyName: null,
    keyword: ''
  };
  yearList = [];
  userId;

  constructor(private workSer: WorkService, private message: NzMessageService, private projectSer: ProjectService,
    private $localStorage: LocalStorageService,
    private router: Router, private commonSer: CommonService, private datePipe: DatePipe) {
  }

  ngOnInit() {
    this.userId = this.$localStorage.retrieve('userInfo').userId;
    let nowYear = new Date().getFullYear();
    for (let i = nowYear; i > 2000; i--) {
      const y = {
        value: i,
        label: i
      };
      this.yearList.push(y);
    }
    this.getList();
  }

  //获取列表
  getList() {
    this.page.isLoading = true;
    const obj = {
      obj: this.obj,
      pageNum: this.page.pageNum,
      pageCount: this.page.pageCount
    };
    this.projectSer.selectMaintainProject(obj).subscribe(
      (res) => {
        if (res.errCode == 10000) {
          this.page.list = res.data.data;
          this.page.totalNum = res.data.totalNum;
          this.page.isLoading = false;
        }
      }
    );
  }

  //翻页
  change(e) {
    if (e > 0) {
      this.page.pageNum = e;
      this.getList();
    }
  }

  //搜索
  search() {
    this.page.pageNum = 1;
    this.getList();
  }

  // 新增项目节点
  handleOpenPointModal(data) {
    this.smartProjectNode.showModal(data);
  }

  //新增运维项目
  handleOpenModal() {
    this.smartCreateProject.showAddModal('新增运维项目');
  }

  //编辑运维项目
  handleEdit(data) {
    this.smartCreateProject.showEditModal(data, '编辑运维项目');
  }

  //查看
  handleLook(data) {
    console.log(this.smartLookProject);
    this.smartLookProject.showModal(data);
  }

  //删除
  handleDelete(item) {
    this.commonSer.confirmThing('删除', "确定删除该项目?", () => {
      const data = {
        id: item.id
      };
      this.projectSer.deleteMaintainProject(data).subscribe(
        (res) => {
          if (res.errCode == 10000) {
            this.message.success('删除成功!');
            this.getList();
          }
        }
      )
    })
  }

  //导出
  exportFile() {
    const data = {};
    this.projectSer.exportExcel(data).subscribe(
      (data) => {
        this.commonSer.downloadFile('运维项目.xlsx', data);
      }
    );
  }

}
