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

@Component({
  selector: "app-board001-create-board",
  templateUrl: "./board001-create-board.component.html"
})
export class Board001CreateBoardComponent implements OnInit {
  announceData: AnnounceData;
  errorMessage: string;
  loading = false;

  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 router: Router) {}

  ngOnInit() {
    this.announceData = new AnnounceData();
  }

  createAnnounceData() {
    this.announceData.StartOn = this.startDT.formatted;
    this.announceData.Status = 1;
    this.boardDataService.postAnnounceData(this.announceData).subscribe(res => {
      this.loading = true;
      const result = res.json();
      if (result.Success === true) {
        this.loading = false;
        swal({
          type: "success",
          title: "新增成功",
          text: "轉跳回檢視頁面...",
          showConfirmButton: false,
          timer: 1000,
          onOpen: () => {
            swal.showLoading();
          },
          onClose: () => {
            this.router.navigate(["/board/board001"]);
          }
        });
      } else {
        this.loading = false;
        swal({
          type: "error",
          title: "新增失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true
        });
      }
    });
  }

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