import { SiteData } from './../../../data/SiteInformationData';
import { SysDataService } from "../../../service/sys-data.service";
import { HelperService } from "../../../service/helper.service";
import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { ActivatedRoute } from "@angular/router";
import swal from "sweetalert2";
import { AreaArrayData } from "app/data/SiteInformationData";

@Component({
  selector: "app-sys002-set-user",
  templateUrl: "./sys002-set-user.component.html"
})
export class Sys002SetUserComponent implements OnInit {
  errorMessage: String;
  data: any;
  selectRole: any;
  selectGroup: any;
  selectedUser: any;
  roleId: number;
  groupId: number;

  siteInformationList = [];
  areaArray: any;
  Site_Id: any;
  newAreaList: any = [];

  constructor(
    private sysDataService: SysDataService,
    private helperService: HelperService,
    private router: Router,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    this.selectRole = this.helperService.RoleCode;
    this.selectGroup = this.helperService.GroupCode;
    this.route.data.subscribe((data: { selectedUser: any }) => {
      this.selectedUser = data.selectedUser;
      this.roleId = this.selectedUser.RoleCode_Id;
      this.groupId = this.selectedUser.GroupCode_Id;
    });
    this.getSiteInformationAll();


  }

  getSiteInformationAll(): any {
    this.sysDataService.getSiteInformationAll().subscribe(res => {
      const result = res.json().Result;

      var allArea = result.map(function(item) {
        return item.Area;
    });
      this.areaArray = allArea.filter(function(element, index, arr) {
        return arr.indexOf(element) === index;
    });

      this.siteInformationList = result;

      for (let index = 0; index < this.areaArray.length; index++) {
        const element = this.areaArray[index];
        const newArea = new AreaArrayData();
        newArea.Area = element;
        newArea.IsChecked = false;
        this.newAreaList.push(newArea);
      }

      for (let index = 0; index < this.siteInformationList.length; index++) {
        const element = this.siteInformationList[index];
        element.IsChecked = false;
        for (let i = 0 ; i < this.selectedUser.Site.length; i++) {
          if (element.SiteID === this.selectedUser.Site[i].SiteId) {
            element.IsChecked = true;
          }
        }
      }

    }, error => (this.errorMessage = <any>error));
  }

  selectSiteInformation(id) {
    const sitedata = new SiteData();
    sitedata.Id = id.Id;
    sitedata.SiteId = id.SiteID;
    sitedata.SiteName = id.SiteName;

    const index = this.selectedUser.Site.map(function(e) { return e.SiteId; }).indexOf(sitedata.SiteId);
    if (index === -1) {
      this.selectedUser.Site.push(sitedata);
    } else {
      this.selectedUser.Site.splice(index, 1);
    }
  }

  isChecked(Id) {

    for (let i = 0 ; i < this.selectedUser.Site.length; i++) {
      if (this.selectedUser.Site[i].SiteId === Id) {
        return 1;
      }
    }
    return 0;
  }

  doEditUser() {
    this.selectedUser.Site_Id = [];
    for (let i = 0 ; i < this.selectedUser.Site.length; i++) {
      this.selectedUser.Site_Id.push(this.selectedUser.Site[i].Id);
    }

    this.sysDataService.putUserData(this.selectedUser).subscribe(res => {
      const result = res.json();
      if (result.Success === true) {
        swal({
          type: "success",
          title: "修改成功",
          text: "轉跳回檢視頁面...",
          showConfirmButton: false,
          timer: 1000,
          onOpen: () => {
            swal.showLoading();
          },
          onClose: () => {
            this.router.navigate(["sys/sys002"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "修改失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      }
    });
  }

  selectAllSiteInformation(area: AreaArrayData) {
    for (let index = 0; index < this.siteInformationList.length; index++) {
      const element = this.siteInformationList[index];
      if (element.Area === area.Area) {
        if (area.IsChecked) {
          const sitedata = new SiteData();
          sitedata.Id = element.Id;
          sitedata.SiteId = element.SiteID;
          sitedata.SiteName = element.SiteName;
          const index1 = this.selectedUser.Site.map(function(e) { return e.SiteId; }).indexOf(sitedata.SiteId);
          if (index1 === -1) {
            this.selectedUser.Site.push(sitedata);
          } else {
          }

        } else {
          const sitedata = new SiteData();
          sitedata.Id = element.Id;
          sitedata.SiteId = element.SiteID;
          sitedata.SiteName = element.SiteName;
          const index1 = this.selectedUser.Site.map(function(e) { return e.SiteId; }).indexOf(sitedata.SiteId);
          if (index1 === -1) {
          } else {
            this.selectedUser.Site.splice(index1, 1);
          }
        }
        element.IsChecked = area.IsChecked;

      }

    }

  }


  backPreviousPage() {
    this.router.navigateByUrl("/sys/sys002");
  }
}
