import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { dict, cityTree } from '/@/api/common/api'
import { useUserStore } from '/@/store/modules/user';
import { ref } from 'vue';
import { render } from '/@/utils/common/renderUtils';

const userStore: any = useUserStore();

export const columns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '用户名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '用户级别',
    dataIndex: 'userIdentity',
    width: 100,
    customRender: ({ text }) => {
      return render.renderDict(text, 'user_levle');
    },
  },
  {
    title: '用户类型',
    dataIndex: 'userType',
    width: 100,
    customRender: ({ text }) => {
      return render.renderDict(text, 'dk_user_type');
    },
  },
  {
    title: '行政区划',
    dataIndex: 'regionName',
    width: 100,
  },
  {
    title: '状态',
    dataIndex: 'status_dictText',
    width: 80,
  },
];
// 搜索的
export const recycleColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: render.renderAvatar,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'username',
    component: 'JInput',
    colProps: { span: 6 },
  },
  {
    label: '用户名',
    field: 'realname',
    component: 'JInput',
    colProps: { span: 6 },
  },

  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '用户级别',
    field: 'userIdentity',
    colProps: { span: 6 },
    component: 'ApiSelect',
    slot: 'userLevle',
    ifShow: () => {
      return userStore?.userInfo?.userIdentity == 1 || userStore?.userInfo?.userIdentity == 2 ? true : false
    },
    componentProps: ({ }) => {
      {
        return {
          api: dict,
          params: { key: 'user_levle' },
          labelField: 'text',
          valueField: 'value',

        }
      }
    }
  },
  {
    label: '用户类型',
    field: 'userType',
    component: 'ApiSelect',
    componentProps: {
      api: dict,
      params: { key: 'dk_user_type' },
      labelField: 'text',
      valueField: 'value',
    },
    colProps: { span: 6 },
  },

  {
    label: '用户状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'user_status',
      placeholder: '请选择状态',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
  // 修改权限
  {
    label: '行政区划',
    field: 'codeRegion',
    component: 'JTreeSelect',
    colProps: { span: 6 },
    ifShow: (userStore?.userInfo?.userIdentity == '1' || userStore?.userInfo?.userIdentity == '2') ? true : false,
    slot: 'codeRegion',
    // componentProps: ({ formModel, formActionType }) => {
    //   {

    //     return {
    //       url: '/sys/api/tree/xzbm',
    //       isdisabled: formModel.userIdentity,
    //       value: '440000'
    //     }
    //   }
    // }
    // cityTree
  },
];

// 获取等级
const userIdent = ref<any>([])
export const getUserLevel = () => {
  const getIsSuspect = async () => {
    userIdent.value = await dict({ key: 'user_levle' })

  }
  getIsSuspect();

  return { data: userIdent };
}
// 获取行政区域
const citys = ref<any>([])
export const getCitys = () => {
  const getIsSuspect = async () => {
    citys.value = await cityTree()
  }
  getIsSuspect();
  console.log(citys, 'zoulm')
  return { data: citys };
}
// 新增
export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
    // dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'username', model, schema, true),
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
  {
    label: '用户姓名',
    field: 'realname',
    required: true,
    component: 'Input',
  },
  {
    label: '角色',
    field: 'selectedroles',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: '',
      labelField: 'roleName',
      valueField: 'id',
    },
  },
  // {
  //   label: '所属部门',
  //   field: 'selecteddeparts',
  //   component: 'JSelectDept',
  //   componentProps: ({ formActionType, formModel }) => {
  //     return {
  //       sync: false,
  //       checkStrictly: true,
  //       defaultExpandLevel: 2,

  //       onSelect: (options, values) => {
  //         const { updateSchema } = formActionType;
  //         //所属部门修改后更新负责部门下拉框数据
  //         updateSchema([
  //           {
  //             field: 'departIds',
  //             componentProps: { options },
  //           },
  //         ]);
  //         //所属部门修改后更新负责部门数据
  //         formModel.departIds && (formModel.departIds = formModel.departIds.filter((item) => values.value.indexOf(item) > -1));
  //       },
  //     };
  //   },
  // },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    dynamicRules: ({ }) => {
      // { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0] },
      return [

        // { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误' },
      ];
    },
  },
  // {
  //   label: '负责部门',
  //   field: 'departIds',
  //   component: 'Select',
  //   componentProps: {
  //     mode: 'multiple',
  //   },
  //   ifShow: ({ values }) => values.userIdentity == 2,
  // },

  {
    label: '用户等级',
    field: 'userIdentity',
    component: 'ApiSelect',
    required: true,
    slot: 'level',
    componentProps: ({ formModel, formActionType }) => {
      {
        return {
          api: dict,
          params: { key: 'user_levle' },
          labelField: 'text',
          valueField: 'value',
          onChange: (v) => {
            const { updateSchema } = formActionType;
            if (v) {
              console.log(v, 'v')
              formModel.codeRegion = ''
              updateSchema({
                field: 'codeRegion',
                componentProps: {
                  isdisabled: v
                },
              });
            }
          }
        }
      }
    }
  },
  {
    label: '行政区划',
    field: 'codeRegion',
    required: true,
    component: 'JTreeSelect',
    slot: 'codeRegion',
    componentProps: ({ formModel, formActionType }) => {
      {
        return {
          url: '/sys/api/tree/xzbm',
          isdisabled: formModel.userIdentity
        }
      }
    }
    // cityTree
  },
  {
    label: '用户类型',
    field: 'userType',
    required: true,
    component: 'ApiSelect',
    componentProps: {
      api: dict,
      params: { key: 'dk_user_type' },
      labelField: 'text',
      valueField: 'value',
    },
  },
];

export const formPasswordSchema: FormSchema[] = [
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    componentProps: { readOnly: true },
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '请输入登录密码',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
];

export const formAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: '代理人用户名',
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 10,
    },
  },
  {
    field: 'startTime',
    label: '代理开始时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择代理开始时间',
    },
  },
  {
    field: 'endTime',
    label: '代理结束时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择代理结束时间',
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

export const formQuitAgentSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    componentProps: {
      readOnly: true,
      allowClear: false,
    },
  },
  {
    field: 'agentUserName',
    label: '交接人员',
    required: true,
    component: 'JSelectUser',
    componentProps: {
      rowKey: 'username',
      labelKey: 'realname',
      maxSelectCount: 1,
    },
  },
  {
    field: 'startTime',
    label: '交接开始时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择交接开始时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'endTime',
    label: '交接结束时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择交接结束时间',
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      type: 'radioButton',
    },
  },
];

//租户用户列表
export const userTenantColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '用户姓名',
    dataIndex: 'realname',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 120,
    customRender: render.renderAvatar,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: '部门',
    width: 150,
    dataIndex: 'orgCodeTxt',
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ text }) => {
      if (text === '1') {
        return '正常';
      } else if (text === '3') {
        return '审批中';
      } else {
        return '已拒绝';
      }
    },
  },
];

//用户租户搜索表单
export const userTenantFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'username',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '用户名',
    field: 'realname',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sex',
      placeholder: '请选择性别',
      stringToNumber: true,
    },
    colProps: { span: 6 },
  },
];
