import {Component, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {CustomValidators} from 'ng2-validation';
import {FrameAbstractComponent} from '../../../frame/FrameAbstractComponent';
import {FrameRequest} from '../../../../shared/frame/frame-request';
import {FrameUploaderComponent} from '../../../../core/uploader/uploader.component';
import {AppComponent} from '../../../../app.component';

@Component({
  selector: 'app-cf-agent',
  templateUrl: './cf-agent.component.html',
  styleUrls: ['./cf-agent.component.scss']
})
export class CfAgentComponent extends FrameAbstractComponent {

  initAgent = { agent_status: 'Active'}
  weixinArray = [];

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;


  constructor(fb: FormBuilder) {
    super();
    this.valForm = fb.group({
      'agent_name': [null, Validators.compose([Validators.required, CustomValidators.rangeLength([1, 64])])],
      // 'short_name': [null, Validators.compose([Validators.required, CustomValidators.rangeLength([1, 32])])],
      'agent_status': [null, Validators.required]
    });
  }


  optrAgent(step: string, entity: string , url?: string) {
    this.goViewStep(step, entity, url);
    FrameRequest.requestArray('weixin/define/all.htm', {}, (array) => {
      this.weixinArray = array;
    });
  }

  uploadAgentImage(agent: any) {
    if (agent.agent_image) {
    AppComponent.confirm('考点图片已存在，此操作将会替换现有图片信息' , () => {
      this.uploader.choooseFileAndUpload({agent_id: agent.agent_id}, (() => {
        this.page.reload();
      }).bind(this));
    });
    } else {
      this.uploader.choooseFileAndUpload({agent_id: agent.agent_id}, (() => {
        this.page.reload();
      }).bind(this));
    }

  }
}
