import { SetDataService } from "../../../service/set-data.service";
import { Router } from "@angular/router";
import swal from "sweetalert2";

import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { AnnounceData } from "app/data/AnnounceData";
import { BoardDataService } from "app/service/board-data.service";
import { IMyDpOptions } from "mydatepicker";

@Component({
  selector: "app-board001-set-board",
  templateUrl: "./board001-set-board.component.html"
})
export class Board001SetBoardComponent implements OnInit {
  errorMessage: String;
  data: any;
  announceData: AnnounceData;

  selectedItem: any;
  itemId: number;
  startDT: any;

  public myDatePickerOptions: IMyDpOptions = {
    dateFormat: 'yyyy/mm/dd',
    editableDateField: false,
    openSelectorOnInputClick: true,
    dayLabels: {
      su: '日',
      mo: '一',
      tu: '二',
      we: '三',
      th: '四',
      fr: '五',
      sa: '六',
    },
    monthLabels: {
      1: '一月',
      2: '二月',
      3: '三月',
      4: '四月',
      5: '五月',
      6: '六月',
      7: '七月',
      8: '八月',
      9: '九月',
      10: '十月',
      11: '十一月',
      12: '十二月',
    },
    todayBtnTxt: '今天',
  };

  constructor(
    private boardDataService: BoardDataService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.announceData = new AnnounceData();
    this.route.data.subscribe((data: { selectedItem: any }) => {
      this.selectedItem = data.selectedItem;
      this.startDT = this.selectedItem.StartOn;
    });
  }

  doEditAnnounce() {
    this.selectedItem.StartOn = this.startDT.formatted;
    this.boardDataService.putAnnounceData(this.selectedItem).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(["board/board001"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "修改失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true
        });
      }
    });
  }

  backPreviousPage() {
    this.router.navigateByUrl("/board/board001");
  }
}
