import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {environment} from '../../../../environments/environment';
import {AgentService} from '../../../services/agent.service';
import {ValidatorsService} from '../../../services/validators.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-user-edit',
  templateUrl: './user-edit.component.html',
  styleUrls: ['./user-edit.component.scss']
})
export class UserEditComponent implements OnInit {

  validateForm!: FormGroup;
  data: any;
  editFlag = false;
  id: number;
  passwordVisible = false;
  logoFile = [];
  WEB_SERVE_URL = environment.baseUrl;
  userId: number;
  time;
  constructor(
      private fb: FormBuilder,
      private agent: AgentService,
      private message: NzMessageService, // 信息提示
      private activatedRoute: ActivatedRoute,
  ) {
  }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams.agentId != null) {
        this.editFlag = true;
        this.id = queryParams.agentId;
        this.getDetails(queryParams.agentId);
      }
    });
    this.validateForm = this.fb.group({
      loginName: [null, [Validators.required, ValidatorsService.maxLength(20)]],
      password: [null, [Validators.required]],
      agentName: [null, [Validators.required]],
      type: [2],
      agentUser: [null, [Validators.required, ValidatorsService.maxLength(50)]],
      agentPhone: [null, [Validators.required, ValidatorsService.maxLength(50)]],
      agentAddress: [null, [Validators.required, ValidatorsService.maxLength(80)]],
      secUser: {},
    });
  }

  // 返回
  getBack() {
    history.back();
  }

  // 重置
  public resetForm(): void {
    this.validateForm.reset();
  }

  // 课程保存
  public getSave(): void {
    // tslint:disable-next-line:forin
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
      if (this.validateForm.controls[i].errors != null) {
        this.message.error('必填项不能为空');
        return;
      }
    }
    this.validateForm.value['secUser']['loginName'] = this.validateForm.value.loginName;
    this.validateForm.value['secUser']['password'] = this.validateForm.value.password;
    if (this.logoFile.length !== 0) {
      if (this.logoFile[0]['response'] != null) {
        this.validateForm.value.logo = this.logoFile[0]['response']['return_data'][0];
      } else {
        this.validateForm.value.logo = this.logoFile[0].name;
      }
    }

    if (this.editFlag) {
      this.validateForm.value.id = this.id;
      this.agent.updateAgent(this.validateForm.value, data => {
        if (data['return_code'] === '000000') {
          this.getBack();
          this.message.success('修改成功');
        } else {
          this.message.create('error', '修改失败');
        }
      });
    } else {

      this.agent.insertAgent(this.validateForm.value, data => {
        if (data['return_code'] === '000000') {
          this.getBack();
          this.message.success('添加成功');
        } else {
          this.message.create('error', data['return_msg']);
        }
      });
    }
  }



  public getDetails(id) {
    this.agent.findByAgentId(id, data => {
      console.log(data);
      if (data['return_code'] === '000000') {
        data['return_data'].loginName = data['return_data'].secUser.loginName;
        data['return_data'].password = data['return_data'].secUser.password;
        this.validateForm.patchValue(data['return_data']);
      } else {
        this.message.create('error', data['return_msg']);
      }
    });
  }

}
