import { Component, OnInit, ViewChild } from "@angular/core";
import { MainService } from "../../../services/main.service";
import { ToolService } from 'src/app/services/tool.service';
import { NzMessageService } from 'ng-zorro-antd';
import { Subject, forkJoin } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-ngx';

@Component({
  selector: "app-configuration-drug",
  templateUrl: "./configuration-drug.component.html",
  styleUrls: ["./configuration-drug.component.less"],
})
export class ConfigurationDrugComponent implements OnInit {
  @ViewChild("osComponentRef1", {
    read: OverlayScrollbarsComponent,
    static: false,
  })
  osComponentRef1: OverlayScrollbarsComponent;

  loading:boolean = false; //页面加载的loading
  btnLoading:boolean = false; //提交按钮的loading
  tabModalName:string = 'characteristics'; //当前选中的tab
  hosId = this.tool.getCurrentHospital().id; //当前院区

  // 配置
  configs:any = {};
  // 任务类型
  tasktype:any = {};
  constructor(private mainService: MainService,private tool: ToolService,private msg: NzMessageService) {}

  ngOnInit():void {
    this.activeDictionary = this.dictionaryKeyList[0];
    this.init();
  }

  // 工单模式
  drugsModelList:any[] = [
    { label: '一单一码', value: 1 },
    { label: '一单多码', value: 2 },
  ];

  // 手动填写
  drugsManualList:any[] = [];

  // 数据字典key列表
  dictionaryKeyList: any[] = [
    {
      name: '静配',
      value: 'drugsJpbag',
      postData: {
        drugsModel: undefined,//工单模式
        drugsCreateSign: [{ label:'是否开启', value: 0 }],//支持建单并签到
        drugsReceiveSign: [{ label:'是否开启', value: 0 }],//支持接单并签到
        drugsStartCheck: [{ label:'是否开启', value: 0 }],//起点科室支持核对
        drugsStartCheckScanIds: [],//起点核对方式
        drugsStartManual: undefined,//起点科室手动填写
        drugsStartPhoto: [{ label:'是否开启', value: 0 }],//起点科室支持拍照留存
        drugsEndCheck: [{ label:'是否开启', value: 0 }],//终点科室支持核对
        drugsEndCheckScanIds: [],//终点核对方式
        drugsEndManual: undefined,//终点科室手动填写
        drugsEndPhoto: [{ label:'是否开启', value: 0 }],//终点科室支持拍照留存
      }
    },
    {
      name: '西药',
      value: 'drugsWestern',
      postData: {
        drugsModel: undefined,//工单模式
        drugsCreateSign: [{ label:'是否开启', value: 0 }],//支持建单并签到
        drugsReceiveSign: [{ label:'是否开启', value: 0 }],//支持接单并签到
        drugsStartCheck: [{ label:'是否开启', value: 0 }],//起点科室支持核对
        drugsStartCheckScanIds: [],//起点核对方式
        drugsStartManual: undefined,//起点科室手动填写
        drugsStartPhoto: [{ label:'是否开启', value: 0 }],//起点科室支持拍照留存
        drugsEndCheck: [{ label:'是否开启', value: 0 }],//终点科室支持核对
        drugsEndCheckScanIds: [],//终点核对方式
        drugsEndManual: undefined,//终点科室手动填写
        drugsEndPhoto: [{ label:'是否开启', value: 0 }],//终点科室支持拍照留存
      }
    },
    {
      name: '退药',
      value: 'drugsReturn',
      postData: {
        drugsModel: undefined,//工单模式
        drugsCreateSign: [{ label:'是否开启', value: 0 }],//支持建单并签到
        drugsReceiveSign: [{ label:'是否开启', value: 0 }],//支持接单并签到
        drugsStartCheck: [{ label:'是否开启', value: 0 }],//起点科室支持核对
        drugsStartCheckScanIds: [],//起点核对方式
        drugsStartManual: undefined,//起点科室手动填写
        drugsStartPhoto: [{ label:'是否开启', value: 0 }],//起点科室支持拍照留存
        drugsEndCheck: [{ label:'是否开启', value: 0 }],//终点科室支持核对
        drugsEndCheckScanIds: [],//终点核对方式
        drugsEndManual: undefined,//终点科室手动填写
        drugsEndPhoto: [{ label:'是否开启', value: 0 }],//终点科室支持拍照留存
      }
    },
    {
      name: '毒麻',
      value: 'drugsPoison',
      postData: {
        drugsModel: undefined,//工单模式
        drugsCreateSign: [{ label:'是否开启', value: 0 }],//支持建单并签到
        drugsReceiveSign: [{ label:'是否开启', value: 0 }],//支持接单并签到
        drugsStartCheck: [{ label:'是否开启', value: 0 }],//起点科室支持核对
        drugsStartCheckScanIds: [],//起点核对方式
        drugsStartManual: undefined,//起点科室手动填写
        drugsStartPhoto: [{ label:'是否开启', value: 0 }],//起点科室支持拍照留存
        drugsEndCheck: [{ label:'是否开启', value: 0 }],//终点科室支持核对
        drugsEndCheckScanIds: [],//终点核对方式
        drugsEndManual: undefined,//终点科室手动填写
        drugsEndPhoto: [{ label:'是否开启', value: 0 }],//终点科室支持拍照留存
      }
    },
    {
      name: '中草药',
      value: 'drugsHerbal',
      postData: {
        drugsModel: undefined,//工单模式
        drugsCreateSign: [{ label:'是否开启', value: 0 }],//支持建单并签到
        drugsReceiveSign: [{ label:'是否开启', value: 0 }],//支持接单并签到
        drugsStartCheck: [{ label:'是否开启', value: 0 }],//起点科室支持核对
        drugsStartCheckScanIds: [],//起点核对方式
        drugsStartManual: undefined,//起点科室手动填写
        drugsStartPhoto: [{ label:'是否开启', value: 0 }],//起点科室支持拍照留存
        drugsEndCheck: [{ label:'是否开启', value: 0 }],//终点科室支持核对
        drugsEndCheckScanIds: [],//终点核对方式
        drugsEndManual: undefined,//终点科室手动填写
        drugsEndPhoto: [{ label:'是否开启', value: 0 }],//终点科室支持拍照留存
      }
    },
  ];

