import { Component, Input, SimpleChanges } from '@angular/core';
import { CommonNzModule } from './../../../../../../common.nz.module';

interface FlowItem {
  creator: string;
  createTime: string;
  status: number;
  action: string;
  toNext: boolean;
  hideNameAndTime?: boolean;
  additionText?: string;
  additionColor?: string;
}

interface ActionConfig {
  title: string;
  activeColor: string;
  icon: string;
}

const flowActionMap: Record<string, ActionConfig> = {
  submit: {
    title: '提交问题',
    activeColor: '#2953E8',
    icon: 'icons:hazard/submit',
  },
  audit: {
    title: '隐患审核',
    activeColor: '#DF0000',
    icon: 'icons:hazard/audit',
  },
  plan: {
    title: '治理方案',
    activeColor: '#FA642A',
    icon: 'icons:hazard/plan',
  },
  planAudit: {
    title: '方案审核',
    activeColor: '#01C49B',
    icon: 'icons:hazard/plan-audit',
  },
  handle: {
    title: '隐患治理',
    activeColor: '#7C1CFF',
    icon: 'icons:hazard/handle',
  },
  accept: {
    title: '治理验收',
    activeColor: '#4DC672',
    icon: 'icons:hazard/accept',
  },
};

@Component({
  selector: 'hazard-detail-flow',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './hazard-flow.component.html',
  styleUrl: './hazard-flow.component.less',
})
export class HazardFlowComponent {
  @Input() data!: Hazard.HazardDto;

