import { Params } from '@angular/router';
import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { SupplyManagementService } from '../../services/supply-management.service';

@Component({
  selector: 'app-supply-management-update-price',
  templateUrl: './update-price.component.html',
})
export class SupplyManagementUpdatePriceComponent implements OnInit {
  record: any = {};
  i: any;
  ii: any;
  schema: SFSchema = {};
  ui: SFUISchema = {};
  columns: STColumn[] = [];
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  freightType: any = {
    1: '元/吨',
    2: '元/方',
    3: '元/车'
  } // 运单类型
  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public service: SupplyManagementService
  ) { }


  ngOnInit(): void {
    this.initSF();
    this.initSt();
    console.log(this.i)
    const Params = {
      id: this.i.id
    }
   this.service.request(this.service.$api_get_listModifyPrice, Params).subscribe((res) =>{
    console.log(res)
    this.ii = res[0];
   })
  }

  initSF() {
    this.schema = {
      properties: {
        resourceCode: {
          type: 'string',
          title: '货源编号',
          ui: {
            widget: 'text'
          },
        },
        freightPrice: {
          type: 'string',
          title: '运费单价',
          ui: {
            widget: 'custom'
          }
        },
        rule: {
          type: 'string',
          title: '取整规则',
          enum: [
            { label: '保留小数', value: '1' },
            { label: '抹除小数', value: '2' },
            { label: '抹除个位', value: '3' },
          ],
          ui: {
            widget: 'select',
            optionalHelp: {
              text: '例如:付司机运费= 重量*单价 = 999.99;\n 保留小数：即 999.99; \n 抹除小数：即 999.00;\n  抹除个位，即 990.00',
            }
          }
        },

      },
      required: ['freightPrice', 'rule',],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 100,
        grid: { span: 16 },
      },
    };
  }

  /**
   * 初始化数据列表
   */
  initSt() {
    this.columns = [
      { title: '日志内容', width: 120, index: 'owner', className: 'text-center' },
      { title: '更新时间', index: 'goodsQuantity', width: 100, className: 'text-center' },
      { title: '操作人', width: 100, render: 'operator', className: 'text-center' },
    ];
  }
  save(value: any): void {
    console.log(value)
    const { id, freightType, freightPrice, resourceCode, rule, resourceId } = value;
    this.service.request(this.service.$api_update_price, { id, freightType, freightPrice, resourceCode, rule, resourceId }).subscribe(res => {
      if (res) {
        this.msgSrv.success('保存成功');
        this.modal.close(true);
      }
    });
  }

  close(): void {
    this.modal.destroy();
  }
}