  // 点击数据字典key
  activeDictionary:any;
  clickDictionaryKey(item){
    this.activeDictionary = item;
    this.init();
  }

  // 修改起点科室支持核对
  changeDrugsStartCheck(e){
    console.log(e);
    if(!e[0].checked){
      this.activeDictionary.postData.drugsStartCheckScanIds = this.activeDictionary.postData.drugsStartCheckScanIds.map(v => ({...v, checked: false}));
      this.activeDictionary.postData.drugsStartManual = undefined;
      this.activeDictionary.postData.drugsStartPhoto[0].checked = false;
    }
  }

  // 修改终点科室支持核对
  changeDrugsEndCheck(e){
    console.log(e);
    if(!e[0].checked){
      this.activeDictionary.postData.drugsEndCheckScanIds = this.activeDictionary.postData.drugsEndCheckScanIds.map(v => ({...v, checked: false}));;
      this.activeDictionary.postData.drugsEndManual = undefined;
      this.activeDictionary.postData.drugsEndPhoto[0].checked = false;
    }
  }

  // 切换tab
  tabModal(tabModalName:string){
    this.tabModalName = tabModalName;
  }
  // 保存
  submitForm() {
    if(!this.tasktype.id){
      this.msg.create("warning", "请先配置任务类型！");
      return;
    }

    if(!this.activeDictionary.postData.drugsModel){
      this.msg.create("warning", "请选择工单模式！");
      return;
    }

    if(this.activeDictionary.postData.drugsStartCheck[0].checked && !this.activeDictionary.postData.drugsStartCheckScanIds.filter(v => v.checked).length){
      this.msg.create("warning", "请选择起点科室的核对方式！");
      return;
    }

    if(this.activeDictionary.postData.drugsEndCheck[0].checked && !this.activeDictionary.postData.drugsEndCheckScanIds.filter(v => v.checked).length){
      this.msg.create("warning", "请选择终点科室的核对方式！");
      return;
    }

    let postData:any = {
      id: this.configs.id,
      taskType: this.tasktype.id,
      hosId: this.hosId,
      drugsModel: this.activeDictionary.postData.drugsModel || undefined,
      drugsCreateSign: this.activeDictionary.postData.drugsCreateSign[0].checked ? 1 : 0,
      drugsReceiveSign: this.activeDictionary.postData.drugsReceiveSign[0].checked ? 1 : 0,
      drugsStartCheck: this.activeDictionary.postData.drugsStartCheck[0].checked ? 1 : 0,
      drugsStartCheckScanIds: this.activeDictionary.postData.drugsStartCheckScanIds.filter(v => v.checked).map(v => v.value).toString() || undefined,
      drugsStartManual: this.activeDictionary.postData.drugsStartManual ? { id: this.activeDictionary.postData.drugsStartManual } : undefined,
      drugsStartPhoto: this.activeDictionary.postData.drugsStartPhoto[0].checked ? 1 : 0,
      drugsEndCheck: this.activeDictionary.postData.drugsEndCheck[0].checked ? 1 : 0,
      drugsEndCheckScanIds: this.activeDictionary.postData.drugsEndCheckScanIds.filter(v => v.checked).map(v => v.value).toString() || undefined,
      drugsEndManual: this.activeDictionary.postData.drugsEndManual ? { id: this.activeDictionary.postData.drugsEndManual } : undefined,
      drugsEndPhoto: this.activeDictionary.postData.drugsEndPhoto[0].checked ? 1 : 0,
    };
    this.btnLoading = true;
    this.mainService
      .simplePost("addData", "taskTypeConfig", postData)
      .subscribe((result) => {
        this.btnLoading = false;
        if (result.status == 200) {
          this.getConfig();
        }
      });
  }

