/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-18 15:57:44
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-04-09 16:36:42
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\financial-management\\components\\voucher-management\\voucher-management.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-voucher-management',
  templateUrl: './voucher-management.component.html',
  styleUrls: ['../../../commom/less/box.less']
})
export class VoucherManagementComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  selectedRows: any[] = [];
  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value
      });
      if (this.sf.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf.value.createTime?.[0] || null,
            end: this.sf.value.createTime?.[1] || null
          }
        });
      }
      if (this.sf.value.vctime) {
        Object.assign(requestOptions.body, {
          vctime: {
            start: this.sf.value.vctime?.[0] || null,
            end: this.sf.value.vctime?.[1] || null
          }
        });
      }
    }
    return requestOptions;
  };

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

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

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

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        vccode: {
          type: 'string',
          title: '凭证号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount()
          },
          default: ''
        },
        vctime: {
          title: '凭证时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        sourceCode: {
          type: 'string',
          title: '原始单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        sourceType: {
          type: 'string',
          title: '原始单类型',
          enum: [
            { value: '', label: '全部' },
            { value: 1, label: '收款' },
            { value: 2, label: '收款(退款)' },
            { value: 3, label: '应收费用' },
            { value: 4, label: '应收费用(负数)' },
            { value: 5, label: '应收核销' },
            { value: 6, label: '应收核销(负数)' },
            { value: 7, label: '应付费用' },
            { value: 8, label: '应付费用(负数)' },
            { value: 9, label: '应付核销' },
            { value: 10, label: '应付核销(负数)' },
            { value: 11, label: '分票开票' },
            { value: 12, label: '删除分票' },
            { value: 13, label: '付款' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        remarks: {
          type: 'string',
          title: '凭证摘要',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vctype: {
          type: 'string',
          title: '凭证类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        // subid: {
        //   type: 'string',
        //   title: '科目',
        //   ui: {
        //     widget: 'dict-select',
        //     params: { dictKey: 'refund:apply:status' },
        //     placeholder: '请选择',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        drmoney: {
          type: 'string',
          title: '借方金额',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        crmoney: {
          type: 'string',
          title: '贷方金额',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vcltdid: {
          type: 'string',
          title: '帐套',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        sts: {
          type: 'string',
          title: '凭证状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        importncnotes: {
          type: 'string',
          title: 'NC凭证',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vc2code: {
          type: 'string',
          title: '汇总凭证号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        importnc: {
          type: 'string',
          title: '导入NC',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        isvc2: {
          type: 'string',
          title: '是否汇总',
          enum: [
            { value: null, label: '全部' },
            { value: 0, label: '否' },
            { value: 1, label: '是' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, className: 'text-center', fixed: 'left' },
      { title: '凭证号', index: 'vccode', type: 'link', width: 200 },
      { title: '帐套', index: 'ltdId', width: 200, format: item => `${item.vcltdcode}-${item.vcltdname}` },
      { title: '凭证时间', index: 'vctime', type: 'date', width: 200 },
      { title: '凭证类型', index: 'vctype', width: 200 },
      // { title: '序号', index: 'invmoney', width: 200, format: _ => '1' },
      { title: '摘要', index: 'remarks', width: 300 },
      { title: '币种', index: 'currency', width: 100 },
      {
        title: '借方金额',
        index: 'drmoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.drmoney }) }
      },
      {
        title: '贷方金额',
        index: 'crmoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.crmoney }) }
      },
      { title: 'NC凭证', index: 'importncnotes', width: 150 },
      { title: '汇总凭证号', index: 'vc2code', width: 150 },
      { title: '凭证状态', index: 'stsLabel', width: 120 },
      { title: '创建时间', index: 'createTime', width: 140 },
      // { title: '创建人', index: 'createUserId', width: 150 },
      {
        title: '操作',
        width: '130px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            click: (item: any) => this.router.navigate(['/financial-management/voucher-management/detail/' + item.id]),
            acl: { ability: ['FINANCIAL-VOUCHER-view'] }
          }
          // {
          //   text: '修改'
          // },
          // {
          //   text: '删除'
          // },
          // {
          //   text: '提交'
          // }
        ]
      }
    ];
  }
}
