import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STChange, STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSchemaEnum, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TaxManagementService } from '../../services/tax-management.service';

@Component({
  selector: 'app-tax-management-individual-collect',
  templateUrl: './individual-collect.component.html',
  styleUrls: ['../../../commom/less/box.less']
})
export class TaxManagementIndividualCollectComponent implements OnInit {
  _$expand = false;
  schema!: SFSchema;
  columns!: STColumn[];
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  tabs: any[] = [
    { name: '待申报', value: '0' },
    { name: '待审核', value: '1' },
    { name: '已通过', value: '2' },
    { name: '不通过', value: '3' },
    { name: '全部', value: '' }
  ];
  selectedIndex = '0';

  selectedRows: any[] = [];
  constructor(public service: TaxManagementService, private router: Router, private ar: ActivatedRoute, private modal: NzModalService) {}

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {}, {
      declareStatus: this.selectedIndex
    });
    delete params._$expand;
    return { ...params };
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }
  /**
   * 程序初始化入口
   */
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        overdueStatus: {
          title: '是否逾期',
          type: 'string',
          enum: [
            { value: '', label: '全部' },
            { value: false, label: '否' },
            { value: true, label: '是' }
          ],
          ui: {
            placeholder: '请选择',
            widget: 'select',
            containsAllLabel: true
          },
          default: ''
        },
        taxMonth: {
          title: '税款所属期',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd',
          } as SFDateWidgetSchema
        },
        sbrq: {
          title: '申报日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd',
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        nsrmc: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            asyncData: () => this.service.getNetworkEnterpriseName()
          }
        }
      }
    };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      { title: '', type: 'checkbox', className: 'text-center', width: '60px' },
      {
        title: '申报状态',
        index: 'declareStatus',
        className: 'text-center',
        width: '120px',
        type: 'badge',
        badge: {
          '0': { text: '待申报', color: 'default' },
          '1': { text: '待审核', color: 'processing' },
          '2': { text: '已通过', color: 'success' },
          '3': { text: '不通过', color: 'error' }
        }
      },
      {
        title: '是否逾期',
        index: 'overdueStatus',
        className: 'text-center',
        width: '120px',
        type: 'enum',
        enum: { false: '否', true: '是' }
      },
      {
        title: '税款所属期起',
        index: 'skssqq',
        className: 'text-center',
        width: '150px'
      },
      { title: '税款所属期止', index: 'skssqz', className: 'text-center', width: '150px' },
      {
        title: '纳税人名称',
        index: 'nsrmc',
        className: 'text-center',
        width: '180px'
      },
      { title: '纳税人识别号', index: 'nsrsbh', className: 'text-center', width: '200px' },
      {
        title: '税率',
        index: 'sl',
        className: 'text-right',
        width: '150px',
        format: item => `${item.sl ? ((item.sl as number) * 100).toFixed(2) : 0}%`
      },
      { title: '申报人数', index: 'sbrs', className: 'text-right', width: '150px' },
      {
        title: '应税收入',
        index: 'yssr',
        className: 'text-right',
        width: '150px',
        type: 'widget',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yssr }) }
      },
      {
        title: '应纳税额',
        index: 'ynse',
        className: 'text-right',
        width: '180px',
        type: 'widget',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.ynse }) }
      },
      {
        title: '累计已缴纳税额',
        index: 'ljyjnse',
        className: 'text-center',
        width: '150px',
        type: 'widget',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.ljyjnse }) }
      },
      {
        title: '本期应补退税额',
        index: 'bqybtse',
        className: 'text-center',
        width: '150px',
        type: 'widget',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.bqybtse }) }
      },
      { title: '申报日期', index: 'sbrq', className: 'text-center', width: '150px' }
    ];
  }

  /**
   *更正
   * @param record 记录实例
   */
  recall() {
    if (this.selectedRows.length === 0) {
      this.service.msgSrv.warning('请选择需要更正的数据');
      return;
    }
    // this.modal.confirm({
    //   nzTitle: '撤回提示',
    //   nzContent: ' 撤回后可以重新上传，重新上传会覆盖已上传数据，确定要撤回？',
    //   nzOkText: '确定',
    //   nzCancelText: '取消',
    //   nzOnOk: () => {
    //     this.service.request(this.service.$api_recall_reporting, { rows: this.selectedRows }).subscribe((res: any) => {
    //       if (res) {
    //         this.service.msgSrv.success('撤销成功');
    //         this.search();
    //       }
    //     });
    //   }
    // });
  }

  selectChange(item: any) {
    this.selectedIndex = item?.value || '';
    setTimeout(() => {
      this.st.load(1);
    });
  }

  /**
   * 申报
   */
  upload() {
    if (this.selectedRows.length === 0) {
      this.service.msgSrv.warning('请选择需要申报的数据');
      return;
    }
    // this.service.request(this.service.$api_recall_reporting, { rows: this.selectedRows }).subscribe((res: any) => {
    //   if (res) {
    //     this.service.msgSrv.success('申报成功');
    //     this.search();
    //   }
    // })
  }

  /**
   *
   * @param params 更新数据
   */
  uploadSetting() {
    this.service.request(this.service.$api_get_updateData).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('更新成功');
        this.search();
      }
    })
  }

  search() {
    this.st.load(1);
  }

  /**
   * 异步导出
   */
  export() {
    this.service.exportStart(this.sf?.value, this.service.$api_async_export_order_reporting_list);
  }
}
