import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { FreightAccountService } from 'src/app/routes/financial-management/services/freight-account.service';
import { RecordedService } from '../../services/recorded.service';


@Component({
  selector: 'app-partner-recored-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less']
})
export class PartnerRecordedDetailComponent implements OnInit {
  @ViewChild('auditModal', { static: false }) auditModal!: any;
  formData: any = {};
  timeLineData: any = [];
  msg = '';
  id = '';

  constructor(public service: RecordedService, private route: ActivatedRoute, private nzModalService: NzModalService) {
    this.id = route.snapshot.params.id;
    this.getRecordedDetail(this.id);
  }

  ngOnInit(): void { }

  getRecordedDetail(id: string) {
    this.service.request(this.service.$api_get_recorded_record_detail, { id }).subscribe(res => {
      if (res) {
        this.formData = res;
        // 处理流程节点数据
        // 流程是否结束
        // let isEnd = false;
        // if (res.successTime) {
        //   isEnd = true;
        //   if (res.refundStatus === '3') {
        //     this.timeLineData.push({ time: res.successTime, value: `到账成功`, color: 'green' });
        //   } else {
        //     this.timeLineData.push({ time: res.successTime, value: `提现失败`, color: 'red' });
        //   }
        // }
        // if (res.agreeTime && res.refundStatus !== '4') {
        //   this.timeLineData.push({ time: res.agreeTime, value: `银行处理中`, color: 'gray' });
        // }
        // if (res.agreeTime) {
        //   if (res.refundStatus === '4') {
        //     isEnd = true;
        //     this.timeLineData.push({
        //       time: res.agreeTime,
        //       value: `拒绝提现<br/>操作人员：${res.handlerUserIdLabel}`,
        //       color: 'red'
        //     });
        //   } else {
        //     this.timeLineData.push({
        //       time: res.agreeTime,
        //       value: `审核通过<br/>操作人员：${res.handlerUserIdLabel}`,
        //       color: 'gray'
        //     });
        //   }
        // }
        // if (res.createTime) {
        //   this.timeLineData.push({
        //     time: res.createTime,
        //     value: `提交提现申请<br/>提现${res.amount}元至${res.bankName}(${res.bankCardNumber})<br/>操作人员：${res.userIdLabel}`,
        //     color: 'gray'
        //   });
        // }
        // if (this.timeLineData?.length > 0 && !isEnd) {
        //   this.timeLineData[0].color = 'green';
        // }
      }
    });
  }

  auditAction(item?: any, type: string = '1') {
    this.msg = '';
    let params: Array<string> = [];
    params = [item.id];
    const modal = this.nzModalService.create({
      nzTitle: type === '1' ? '审核' : '复审',
      nzContent: this.auditModal,
      nzFooter: [
        {
          label: '拒绝',
          type: 'default',
          onClick: () => {
            if (!this.msg || this.msg.trim().length === 0) {
              this.service.msgSrv.warning('请填写拒绝原因 ');
              return;
            }
            this.audit({ ids: params, rejectReason: this.msg, sts: '3' }, () => {
              modal.destroy(true);
            }, '审核拒绝成功');
          }
        },
        {
          label: '通过',
          type: 'primary',
          onClick: () => {
            this.audit({ ids: params, rejectReason: this.msg, sts: this.formData?.sts === '0' ? 1 : 2 }, () => {
              modal.destroy(true);
            }, `${this.formData?.sts === '1' ? '审核' : '复审'}通过成功`);
          }
        }
      ]
    });
    modal.afterClose.subscribe((res: any) => {
      if (res) {
        this.getRecordedDetail(this.id);
      }
    });
  }
  goBack() {
    history.go(-1);
  }

  /**
   * 审核
   */
  audit(params: any, callback: Function, msg = '成功') {
    this.service
      .request(this.service.$api_audit_recored, ...params)
      .subscribe(res => {
        if (res) {
          this.service.msgSrv.success(msg);
          callback();
        }
      });
  }
}
