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 { ParterRebateManageMenRecordDetailComponent } from '../../model/record-detail/record-detail.component';
import { ParterRebateManageMenAbnormalFeedbackComponent } from '../../model/abnormal-feedback/abnormal-feedback.component';

@Component({
  selector: 'app-parter-channel-rebate-management-setting',
  templateUrl: './rebate-setting.component.html'
})
export class ParterRebateManageMentSettingComponent implements OnInit {
  schema: SFSchema = {};
  columns!: STColumn[];
  ui!: SFUISchema;
  @ViewChild('st', { static: false })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  spuStatus = '1';
  _$expand = false;
  constructor(
    public router: Router,
    public ar: ActivatedRoute,
    public service: RebateManagementService,
    private modal: NzModalService,
    public shipperservice: ShipperBaseService,
  ) {}
  /**
 * 查询参数
 */
   get reqParams() {
    const params: any = Object.assign({}, this.sf?.value || {});
    return { 
      ...params,
     };
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        configName: {
          type: 'string',
          title: '配置名称',
        },
        stateLocked: {
          type: 'string',
          title: '状态',
          enum: [
            {label: '全部', value: ''},
            {label: '启用', value: 0},
            {label: '禁用', value: 1},
          ],
          ui: {
            widget: 'select',
          },
        
        },
      }
    };
    this.ui = {
      '*': {
        spanLabelFixed: 140,
        grid: { span: 8, gutter: 4 }
      }
    };
  }

  initST() {
    this.columns = [
      {
        title: '配置名称',
        index: 'configName',
        width: '200px',
      },
      {
        title: '配置类型',
        render: 'configType',
        width: '200px',

      },
      {
        title: '备注',
        index: 'remark',
        width: '250px',
      },
      {
        title: '关联合伙人范围',
        render: 'partnerType',
        width: '200px',
      },
      {
        title: '创建时间',
        index: 'enableTime',
        width: '200px',
      },
      {
        title: '启用时间',
        index: 'enableTime',
        width: '200px',
      },
      {
        title: '优先级',
        index: 'priority',
        width: '100px',
      },
      {
        title: '状态',
        render: 'stateLocked',
        width: '100px',
      },
      {
        title: '操作',
        fixed: 'right',
        width: '90px',
        className: 'text-left',
        buttons: [
          {
            text: '查看',
            click: _record => this.viewEvaluate(_record),
          },
          {
            text: '禁用',
            iif: (_record) =>{ return _record.stateLocked == true},
            click: _record => this.viewEvaluate(_record),
          },
          {
            text: '启用',
            iif: (_record) =>{ return _record.stateLocked == false},
            click: _record => this.viewEvaluate(_record),
          },
        ]
      }
    ];
  }
  /**
   *禁用
   */
   viewEvaluate(item: any) {
     console.log(item.stateLocked);
     let title = ''
     let stateLocked: boolean;
     if(item.stateLocked) {
       title = '是否禁用该配置？'
       stateLocked = false
     } else {
       title = '是否启用该配置？'
       stateLocked = true
     }
    this.modal.confirm({
      nzTitle: title,
      nzOnOk: () => {
        const params = {
          id: item?.id,
          stateLocked: stateLocked,
        }
          this.service.request(this.service.$api_set_updateRebateConfig,params).subscribe((res: any) => { 
            if(res) {
              this.service.msgSrv.success('设置成功！')
              this.st.reload();
            }
          })
      }
    });
  }
  /**
   *查看
   */
   feedback(item?: any) {
       const modal = this.modal.create({
          nzTitle: '查看',
          nzWidth: 580,
          nzContent: ParterRebateManageMenAbnormalFeedbackComponent,
          nzComponentParams: { i: item },
          nzFooter: null
        });
        modal.afterClose.subscribe((res: any) => {
          if (res) {
          }
        });
  }
  configAction() {
    this.router.navigate(['/partner/rebate/setting/add/', 1])
  }
  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this.st.load(1);
  }
}