  //初始化
  init() {
    this.loading = true;
    let drugs_bag_check_scan_type$ = this.mainService.getDictionary('list', 'drugs_bag_check_scan_type', true);
    let drugs_bag_manual_type$ = this.mainService.getDictionary('list', 'drugs_bag_manual_type', true);
    forkJoin(drugs_bag_check_scan_type$, drugs_bag_manual_type$).subscribe((data:any[]) => {
      let drugs_bag_check_scan_type = data[0] || [];
      let drugs_bag_manual_type = data[1] || [];

      this.activeDictionary.postData.drugsStartCheckScanIds = drugs_bag_check_scan_type.map(v => ({ label:v.name, value: v.id }));
      this.activeDictionary.postData.drugsEndCheckScanIds = drugs_bag_check_scan_type.map(v => ({ label:v.name, value: v.id }));
      this.drugsManualList = drugs_bag_manual_type.map(v => ({ label:v.name, value: v.id }));

      this.getTaskType();
    })
  }

  //获取任务类型
  getTaskType() {
    this.loading = true;
    let postData = {
      idx: 0,
      sum: 1,
      taskType: {
        simpleQuery: true,
        hosId: {
          id: this.hosId
        },
        ordinaryField: {
          key: 'ordinary_field',
          value: this.activeDictionary.value,
        }
      }
    };
    this.mainService
      .getFetchDataList("simple/data", "taskType", postData)
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.tasktype = result.list[0] || {};
          this.getConfig();
        }
      });
  }

  // 获取配置
  getConfig() {
    this.loading = true;
    let postData = {
      idx: 0,
      sum: 1,
      taskTypeConfig: {
        taskTypeDTO: {
          hosId: {
            id: this.hosId
          },
          ordinaryField: {
            key: 'ordinary_field',
            value: this.activeDictionary.value,
          }
        }
      }
    };
    this.mainService
      .getFetchDataList("simple/data", "taskTypeConfig", postData)
      .subscribe((result) => {
        this.loading = false;
        if (result.status == 200) {
          this.configs = result.list[0] || {};
          this.activeDictionary.postData.drugsModel = this.configs.drugsModel || undefined;
          this.activeDictionary.postData.drugsCreateSign[0].checked = this.configs.drugsCreateSign == 1;
          this.activeDictionary.postData.drugsReceiveSign[0].checked = this.configs.drugsReceiveSign == 1;

          this.activeDictionary.postData.drugsStartCheck[0].checked = this.configs.drugsStartCheck == 1;
          this.activeDictionary.postData.drugsStartCheckScanIds = this.activeDictionary.postData.drugsStartCheckScanIds.map(v => ({...v, checked: (this.configs.drugsStartCheckScanIds || '').split(',').includes(v.value.toString())}));
          this.activeDictionary.postData.drugsStartManual = this.configs.drugsStartManual ? this.configs.drugsStartManual.id : undefined;
          this.activeDictionary.postData.drugsStartPhoto[0].checked = this.configs.drugsStartPhoto == 1;

          this.activeDictionary.postData.drugsEndCheck[0].checked = this.configs.drugsEndCheck == 1;
          this.activeDictionary.postData.drugsEndCheckScanIds = this.activeDictionary.postData.drugsEndCheckScanIds.map(v => ({...v, checked: (this.configs.drugsEndCheckScanIds || '').split(',').includes(v.value.toString())}));
          this.activeDictionary.postData.drugsEndManual = this.configs.drugsEndManual ? this.configs.drugsEndManual.id : undefined;
          this.activeDictionary.postData.drugsEndPhoto[0].checked = this.configs.drugsEndPhoto == 1;
        }
      });
  }
}
