/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-03-21 09:26:45
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-04-22 15:01:43
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\partner\\rebate-management\\components\\rebate-setting\\add\\add.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { ModalHelper } from '@delon/theme';
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STComponent, STData, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { processSingleSort, ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { RebateManagementService } from '../../../services/rebate-management.service';
import { ParterRebateManageMentAddPartnerListComponent } from '../add-partnerlist/add-partnerlist.component';
import { inRange } from '@delon/util';
@Component({
  selector: 'app-parter-channel-rebate-management-add',
  styleUrls: ['./add.component.less'],
  templateUrl: './add.component.html'
})
export class ParterRebateManageMentAddComponent implements OnInit {
  @ViewChild('table') table!: any;
  tabelData: any;
  addStatus: boolean = false;
  configName: string = '';
  partnerType: string = '';
  remarke: string = '';
  accountingRate: Number = 0;
  priority: string = '';
  partnerPeopleList: any = [];
  configType = '1';
  precision = 2;
  partnerId :Array<string> =[];
  inputValue = '';
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  schema1!: SFSchema;
  constructor(
    public router: Router,
    public ar: ActivatedRoute,
    public service: RebateManagementService,
    private modal: NzModalService,
    public shipperservice: ShipperBaseService
  ) {}
  columns: STColumn[] = [
    {
      title: '合伙人名称',
      index: 'enterpriseName',
      width: 180,
      format: item => (item.partnerType ? `${item.enterpriseName || item.contactName}` : '')
    },
    { title: '联系人', index: 'contactName', width: 150, format: item => (item.partnerType ? `${item.contactName}` : '') },
    { title: '手机号', index: 'contactMobile', className: 'text-center', width: 150 },
    { title: '类型', index: 'partnerType', className: 'text-center', width: 130, type: 'enum', enum: { 1: '企业', 2: '个人' } },
    {
      title: '操作',  width: '90px',  fixed: 'right',
      buttons: [
        {
          text: '移除',
          click: _record => this.delete(_record),
          acl: { ability: ['AbnormalAppear-reply'] }
        },
      ]
    },
  ];
  initSF(data?: any) {
    this.schema1 = {
      properties: {
        ruleDescription: {
          type: 'string',
          title: '',
          ui: {
            widget: 'tinymce',
            loadingTip: 'loading...',
            config: {
              height: 500,
            }
          },
          // default: data?.agreementContent || ''
        }
      }
    };
  }
  ngOnInit() {
    this.addStatus =false
    this.initSF();
  }
  goBack() {
    window.history.go(-1);
  }
  /**
   *合伙人选择
   */
   add(item?: any) {
    const modalRef = this.modal.create({
      nzTitle: '合伙人选择',
      nzWidth: 1000,
      nzContent: ParterRebateManageMentAddPartnerListComponent,
      nzComponentParams: {
        i: item,
      },
      nzFooter: null
    });
    modalRef.afterClose.subscribe((res: any) => {
      this.partnerId = [];
      if (res) {
        if(Array.isArray(res)) {
          console.log(res);
          console.log(this.partnerPeopleList);
          this.partnerPeopleList =  this.partnerPeopleList.concat(res);
          res.forEach((ele: any) => {
            this.partnerId.push(ele?.id);
          })
        } else {
          console.log(res);
          this.partnerPeopleList =  this.partnerPeopleList.concat(res);
          this.partnerId.push(res?.id);
      }
      }
    });
  }
  delete(item: any) {
  this.partnerPeopleList = this.partnerPeopleList.filter((d:any, i: any) => {
   return item.id != d.id
  });
  }
  save () {
    const params = {
      accountingRate: this.accountingRate,
      configName: this.configName,
      configType: this.configType,
      rebateConfigLineDTO: this.table.data,
      priority:  this.priority,// 优先级
      partnerId: this.partnerId.join(','),
      ruleDescription: this.sf.value.ruleDescription,
      remarke: this.remarke,
      partnerType: this.partnerType
    }
    console.log(params);
    this.service.request(this.service.$api_save_rebateConfig, params).subscribe((res: any) => {
      if(res) {
        console.log(res);
        this.service.msgSrv.success('新增成功！')
        this.router.navigate(['/partner/rebate/setting'])
      }
    })
  }
  changePartner(value: any) {
    console.log(value);
    if(value) {
      this.addStatus = true
    }
  }
}
