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

import { MainService } from "../../services/main.service";
import { ToolService } from "../../services/tool.service";
import { Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";

@Component({
  selector: "app-advice-management",
  templateUrl: "./advice-management.component.html",
  styleUrls: ["./advice-management.component.less"],
})
export class AdviceManagementComponent implements OnInit {
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private mainService: MainService,
    private tool: ToolService
  ) {}
  changeInpSubject = new Subject();
  ngOnInit() {
    this.changeInpSubject.pipe(debounceTime(500)).subscribe((v) => {
      this.getDeparts(v);
    });
    this.coopBtns = this.tool.initCoopBtns(this.route);
    this.getAllHos();
    this.getReplyTypes();
  }

  listOfData: any[] = []; //表格数据

  modal: boolean = false; //回复模态框
  coopId: number; //表格中执行操作的id
  department: any; //所属科室
  num; //工号
  name; //姓名
  replyType: any; //选择状态
  alldepart: any = []; //所有所属科室
  replyTypes: any; //所有状态
  pageIndex: number = 1; //页码
  listLength: number = 10; //总条数
  pageSize: number = 10; //每页条数

  promptContent: string; //操作提示框提示信息
  ifSuccess: boolean; //操作成功/失败
  promptInfo: string; //操作结果提示信息
  promptModalShow: boolean; //操作提示框是否展示

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

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

  // 获取院区
  hospital: string; //选中院区
  getAllHos() {
    this.hospital = this.tool.getCurrentHospital().id + "";
    this.changeHos();
    this.getList();
  }
  // 修改院区获取对应科室
  changeHos() {
    this.department = null;
    this.getDeparts();
  }
  // 搜索
  search() {
    this.pageIndex = 1;
    this.getList();
  }
  // 重置
  reset() {
    this.pageIndex = 1;
    this.replyType = null;
    this.department = null;
    this.getList();
  }
  // 表格数据
  loading1 = false;
  getList() {
    var that = this;
    let data = {
      idx: that.pageIndex - 1,
      sum: that.pageSize,
      advice: {
        replaceFlag: that.replyType || "",
        hosId: that.hospital,
        creatDepartment: {
          id: that.department || "",
        },
      },
    };
    if (!data.advice.replaceFlag) {
      delete data.advice.replaceFlag;
    }
    if (!data.advice.creatDepartment || !data.advice.creatDepartment.id) {
      delete data.advice.creatDepartment;
    }
    this.loading1 = true;
    that.mainService
      .getFetchDataList("adviceCollection", "advice", data)
      .subscribe((data) => {
        this.loading1 = false;
        that.listOfData = data.list;
        that.listLength = data.totalNum;
      });
  }

  // 获取所有科室
  timerNum = 0; //边输入边搜索的定时器
  getDeparts(dept?) {
    var that = this;
    let data = {
      department: {
        hospital: { id: that.hospital },
        dept: dept || "",
      },
      idx: 0,
      sum: 20,
    };
    this.timerNum++;
    that.mainService
      .getFetchDataList("data", "department", data)
      .subscribe((data) => {
        console.log(data);
        that.alldepart = data.list;
        this.timerNum--;
        if (this.timerNum === 0) {
          that.isLoading = false;
        }
      });
  }

  // 获取所属组
  getReplyTypes() {
    var that = this;
    that.replyTypes = [
      {
        value: 0,
        label: "未回复",
      },
      {
        value: 1,
        label: "已回复",
      },
    ];
  }

  // 新增弹框
  replyCon: any; //意见内容
  reply(data) {
    this.modal = true;
    this.replyCon = data;
  }
  hideModal() {
    this.modal = false;
  }
  // 回复
  adviceSubContent: string; //回复内容
  submitForm() {
    console.log(this.adviceSubContent);
    let that = this;
    if (!that.adviceSubContent) return;
    that.btnLoading = true;
    let postData = {
      advice: {
        id: that.replyCon.id,
        replyUser: { id: that.replyCon.createUser.id },
        replyContent: that.adviceSubContent,
      },
    };
    that.mainService
      .postCustom("adviceCollection", "updData/advice", postData)
      .subscribe((data) => {
        that.btnLoading = false;
        that.adviceSubContent = "";
        if (data.status == 200) {
          that.showPromptModal("回复", true, "");
          that.getList();
          that.hideModal();
        } else {
          that.showPromptModal("回复", false, data.msg);
        }
      });
  }

  // 展示信息提示框（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();
  }

  // 查看
  detail(id) {
    this.router.navigateByUrl("/main/adviceManagement/adviceDetail/" + id);
  }

  // 边输边搜节流阀
  isLoading = false;
  changeInp(e) {
    this.isLoading = true;
    this.changeInpSubject.next(e);
  }

  // 截取意见内容(ie内核截取)
  spliceContent(con) {
    if (con.length >= 41 && navigator.userAgent.indexOf("Trident") > -1) {
      return con.slice(0, 20) + "...";
    } else {
      return con;
    }
  }
}
