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 { UserData } from "../../../data/UserData";
import swal from "sweetalert2";
import { AreaArrayData } from "app/data/SiteInformationData";

@Component({
  selector: "app-sys002-create-user",
  templateUrl: "./sys002-create-user.component.html"
})
export class Sys002CreateUserComponent implements OnInit {
  userData: UserData;
  selectGroup: any;
  selectRole: any;
  isAccountExisted: boolean;
  errorMessage: string;
  siteInformationList = [];
  areaArray: any;
  newAreaList: any = [];
  Site_Id: any;
  allIsChecked: any;

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

  ngOnInit() {
    this.userData = new UserData();
    this.userData.GroupCode_Id = "";
    this.userData.RoleCode_Id = "";
    this.userData.Site_Id = [];
    this.allIsChecked = false;
    this.selectRole = this.helperService.RoleCode;
    const t1 = setInterval(() => {
      if (this.helperService.RoleCode) {
        this.selectRole = this.helperService.RoleCode;
        clearInterval(t1);
      }
    }, 10);
    this.selectGroup = this.helperService.GroupCode;
    const t2 = setInterval(() => {
      if (this.helperService.GroupCode) {
        this.selectGroup = this.helperService.GroupCode;
        clearInterval(t2);
      }
    });

  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);
      }

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

  selectSiteInformation(id) {
    const index = this.userData.Site_Id.indexOf(id);
    if (index === -1) {
      this.userData.Site_Id.push(id);
    } else {
      this.userData.Site_Id.splice(index, 1);
    }
  }

  createNewUser() {
    this.sysDataService.postUserData(this.userData).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"
        });
      }
    });
  }

  checkUserNameExist() {
    this.sysDataService
      .getExistUserName(this.userData.Account)
      .subscribe(res => {
        const result = res.json().Result;

        this.isAccountExisted = result;
        // if (result === true) {
        //   console.log("帳號存在");
        // } else {
        //   console.log("此帳號可用");
        // }
      });
  }


  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 index1 = this.userData.Site_Id.indexOf(element.Id);
          if (index1 === -1) {
            this.userData.Site_Id.push(element.Id);
          } else {
            // this.userData.Site_Id.splice(index, 1);
          }

        } else {
          const index1 = this.userData.Site_Id.indexOf(element.Id);
          if (index1 === -1) {
            // this.userData.Site_Id.push(element.Id);
          } else {
            this.userData.Site_Id.splice(index1, 1);
          }
        }
        element.IsChecked = area.IsChecked;

      }

    }

  }

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


}
