/*
 * @Author: your name
 * @Date: 2021-12-14 14:03:07
 * @LastEditTime: 2021-12-24 17:58:45
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \tms-obc-web\src\app\routes\order-management\modal\bulk\update-freight\update-freight.component.ts
 */
import { Component, Input, OnInit, ViewChild } from '@angular/core';
import {
  SFComponent, SFNumberWidgetSchema, SFSchema,
  SFSelectWidgetSchema,
  SFStringWidgetSchema, SFTextWidgetSchema
} from '@delon/form';
import { Subscription } from 'rxjs';
import { OrderManagementService } from '../../../services/order-management.service';

@Component({
  selector: 'app-order-management-update-freight',
  templateUrl: './update-freight.component.html',
  styleUrls: ['./update-freight.component.less']
})
export class UpdateFreightComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  schema!: SFSchema;

  @Input()
  data: any;

  calculateSub!: Subscription;
  constructor(private service: OrderManagementService) {}

  ngOnInit(): void {
    this.schema = this.initSF(this.data);
  }

  /**
   * 初始化表单
   */
  private initSF(data: any): SFSchema {
    return {
      properties: {
        dto: {
          type: 'object',
          properties: {
            freightPrice: {
              type: 'string',
              title: '运费单价',
              ui: {
                addOnAfter: data.freightTypeLabel,
                placeholder: '请输入',
                grid: {
                  span: 12
                },
                change: (val: any) => this.changeNumVal()
              } as SFStringWidgetSchema,
              default: data.freightPrice
            },
            rule: {
              title: '',
              type: 'string',
              default: data.rule,
              enum: [
                { label: '保留小数', value: '1' },
                { label: '抹除小数', value: '2' },
                { label: '抹除个数', value: '3' }
              ],
              ui: {
                widget: 'select',
                spanLabelFixed: 10,
                grid: {
                  span: 10
                }
              } as SFSelectWidgetSchema
            },
            settlementBasis: {
              title: '结算依据',
              type: 'string',
              default: data.settlementBasis,
              enum: [
                { label: '以收货为准', value: '1' },
                { label: '以发货为准', value: '2' }
              ],
              ui: {
                widget: 'select',
                grid: {
                  span: 12
                }
              } as SFSelectWidgetSchema
            },
            blank1: {
              title: '',
              type: 'string',
              ui: { widget: 'text', grid: { span: 12 }, class: 'input-back' },
              default: ' '
            },
            freightType: {
              title: '',
              type: 'string',
              ui: { hidden: true },
              default: data.freightType
            },
            acceptWeight: {
              type: 'number',
              title: '装货重量',
              default: data.acceptWeight,
              ui: {
                unit: '吨',
                placeholder: '请输入',
                grid: {
                  span: 12
                },
                hideStep: true,
                change: (val: any) => this.changeNumVal()
              } as SFNumberWidgetSchema
            },
            acceptVolume: {
              type: 'number',
              title: '装货体积',
              default: data.acceptVolume,
              ui: {
                unit: '方',
                placeholder: '请输入',
                grid: {
                  span: 12
                },
                hideStep: true,
                change: (val: any) => this.changeNumVal()
              } as SFNumberWidgetSchema
            },
            settlementWeight: {
              type: 'number',
              title: '卸货重量',
              default: data.settlementWeight,
              ui: {
                unit: '吨',
                placeholder: '请输入',
                grid: {
                  span: 12
                },
                hideStep: true,
                change: (val: any) => this.changeNumVal()
              } as SFNumberWidgetSchema
            },
            settlementVolume: {
              type: 'number',
              title: '卸货体积',
              default: data.settlementVolume,
              ui: {
                unit: '方',
                placeholder: '请输入',
                grid: {
                  span: 12
                },
                hideStep: true,
                change: (val: any) => this.changeNumVal()
              } as SFNumberWidgetSchema
            }
          },
          required: ['freightPrice', 'settlementBasis']
        },
        changeCause: {
          title: '审核备注',
          type: 'string',
          maxLength: 100,
          default: data.changeCause,
          ui: {
            widget: 'textarea',
            span: 24,
            placeholder: '选填，最多不超过100字',
            autosize: {
              minRows: 3,
              maxRows: 5
            }
          } as SFTextWidgetSchema
        }
      }
    };
  }

  changeNumVal() {
    if (this.calculateSub) {
      this.calculateSub.unsubscribe();
    }
    this.calculateSub = this.service
      .request(this.service.$api_calculate_cost, { billId: this.data.billId, ...this.sf.value, changeCause: '' })
      .subscribe((res: any) => {
        if (res) {
          Object.assign(this.data, {
            totalFreight: res.totalFreight,
            freight: res.freight,
            surcharge: res.surcharge
          });
        }
      });
  }
}
