import { Router } from '@angular/router';
import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { ContractManagementService } from '../../services/contract-management.service';


@Component({
  selector: 'app-contract-management-template-detail-complaint',
  templateUrl: './contract-template.component.html',
  styleUrls: ['./contract-template.component.less']
})
export class ContractManagementTemplateDetailComponent implements OnInit {
  ui: SFUISchema = {};
  uiView: SFUISchema = {};
  schema: SFSchema = {};
  schemaView: SFSchema = {};
  auditMany = false;
  _$expand = false;
  channelId: any;
  @ViewChild('st') private readonly st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  columns: STColumn[] = [];
  datass: any = [
    {
      one: '1',
      two: '1',
      three: '1',
      id: 1
    },
    {
      one: '2',
      two: '2',
      three: '2',
      id: 2
    },
  ];
  constructor(
    public service: ContractManagementService,
    private modal: NzModalService,
    private router: Router
  ) { }

  /**
 * 查询参数
 */
  get reqParams() {
    return {
      templateType: 'MX',
      ...this.sf?.value,
    };
  }
  get selectedRows() {
    return this.st?.list.filter((item) => item.checked) || [];
  }
  ngOnInit(): void {
    this.initSF();
    this.initST();
    this.initSTAudit();
  }



  /**
 * 初始化查询表单
 */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        templateName: {
          type: 'string',
          title: '模板名称',
        },
        signingObject: {
          type: 'string',
          title: '签约对象',
          enum: [
            { label: '全部', value: '' },
            { label: '货主', value: 1 },
            { label: '司机', value: 2 }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        contractType: {
          title: '合同类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            containsAllLable: true,
            params: { dictKey: 'contract:type' },
            containAllLable: true,
          } as SFSelectWidgetSchema
        },
        resourceType: {
          title: '货源类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'goodresource:type' },
            containsAllLable: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            },
          } as SFSelectWidgetSchema,
        },
      },
    };
    this.ui = { '*': { spanLabelFixed: 110, grid: { span: 8, gutter: 4 } } };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      {
        title: '合同模板名称',
        className: 'text-center',
        render: 'templateName'
      },
      {
        title: '签约对象',
        width: '100px',
        className: 'text-center',
        index: 'signingObjectLabel'
      },
      {
        title: '合同类型',
        width: '100px',
        className: 'text-center',
        index: 'contractTypeLabel'
      },
      {
        title: '货源类型',
        width: '100px',
        className: 'text-center',
        index: 'resourceTypeLabel'
      },
      { title: '创建人', index: 'createUserIdLabel', width: '120px', className: 'text-center' },
      {
        title: '创建时间',
        className: 'text-center',
        index: 'createTime',
        width: '200px'
      },
      {
        title: '操作',
        fixed: 'right',
        width: '110px',
        className: 'text-center',
        buttons: [
          {
            text: '编辑',
            click: (_record) => this.edit(_record),
            acl: { ability: ['CONTRACT-TEMPLATE-edit'] },
          },
          {
            text: '删除',
            click: (_record) => this.delete(_record),
            acl: { ability: ['CONTRACT-TEMPLATE-delete'] },
          },
        ],
      },
    ];
  }
  initSTAudit() {
    this.schemaView = {
      properties: {
        handleResult: {
          title: '处理结果',
          type: 'string',
          maxLength: 50,
          ui: {
            placeholder: '最多不超过50字',
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 }
          },
        },
      },
      required: ['handleResult']
    };
    this.uiView = { '*': { spanLabelFixed: 110, grid: { span: 24 } } };
  }
  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }
 /**
   * 伸缩查询条件
   */
  expandToggle(): void {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }
  tabChange(item: any) {
  }
  /**
   * 重置表单
   */
  resetSF(): void {
    this.sf.reset();
    this._$expand = false;
  }

  edit(value: any) {
    this.router.navigate(['/contract-management/template/text/' + value.id],{
      queryParams: {
        status: 2
      }
    })
  }
  creatTemplate() {
    this.router.navigate(['/contract-management/template/text/' + 0], {
      queryParams: {
        status: 1
      }
    })
  }
  goBack() {
    window.history.go(-1)
  }
  view(value: any) {
    this.router.navigate(['/contract-management/template/text/' + value.id],{
      queryParams: {
        status: 3
      }
    })
  }
  delete(value: any) {
    this.modal.confirm({
      nzTitle: '<i>删除确认</i>',
      nzOnOk: () =>
        this.service.request(this.service.$api_deletebatch_contractTemplate, [value.id]).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('删除成功！');
            this.st.reload(1)
          }
        })
    });
  }
}
