/*
 * @Description:
 * @Author: wsm
 * @Date: 2021-07-15 09:42:57
 * @LastEditTime: 2021-12-01 15:06:11
 * @LastEditors: Please set LastEditors
 * @Reference:
 */
import { AfterViewInit, Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { filter } from 'rxjs/operators';


@Component({
  selector: 'app-supply-goods-components-list-large-amount',
  templateUrl: './large-amount.component.html',
  styleUrls: ['./large-amount.component.less'],
})
export class SupplyGoodsComponentListLargeAmountComponent implements OnInit, AfterViewInit {
  @ViewChild('st', { static: true }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  validateForm!: FormGroup;
  @ViewChild('exportTemplate')
  exportTemplate!: TemplateRef<any>;
  ui: SFUISchema = {};
  mode: any = 'search';
  tabs = ['全部', '进行中',  '已完结',  '已取消'];
  listData: any =[
    {
      id: 1,
      id2: 2,
      id3:3,
      id4: 4,
    }
  ]
  orderStatus: any = '0'; // 订单状态, 10为已提交待付款, 20为已付款待发,30为已发货待收货,50取消订单,40 已完成
  nzSelectedIndex = 0;
  total = 1;
  loading = false;
  pageSize = 20;
  pageIndex = 1;
  toSendCount = 0;
  addTimeStart = '';
  addTimeEnd = '';
  sfExpand = false;
  _$expand = false;

  schema!: SFSchema;
  payStatus: any;
  constructor(
    private fb: FormBuilder,
    private modal: NzModalService,
    private router: Router,
    private ar: ActivatedRoute,
    private modalHelper: ModalHelper,
    public shipperSrv: ShipperBaseService
    
  ) {}

  ngOnInit(): void {
    this.getList();
    this.initSF();
    this.initUI();
  }

  ngAfterViewInit() {
    // this.router.events.pipe(filter((evt) => evt instanceof NavigationEnd)).subscribe(() => {
    //
    // });
  }

  initSF() {
    this.schema = {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true,
          },
        },
        storeName: { title: '货源编号', type: 'string', ui: { showRequired: false } },
        contactsName: {
          title: '货物名称',
          type: 'string',
          ui: {
            showRequired: false,
          },
        },
        unifiedSocialCreditCode: {
          title: '装货地',
          type: 'string',
          ui: {
            showRequired: false,
          },
        },
        unifiedSocialCreditCode2: {
          title: '卸货地',
          type: 'string',
          ui: {
            showRequired: false,
            visibleIf: {
              expand: (value: boolean) => value,
            },
          },
        },
        createUserId: {
          type: 'string',
          title: '录单员',
          default: '',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              _$expand: (value: boolean) => value,
            },
            asyncData: () => this.shipperSrv.getStaffList(),
            allowClear: true,
          },
        },
      },
    };
    this.ui = { '*': { spanLabelFixed: 90, grid: { span: 8, gutter: 4 }, enter: () => this.st.load() } };
  }
  initUI() {
    this.ui = {
      '*': {
        spanLabelFixed: 90,
        grid: { span: 8 },
      },
    };
  }
  tabChange(i: number) {
    console.log(i)
    this.changeIndex(i);
    this.getList();
  }
  changeIndex(i: number) {
    if (i === 0) {
      this.orderStatus = '0';
      this.payStatus = null;
    } else if (i === 1) {
      this.orderStatus = '5';
      this.payStatus = null;
    } else if (i === 2) {
      this.orderStatus = null;
      this.payStatus = '10';
    } else if (i === 3) {
      this.orderStatus = '20';
      this.payStatus = null;
    }
    this.pageIndex = 1;
  }
  getList(type?: string) {
    // this.loading = true;
    const params: any = {
      pageSize: this.pageSize,
      pageIndex: this.pageIndex,
      ...this.sf?.value,
      orderStatus: this.orderStatus,
      payStatus: this.payStatus,
      createTime: this.sf?.value.createTime || [],
      time: {},
    };
    if (this.sf?.value?.createTime) {
      delete params.createTime;
      params.time.start = this.sf?.value?.createTime[0];
      params.time.end = this.sf?.value?.createTime[1];
    }
    delete params.expand;
    if (type === 'search') {
      params.pageIndex = 1;
    }
    // this.service.request(this.service.$api_get_page, params).subscribe((res) => {
    //   this.loading = false;
    //   this.listData = res.orderPage.records;
    //   this.toSendCount = res.toSendCount;
    //   this.listData.forEach((element: { goodsList: { goodsSpecJson: string }[] }) => {
    //     element.goodsList.forEach((item: { goodsSpecJson: string }) => {
    //       if (item.goodsSpecJson) {
    //         item.goodsSpecJson = JSON.parse(item.goodsSpecJson);
    //       }
    //     });
    //   });
    //   this.total = res.orderPage.total;
    // });
  }

  orderExport() {
    // this.service.exportStart(params, this.service.$api_export);
    this.modal.create({
      nzTitle: '订单导出',
      nzContent: this.exportTemplate,
      // nzContent: `待导出订单总数：${this.pageInfo.total}条，确定要导出吗？`,
      nzOnOk: () => {
        const params: any = {
          pageSize: this.pageSize,
          pageIndex: this.pageIndex,
          ...this.sf?.value,
          orderStatus: this.orderStatus,
          // createTime: this.sf?.value.createTime || [],
        };
        delete params.expand;
      },
    });
  }

  goDetail(data: any) {
    this.router.navigate(['../order-detail', data.orderSn], { relativeTo: this.ar });
  }


  changePageIndex(pageIndex: number) {
    this.pageIndex = pageIndex;
    this.getList();
  }
  changePageSize(value: number) {
    this.pageSize = value;
    this.getList();
  }
  expandToggle() {
    this.sfExpand = !this.sfExpand;
    this.sf?.setValue('/expand', this.sfExpand);
  }

  showApply() {

  }
 

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



   /**
   * 查询字段个数navigate
   */
    get queryFieldCount(): number {
      return Object.keys(this.schema?.properties || {}).length;
    }
}
