import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import Parse from 'parse';
import { ParseSchema } from './func-parse';

export const Profile: ParseSchema = {
  title: '报送人',
  className: 'Profile',
  emptyImg: '/img/webhook-empty.png',
  include: ['user'],
  buttons: [
    {
      name: '编辑',
      place: 'item',
      show: (options: { object: Parse.Object }) => {
        if (location?.pathname == '/nav-admin/manage/user') {
          return true;
        }
        return false;
      },
      handle: (options: {
        dialog: MatDialog;
        object: Parse.Object;
        router?: Router;
      }) => {
        options.router?.navigate([
          '/nav-admin/manage/user/edit',
          { id: options.object?.get('user').id },
        ]);
      },
    },
    {
      name: '通过账号',
      place: 'item',
      show: (options: { object: Parse.Object }) => {
        if (
          location?.pathname == '/nav-admin/manage/user' &&
          options.object?.get('user')?.get('accountState') == '待认证'
        ) {
          return true;
        }
        return false;
      },
      handle: (options: {
        dialog: MatDialog;
        object: Parse.Object;
        router?: Router;
        modal: any;
      }) => {
        options.modal?.confirm({
          nzTitle: '确定通过认证吗?',
          nzContent: '',
          nzOkText: '确认',
          nzOkType: 'primary',
          nzOkDanger: true,
          nzOnOk: async () => {
            let query = new Parse.Query('_User');
            query.equalTo('objectId', options.object.get('user')?.id);
            let user = await query.first();
            user?.set('accountState', '已认证');
            await user?.save();
            history.go(0);
          },
          nzCancelText: '取消',
          nzOnCancel: () => console.log('Cancel'),
        });
      },
    },
    {
      name: '禁用账号',
      place: 'item',
      show: (options: { object: Parse.Object }) => {
        if (location?.pathname == '/nav-admin/manage/user') {
          return true;
        }
        return false;
      },
      handle: (options: {
        dialog: MatDialog;
        object: Parse.Object;
        router?: Router;
        modal: any;
      }) => {
        options.modal?.confirm({
            nzTitle: '确定禁用账号吗?',
            nzContent: '禁用后用户将不能登录',
            nzOkText: '确认',
            nzOkType: 'primary',
            nzOkDanger: true,
            nzOnOk: async () => {
              let query = new Parse.Query('_User');
              query.equalTo('objectId', options.object.get('user')?.id);
              let user = await query.first();
              console.log(user);
              user?.set('accountState', '已禁用');

              await user?.save();
              history.go(0);
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel'),
          });
      },
    },
    {
      name: '删除账号',
      place: 'item',
      show: (options: { object: Parse.Object }) => {
        if (location?.pathname == '/nav-admin/manage/user') {
          return true;
        }
        return false;
      },
      handle: (options: {
        dialog: MatDialog;
        object: Parse.Object;
        router?: Router;
        modal:any
      }) => {
        options.modal?.confirm({
            nzTitle: '确定删除账号吗?',
            nzContent: '删除后不可恢复',
            nzOkText: '确认',
            nzOkType: 'primary',
            nzOkDanger: true,
            nzOnOk: async () => {
              let query = new Parse.Query('_User');
              query.equalTo('objectId', options.object.get('user')?.id);
              let user = await query.first();
              user?.set('isDeleted', true);
              await user?.save();
              history.go(0);
            },
            nzCancelText: '取消',
            nzOnCancel: () => console.log('Cancel'),
          });
      },
    },
  ],
  fieldsArray: [
    {
      key: 'user',
      name: '用户',
      type: 'Pointer',
      targetClass: '_User',
      isHeader: true,
      showName: '${name}',
    },
    {
      key: 'user',
      name: '手机号',
      type: 'Pointer',
      targetClass: '_User',
      isHeader: true,
      showName: '${phone}',
    },
    // {
    //   key: 'user',
    //   name: '邮箱',
    //   type: 'Pointer',
    //   targetClass: '_User',
    //   isHeader: true,
    //   showName: '${email}',
    // },
    { key: 'email', name: '邮箱', type: 'String', isHeader: true },
    { key: 'identity', name: '人员类型', type: 'String', isHeader: true },
    {
      key: 'user',
      name: '认证状态',
      type: 'Pointer',
      targetClass: '_User',
      isHeader: true,
      showName: '${accountState}',
    },
    { key: 'companyType', name: '单位类型', type: 'String', isHeader: true },
    // {key:"mobile",name:"手机号",type:"String",isHeader:true},
    // {key:"emal",name:"邮箱",type:"String",isHeader:true},
    // {key:"createdAt",name:"创建时间",type:"Date",isHeader:true,},
  ],
  nzWidthConfig:['50px','100px','100px','100px','100px','100px','100px','100px']
};