  ngOnChanges(changes: SimpleChanges) {
    if (changes['data']) {
      this.flowList = this.getFlowList();
    }
  }
  flowList: FlowItem[] = [];
  getFlowList() {
    const list: FlowItem[] = this.getEmptyRemainFlow();
    this.fillSubmit(list);
    this.fillAudit(list);
    this.fillPlan(list);
    this.fillHandle(list);

    return list;
  }
  getEmptyRemainFlow(): FlowItem[] {
    return [
      {
        creator: '',
        createTime: '',
        status: 1,
        action: 'submit',
        toNext: false,
      },
      {
        creator: '',
        createTime: '',
        status: 0,
        action: 'audit',
        toNext: false,
      },
      {
        creator: '',
        createTime: '',
        status: 0,
        action: 'plan',
        toNext: false,
      },
      {
        creator: '',
        createTime: '',
        status: 0,
        action: 'planAudit',
        toNext: false,
      },
      // {
      //   creator: '',
      //   createTime: '',
      //   status: 0,
      //   action: 'handle',
      //   toNext: false,
      // },
      // {
      //   creator: '',
      //   createTime: '',
      //   status: 0,
      //   action: 'accept',
      //   toNext: false,
      // },
    ];
  }
  fillSubmit(flow: FlowItem[]) {
    const item = flow[0];
    if (!item) {
      return;
    }
    item.creator = this.data.createUserName;
    item.createTime = this.data.createdDate;
    item.toNext = this.data.status !== 2;
  }
  fillAudit(flow: FlowItem[]) {
    const item = flow[1];
    if (!item) {
      return;
    }
    item.status = 1;
    if (this.data.status !== 0) {
      // 通过或拒绝
      item.creator = this.data.planCheckUserName;
      item.createTime = this.data.checkDate;
      item.additionText = this.data.status === 1 ? '已通过' : '已拒绝';
      item.additionColor = this.data.status === 1 ? '#4DC672' : '#DF0000';
      item.toNext = this.data.status === 1;
    } else {
      item.toNext = false;
      item.additionText = '待审核';
      item.additionColor = '#FA642A';
      item.hideNameAndTime = true;
    }
  }
  fillPlan(flow: FlowItem[]) {
    const planSubmitItem = flow[2];
    const planAuditItem = flow[3];
    if (!planSubmitItem || !planAuditItem) {
      return;
    }
    if (this.data.planStatus !== 0) {
      // 已审核， 通过或拒绝
      planSubmitItem.creator = this.data.plan.updateUserName!;
      planSubmitItem.createTime = this.data.plan.updatedDate!;
      planSubmitItem.status = 1;
      planSubmitItem.toNext = true;

      planAuditItem.creator = this.data.planCheckUserName;
      planAuditItem.createTime = this.data.planCheckDate;
      planAuditItem.status = 1;
      planAuditItem.additionText = this.data.planStatus === 1 ? '已通过' : '已驳回';
      planAuditItem.additionColor = this.data.planStatus === 1 ? '#4DC672' : '#DF0000';
      planAuditItem.toNext = this.data.planStatus === 1;
    } else {
      // 未审核 （包括 未填写 和 已填写）
      if (!this.data.plan.measure) {
        // 未填写
        if (this.data.status === 1) {
          // 整体流程被拒绝时，不显示
          planSubmitItem.additionText = '未填写';
          planSubmitItem.additionColor = '#5F6165';
          planSubmitItem.toNext = false;
        }
      } else {
        // 已填写
        planSubmitItem.status = 1;
        planSubmitItem.toNext = true;
        planAuditItem.creator = this.data.plan.updateUserName!;
        planAuditItem.createTime = this.data.plan.updatedDate!;

        planAuditItem.additionText = '待审核';
        planAuditItem.additionColor = '#FA642A';
        planAuditItem.status = 1;
        planAuditItem.toNext = false;
        planAuditItem.hideNameAndTime = true;
      }
    }
  }
  fillHandle(flow: FlowItem[]) {
    if (!this.data.resolves || !this.data.resolves.length) {
      flow.push(
        ...[
          {
            creator: '',
            createTime: '',
            status: 0,
            action: 'handle',
            toNext: false,
          },
          {
            creator: '',
            createTime: '',
            status: 0,
            action: 'accept',
            toNext: false,
          },
        ]
      );
      return;
    } else {
      this.data.resolves.forEach((layer, index) => {
        const isEndingHandle = layer.handles[layer.handles.length - 1].status === 1;
        const hasNextHandle = index !== this.data.resolves.length - 1;
        const hasCheck = layer.status !== 0;
        const checkResultText = {
          0: '待验收',
          1: '合格',
          2: '不合格',
        }[layer.status];
        const checkResultColor = {
          0: '#FA642A',
          1: '#4DC672',
          2: '#DF0000',
        }[layer.status];

        flow.push(
          ...[
            {
              creator: '',
              createTime: '',
              status: 1,
              action: 'handle',
              toNext: isEndingHandle,
              additionText: isEndingHandle ? '' : '治理中',
              additionColor: isEndingHandle ? '' : '#7C1CFF',
            },
            {
              creator: hasCheck ? layer.checkUserName : '',
              createTime: hasCheck ? layer.checkDate : '',
              status: isEndingHandle ? 1 : 0,
              action: 'accept',
              toNext: hasNextHandle,
              additionText: isEndingHandle ? checkResultText : '',
              additionColor: isEndingHandle ? checkResultColor : '',
            },
          ]
        );
      });
    }
  }
  getTitle(item: FlowItem) {
    return flowActionMap[item.action]?.title;
  }
  getIcon(item: FlowItem) {
    return flowActionMap[item.action]?.icon;
  }
  getIconColor(item: FlowItem) {
    if (item.status) {
      return flowActionMap[item.action]?.activeColor || '#A5A5A5';
    }
    return '#A5A5A5';
  }
  getLineColor(item: FlowItem, index: number) {
    const nextItem = this.flowList[index + 1];
    if (!item.toNext) {
      return '#F6F6F6';
    }
    if (!nextItem) {
      return '#F6F6F6';
    }
    if (!item.status) {
      return '#F6F6F6';
    }
    const mainColor = flowActionMap[item.action]?.activeColor || '#F6F6F6';
    if (nextItem.status) {
      return mainColor;
    } else {
      return `linear-gradient(to bottom, ${mainColor}ff, ${mainColor}00)`;
    }
  }
}
