import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFRadioWidgetSchema, SFSchema, SFStringWidgetSchema, SFUISchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { SystemService } from '../../../services/system.service';

@Component({
  selector: 'app-cart-config-action-modal',
  templateUrl: './cart-config-action-modal.component.html',
  styleUrls: ['./cart-config-action-modal.component.less']
})
export class CartConfigActionModalComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  i: any;
  schema!: SFSchema;
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 120,
      grid: { span: 24 }
    }
  };
  @Input()
  configType: number = 1;
  dictKey = '';
  constructor(private modal: NzModalRef, public service: SystemService) {}

  ngOnInit(): void {
    this.initSF(this.i);
  }
  initSF(staff: any) {
    let option: any = {};
    switch (this.configType) {
      case 1:
        option = {
          title: '车型',
          ui: {
            placeholder: '请输入'
          }
        };
        this.dictKey = 'car:model';
        break;
      case 2:
        option = {
          title: '车长',
          ui: {
            placeholder: '请输入',
            addOnAfter: '米'
          } as SFStringWidgetSchema
        };
        this.dictKey = 'car:length';
        break;
      case 3:
        option = {
          title: '物品名称',
          ui: {
            placeholder: '请输入'
          }
        };
        this.dictKey = 'ban.goods.name';
        break;

      default:
        break;
    }
    this.schema = {
      properties: {
        itemValue: {
          ...option,
          type: 'string',
          default: staff.itemValue
        },
        statePaused: {
          type: 'string',
          title: '状态',
          enum: [
            { label: '启用', value: false },
            { label: '停用', value: true }
          ],
          ui: {
            widget: 'radio',
            visibleIf: {
              itemValue: (value: number) => this.configType !== 3
            }
          } as SFRadioWidgetSchema,
          default: staff?.statePaused || false
        }
      },
      required: ['itemValue']
    };
  }

  sure() {
    if (this.i.id === 0) {
      const params: any = {
        ...this.sf.value,
        dictKey: this.dictKey,
        itemData: this.sf.value.itemValue
      };
      this.service.request(this.service.$api_add_dict, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('保存成功！');
          this.modal.close(true);
        }
      });
    } else {
      const params: any = {
        ...this.i,
        ...this.sf.value
      };
      this.service.request(this.service.$api_update_dict, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('保存成功！');
          this.modal.close(true);
        }
      });
    }
  }

  configAction() {
    if (this.i.id === 0) {
      const params: any = {
        configFullKey: 'ban.goods.name',
        name: this.sf.value.itemValue
      };
      this.service.request(this.service.$api_add_config_item, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('保存成功！');
          this.modal.close(true);
        }
      });
    } else {
      const params: any = { ...this.sf.value, ...this.i, configFullKey: 'ban.goods.name', name: this.sf.value.itemValue };
      this.service.request(this.service.$api_update_config_item, params).subscribe(res => {
        if (res) {
          this.service.msgSrv.success('保存成功！');
          this.modal.close(true);
        }
      });
    }
  }

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