import { h } from 'vue';
import { fetchGetDepartmentList } from '@/service/api/system-department';
import { fetchGetUserList } from '@/service/api';
import type { FormSchema } from '@/components/zt/Form/types/form';
import { ApiSelect } from '@/components/zt/ApiSelect';

export const formSchems: FormSchema[] = [
  {
    field: 'deptId',
    show: false,
    component: 'NInput'
  },
  {
    field: 'parentId',
    label: '上级部门',
    component: 'ApiTreeSelect',
    required: false,
    componentProps: {
      api: fetchGetDepartmentList,
      labelFeild: 'name',
      valueFeild: 'id',
      immediate: true,
      resultFeild: 'data'
    }
  },
  {
    field: 'name',
    label: '部门名称',
    component: 'NInput',
    required: true
  },
  {
    field: 'sort',
    label: '显示排序',
    component: 'NInputNumber',
    required: true,
    componentProps: {
      min: 1
    }
  },
  {
    field: 'leaderUserId',
    label: '负责人',
    component: 'ApiSelect',
    render({ model, field }) {
      return h(ApiSelect, {
        api: fetchGetUserList,
        labelFeild: 'username',
        valueFeild: 'userId',
        resultFeild: 'data.records',
        filterable: true,
        remote: true,
        filterFeild: 'username',
        value: model[field],
        onUpdateValue: (value, option: Recordable) => {
          model[field] = value;
          model.phone = option.mobile;
          model.email = option.email;
        }
      });
    }
  },
  {
    field: 'phone',
    label: '联系电话',
    component: 'NInput',
    ifShow({ model }) {
      return model.leaderUserId;
    }
  },
  {
    field: 'email',
    label: '邮箱',
    component: 'NInput',
    ifShow({ model }) {
      return model.leaderUserId;
    }
  },
  {
    field: 'status',
    label: '部门状态',
    component: 'NRadioGroup',
    required: true,
    componentProps: {
      options: [
        {
          label: '正常',
          value: 1
        },
        {
          label: '停用',
          value: 0
        }
      ]
    },
    defaultValue: 1
  }
];

export const searchSchems: FormSchema[] = [
  {
    field: 'keywords',
    label: '部门名称',
    component: 'NInput'
  },
  {
    field: 'status',
    label: '部门状态',
    component: 'NSelect',
    componentProps: {
      options: [
        {
          label: '启用',
          value: 1
        },
        {
          label: '禁用',
          value: 0
        }
      ]
    }
  }
];
