
import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STColumn, STComponent, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemService } from '../../services/system.service';

@Component({
  selector: 'app-sms-template',
  templateUrl: './sms-template.component.html',
  styleUrls: ['./sms-template.component.less']
})
export class SmsTemplateComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('sfEdit', { static: false })
  sfEdit!: SFComponent;
  editSchema!: SFSchema;
  visible = false;
  isVisible = false;
  tempData = {};
  isEdit = false;

  searchSchema: SFSchema = {
    properties: {
      templateName: {
        type: 'string',
        title: '模板名称',
        ui: { placeholder: '请输入模板名称' }
      },
      templateCode: {
        type: 'string',
        title: '模板编码',
        ui: { placeholder: '请输入模板编码' }
      },
      templateContent: {
        type: 'string',
        title: '模板内容',
        ui: { placeholder: '请输入模板内容' }
      }
    }
  };

  initSF() {
    this.editSchema = {
      properties: {
        templateName: {
          type: 'string',
          title: '模板名称',
          ui: { placeholder: '请输入模板名称', errors: { require: '必填项' } }
        },
        templateCode: {
          type: 'string',
          title: '模板编码',
          readOnly: this.isEdit,
          ui: { placeholder: '请输入模板编码', errors: { require: '必填项' } }
        },
        templateContent: {
          type: 'string',
          title: '模板内容',
          ui: { placeholder: '请输入模板内容', errors: { require: '必填项' } }
        }
      },
      required: ['templateName', 'templateCode', 'templateContent']
    };
  }

  columns: STColumn[] = [
    { title: '模板名称', className: 'text-center', width: '200px', index: 'templateName' },
    { title: '模板编码', className: 'text-center', width: '140px', index: 'templateCode' },
    { title: '模板内容', className: 'text-center', index: 'templateContent' },
    // {
    //   title: '创建人', className: 'text-center', index: 'content',
    // },
    {
      title: '更新时间',
      index: 'modifyTime',
      type: 'date',
      width: '160px',
      className: 'text-center'
    },
    {
      title: '操作',
      width: '90px',
      buttons: [
        {
          text: '编辑',
          acl: { ability: ['SMS_TEMPLATE-save'] },
          click: i => this.edit(i)
        }
      ]
    }
  ];

  constructor(public service: SystemService, private nzModalService: NzModalService, private route: ActivatedRoute) {}

  ngOnInit(): void {
    this.initSF();
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
    }
    return requestOptions;
  };

  edit(item: any) {
    // console.log(item);
    this.isEdit = true;
    this.editSchema!.properties!.templateCode.readOnly = true;
    this.sfEdit?.refreshSchema();
    this.tempData = item;
    this.isVisible = true;
  }

  search() {
    this.st.reload(1);
    this.visible = false;
  }

  open(): void {
    this.visible = true;
  }

  close(): void {
    this.visible = false;
  }

  add() {
    this.isEdit = false;
    this.editSchema!.properties!.templateCode.readOnly = false;
    this.sfEdit?.refreshSchema();
    this.tempData = {};
    this.isVisible = true;
  }

  handleOk(): void {
    const value = this.sfEdit.value;
    const { id, templateCode, templateName, templateContent } = value;
    const params = {
      id,
      templateCode,
      templateName,
      templateContent
    };

    this.service.request(this.service.$api_smsTemplate_edit, params).subscribe(res => {
      // console.log(res);
      if (res) {
        this.isVisible = false;
        this.st.reload();
      }
    });
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
}
