import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { FormBuilder, Validators, FormGroup } from "@angular/forms";

import { ToolService } from "../../services/tool.service";
import { WashingBatchService } from './washing-batch.service';
@Component({
  selector: "app-washing-batch",
  templateUrl: "./washing-batch.component.html",
  styleUrls: ["./washing-batch.component.less"],
})
export class WashingBatchComponent implements OnInit {
  constructor(
    private washingBatchService: WashingBatchService,
    private route: ActivatedRoute,
    private tool: ToolService,
    public router: Router,
  ) {}

  userInfo: any = JSON.parse(localStorage.getItem("user")) || {}; //登录用户信息
  listOfData: any[] = []; //表格数据
  pageIndex: number = 1; //表格当前页码
  pageSize: number = 10; //表格每页展示条数
  listLength: number = 10; //表格总数据量
  tableHeight: number; //表格动态高
  modal: boolean = false; //新增/编辑模态框
  add: boolean; //true:新增；false:编辑
  validateForm: FormGroup; //新增/编辑表单
  coopId: any; //当前操作列
  currentHospital; //当前院区

  btnLoading: boolean = false; //提交按钮loading状态

  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //操作提示框是否展示
  modelName = ""; //模态框名称


  ngOnInit() {
    this.currentHospital = this.tool.getCurrentHospital();
    this.coopBtns = this.tool.initCoopBtns(this.route);
    this.getList(1);
  }

  // 初始化增删改按钮
  coopBtns: any = {};

  // 表格数据
  loading1 = false;
  getList(type) {
    if (type == 1) {
      this.pageIndex = 1;
    }
    let data = {
      idx: this.pageIndex - 1,
      sum: this.pageSize,
      clothesBatch: {
        hosId: this.currentHospital.id,
      },
    };
    this.loading1 = true;
    this.washingBatchService
      .getFetchDataList(data)
      .subscribe((data) => {
        this.loading1 = false;
        if (data.status == 200) {
          this.listOfData = data.list;
          this.listLength = data.totalNum;
        }
      });
  }

  // 展示信息提示框（con:提示信息，success:操作是否成功，promptInfo:操作结果提示信息）
  showPromptModal(con, success, promptInfo?) {
    this.promptModalShow = false;
    this.promptContent = con;
    this.ifSuccess = success;
    this.promptInfo = promptInfo;
    setTimeout(() => {
      this.promptModalShow = true;
    }, 100);
    this.getList(0);
  }

  delModal: boolean = false; //删除模态框
  tipsMsg1: string; //提示框信息
  tipsMsg2: string; //操作后信息
  confirmDelType: string; //确认的类型（启用/停用，删除）
  showDelModal(
    data,
    tipsMsg1: string,
    tipsMsg2: string,
    type: string,
  ) {
    this.confirmDelType = type;
    this.delModal = true;
    this.coopId = data;
    this.tipsMsg1 = tipsMsg1;
    this.tipsMsg2 = tipsMsg2;
  }
  // 隐藏弹窗
  hideDelModal() {
    this.delModal = false;
  }
  // 确认弹窗
  confirmDel() {
    this.btnLoading = true;
    if (this.confirmDelType === "recount") {
      //重新统计
      this.washingBatchService
        .simplePost("addData", "clothesBatch", {id: this.coopId.id, hosId: this.currentHospital.id})
        .subscribe((data) => {
          this.btnLoading = false;
          this.delModal = false;
          if (data.status == 200) {
            this.showPromptModal(this.tipsMsg2, true, "");
          } else {
            this.showPromptModal(this.tipsMsg2, false, data.msg);
          }
        });
    }
  }

  // 打开查看视图
  recallOrderShow: boolean = false;
  coopData:any = {};
  openRecallModal(data): void {
    this.coopData = data;
    if(data.batchState.value == 1){
      this.recallOrderShow = true;
    }else{
      this.router.navigateByUrl(`/washingBatchView/${this.coopData.id}/${this.coopData.createTime}`);
    }
  }
  // 设置为洗涤中
  confirmRec() {
    this.btnLoading = true;
    let postData = {
      id: this.coopData.id,
      stateType: "xdz",
    };
    this.washingBatchService
      .simplePost("addData", 'clothesBatch', postData)
      .subscribe((data) => {
        this.btnLoading = false;
        this.closeRecallOrderModal();
        if (data.status == 200) {
          this.showPromptModal("操作", true, "");
        } else {
          this.showPromptModal("操作", false, data.msg);
        }
      });
  }
  // 立即查看
  recAndDel() {
    this.router.navigateByUrl(`/washingBatchView/${this.coopData.id}/${this.coopData.createTime}`);
  }
  // 取消
  closeRecallOrderModal() {
    this.recallOrderShow = false;
  }
}


