import { getDeptLazyTree } from '@/api/system/dept';

export const userOption = safe => {
  const validatePass = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('请输入密码'));
    } else {
      callback();
    }
  };
  const validatePass2 = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('请再次输入密码'));
    } else if (value !== safe.form.password) {
      callback(new Error('两次输入密码不一致!'));
    } else {
      callback();
    }
  };
  return {
    height: 'auto',
    calcHeight: 150,
    tip: false,
    searchShow: true,
    searchMenuSpan: 24,
    searchIcon: true,
    searchIndex: 4,
    searchMenuPosition: 'right',
    border: true,
    index: true,
    selection: true,
    addBtn: false,
    viewBtn: true,
    dialogType: 'dialog',
    dialogWidth: '90%',
    labelPosition: 'right',
    labelWidth: 120,
    dialogClickModal: false,
    column: [
      {
        label: '账号名',
        prop: 'account',
        search: true,
        searchOrder: 2,
        display: false,
      },
      {
        label: '姓名',
        prop: 'realName',
        search: true,
        searchOrder: 4,
        display: false,
      },
      {
        label: '手机号',
        prop: 'phone',
        search: true,
        searchOrder: 3,
        display: false,
      },
      { label: '工号', prop: 'code', display: false },
      {
        label: '所属部门',
        prop: 'deptName',
        slot: true,
        overHidden: true,
        display: false,
      },
      { label: '岗位', prop: 'postName', overHidden: true, display: false },
      { label: '性别', prop: 'sexName', width: 80, display: false },
      { label: 'OA人员id', prop: 'oaPersonId', minWidth: 120, overHidden: true, display: false },
      { label: '备注', prop: 'remark', minWidth: 160, overHidden: true, display: false },
      {
        label: '状态',
        prop: 'status',
        type: 'select',
        dicData: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
        dataType: 'number',
        search: true,
        searchOrder: 1,
        display: false,
      },
    ],
    group: [
      {
        label: '基础信息',
        prop: 'baseInfo',
        icon: 'el-icon-user-solid',
        column: [
          {
            label: '账号名',
            prop: 'account',
            span: 6,
            rules: [
              {
                required: true,
                message: '请输入账号名',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '密码',
            prop: 'password',
            hide: true,
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            rules: [{ required: true, validator: validatePass, trigger: 'blur' }],
          },
          {
            label: '确认密码',
            prop: 'password2',
            hide: true,
            editDisplay: false,
            viewDisplay: false,
            addDisplay: false,
            rules: [{ required: true, validator: validatePass2, trigger: 'blur' }],
          },
        ],
      },
      {
        label: '详细信息',
        prop: 'detailInfo',
        icon: 'el-icon-s-order',
        column: [
          {
            label: '用户昵称',
            prop: 'name',
            hide: true,
            rules: [
              {
                required: true,
                message: '请输入用户昵称',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '姓名',
            prop: 'realName',
            span: 6,
            rules: [
              {
                required: true,
                message: '请输入姓名',
                trigger: 'blur',
              },
              {
                min: 2,
                max: 10,
                message: '姓名长度在2到5个字符',
              },
            ],
          },
          {
            label: '手机号码',
            prop: 'phone',
            span: 6,
            rules: [{ required: true, message: '请输入手机号码', trigger: 'blur' }],
            overHidden: true,
          },
          {
            label: '电子邮箱',
            prop: 'email',
            span: 6,
            overHidden: true,
          },
          {
            label: '用户性别',
            prop: 'sex',
            type: 'select',
            dicData: [
              {
                label: '男',
                value: 1,
              },
              {
                label: '女',
                value: 2,
              },
              {
                label: '未知',
                value: 3,
              },
            ],
            span: 6,
          },
          {
            label: '用户生日',
            type: 'date',
            prop: 'birthday',
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            hide: true,
          },
          {
            label: '账号状态',
            prop: 'statusName',
            hide: true,
            display: false,
          },
        ],
      },
      {
        label: '职责信息',
        prop: 'dutyInfo',
        icon: 'el-icon-s-custom',
        column: [
          {
            label: '用户编号',
            prop: 'code',
            label: '工号',
            span: 6,
          },
          {
            label: '所属角色',
            prop: 'roleId',
            multiple: true,
            type: 'tree',
            dicData: [],
            props: {
              label: 'title',
            },
            checkStrictly: true,
            slot: true,
            display: false,
          },
          {
            label: '所属部门',
            prop: 'deptId',
            type: 'tree',
            multiple: true,
            dicData: [],
            props: {
              label: 'title',
            },
            checkStrictly: true,
            slot: true,
            span: 6,
            rules: [
              {
                required: true,
                message: '请选择所属部门',
                trigger: 'click',
              },
            ],
          },
          {
            label: '所属岗位',
            prop: 'postId',
            type: 'tree',
            multiple: false,
            dicData: [],
            props: {
              label: 'postName',
              value: 'id',
            },
            rules: [
              {
                required: true,
                message: '请选择所属岗位',
                trigger: 'click',
              },
            ],
            span: 6,
          },
          {
            label: '直属主管',
            prop: 'leaderId',
            type: 'tree',
            multiple: true,
            filterable: true,
            dicData: [],
            props: {
              label: 'realName',
              value: 'id',
            },
            clearable: true,
            display: false,
          },
          {
            label: '是否主管',
            prop: 'isLeader',
            type: 'switch',
            dicData: [
              {
                label: '否',
                value: 0,
              },
              {
                label: '是',
                value: 1,
              },
            ],
            value: 0,
            display: false,
          },
          {
            label: '人员类别',
            prop: 'personCategory',
            type: 'select',
            span: 6,
            value: 1,
            dicData: [
              { label: '内部员工', value: 1 },
              { label: '承运商', value: 2 },
            ],
          },
          {
            label: '数据权限范围',
            prop: 'dataScopeRange',
            type: 'radio',
            span: 24,
            value: 1,
            dicData: [
              { label: '仅所属组织', value: 1 },
              { label: '全部组织', value: 2 },
              { label: '自定义', value: 3 },
            ],
          },
          {
            label: '数据层级范围',
            prop: 'dataLevelRange',
            type: 'radio',
            span: 24,
            value: 1,
            dicData: [
              { label: '包含下级', value: 1 },
              { label: '仅本级', value: 2 },
            ],
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            span: 24,
            maxlength: 200,
            showWordLimit: true,
          },
        ],
      },
    ],
  };
};

export const platformOption = {
  tip: false,
  searchShow: true,
  searchMenuSpan: 24,
  searchIcon: true,
  searchIndex: 8,
  searchMenuPosition: 'right',
  border: true,
  index: true,
  selection: true,
  viewBtn: true,
  dialogClickModal: false,
  menuWidth: 180,
  editBtnText: '配置',
  column: [
    {
      label: '登录账号',
      prop: 'account',
      search: true,
      display: false,
    },
    {
      label: '所属租户',
      prop: 'tenantName',
      slot: true,
      display: false,
    },
    {
      label: '用户姓名',
      prop: 'realName',
      search: true,
      display: false,
    },
    {
      label: '用户平台',
      prop: 'userTypeName',
      slot: true,
      display: false,
    },
    {
      label: '用户平台',
      type: 'select',
      dicUrl: '/blade-system/dict/dictionary?code=user_type',
      props: {
        label: 'dictValue',
        value: 'dictKey',
      },
      dataType: 'number',
      search: true,
      hide: true,
      display: false,
      prop: 'userType',
      rules: [
        {
          required: true,
          message: '请选择用户平台',
          trigger: 'blur',
        },
      ],
    },
    {
      label: '用户拓展',
      prop: 'userExt',
      type: 'textarea',
      minRows: 8,
      span: 24,
      overHidden: true,
      row: true,
      hide: true,
    },
  ],
};

export const excelOption = {
  submitBtn: false,
  emptyBtn: false,
  column: [
    {
      label: '模板上传',
      prop: 'excelFile',
      type: 'upload',
      drag: true,
      loadText: '模板上传中，请稍等',
      span: 24,
      propsHttp: {
        res: 'data',
      },
      tip: '请上传 .xls,.xlsx 标准格式文件',
      action: '/blade-system/user/import-user',
    },
    {
      label: '数据覆盖',
      prop: 'isCovered',
      type: 'switch',
      align: 'center',
      width: 80,
      dicData: [
        {
          label: '否',
          value: 0,
        },
        {
          label: '是',
          value: 1,
        },
      ],
      value: 0,
      slot: true,
      rules: [
        {
          required: true,
          message: '请选择是否覆盖',
          trigger: 'blur',
        },
      ],
    },
    {
      label: '模板下载',
      prop: 'excelTemplate',
      formslot: true,
      span: 24,
    },
  ],
};

export const platformFormOption = {
  submitBtn: false,
  emptyBtn: false,
  labelWidth: 'auto',
  column: [
    {
      label: '用户账号',
      prop: 'account',
      disabled: true,
      span: 24,
    },
    {
      label: '用户平台',
      type: 'select',
      prop: 'userType',
      dicUrl: '/blade-system/dict/dictionary?code=user_type',
      props: {
        label: 'dictValue',
        value: 'dictKey',
      },
      dataType: 'number',
      span: 24,
      rules: [{ required: true, message: '请选择用户平台', trigger: 'blur' }],
    },
    {
      label: '用户拓展',
      prop: 'userExt',
      type: 'textarea',
      minRows: 8,
      span: 24,
    },
  ],
};

export const treeOption = {
  nodeKey: 'id',
  lazy: true,
  treeLoad: function (node, resolve) {
    const parentId = node.level === 0 ? 0 : node.data.id;
    getDeptLazyTree(parentId).then(res => {
      resolve(
        res.data.data.map(item => {
          return {
            ...item,
            leaf: !item.hasChildren,
          };
        })
      );
    });
  },
  addBtn: false,
  menu: false,
  size: 'small',
  props: {
    labelText: '标题',
    label: 'title',
    value: 'value',
    children: 'children',
  },
};
