import { mockData } from './mock';
import { cloneDeep } from 'lodash-es';
import { Modal, message } from 'ant-design-vue';
import { createVNode, nextTick, ref } from 'vue';
import { NsMessage, NsModal } from '/nerv-lib/component';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { enterPrise } from '/@/api/origanizemanage';
import { http } from '/nerv-lib/util/http';
import { generateRandomString } from '/@/util/generateRandomString';
import { copyText } from '/@/util/copyText';

type status = 'NORMAL' | 'FREEZE';
export const formConfig = [
  {
    field: 'orgId',
    label: '组织ID',
    component: 'NsInput',
    show: false,
  },
  {
    field: 'orgCode',
    label: '组织ID',
    component: 'NsInput',
    componentProps: {
      disabled: true,
    },
    rules: [
      {
        required: true,
      },
    ],
  },
  {
    field: 'orgName',
    label: '组织名称',
    component: 'NsInput',
    componentProps: {
      placeholder: '请输入',
      maxLength: 20,
    },
    rules: [{ required: true }],
  },
  {
    field: 'cliqueName',
    label: '集团名称',
    component: 'NsInput',
    componentProps: {
      placeholder: '请输入集团名称',
      maxLength: 20,
    },
    rules: [{ required: true }],
  },
  {
    field: 'cityName',
    label: '省市区',
    component: 'NsCascader',
    fieldMap: ['province', 'city', 'county'],
    componentProps: {
      placeholder: '请选择',
      api: enterPrise.getArea,
      fieldNames: { label: 'regionName', value: 'regionName' },
      showSearch: true,
    },
    rules: [
      {
        required: true,
        message: '请选择省市区',
      },
    ],
  },
  {
    field: 'address',
    label: '地址',
    component: 'NsInput',
    componentProps: {
      placeholder: '请输入地址',
      maxLength: 50,
    },
  },
  {
    field: 'contacts',
    label: '联系人',
    component: 'NsInput',
    componentProps: {
      placeholder: '请输入联系人',
      maxLength: 10,
    },
    rules: [
      {
        required: true,
        message: '请输入联系人',
        trigger: 'blur',
      },
    ],
  },
  {
    field: 'phoneNumber',
    label: '联系电话',
    component: 'NsInput',
    componentProps: {
      placeholder: '请输入联系电话',
      maxLength: 11,
    },
    rules: [
      {
        required: true,
        message: '请输入联系电话',
        trigger: 'blur',
      },
      {
        pattern: /^1[3-9]\d{9}$/,
        message: '请输入正确的联系电话',
        trigger: 'blur',
      },
    ],
  },
];

const handle = async (record: any) => {
  const { orgName, orgId } = record;
  const res = await http.post(enterPrise.queryRootAc, {
    orgId,
  });
  const { accountNo, userId } = res.data[0];

  NsModal.confirm({
    title: '重置密码',
    icon: createVNode(ExclamationCircleOutlined),
    okText: '重置',
    content: createVNode('div', {}, [
      createVNode('p', {}, `组织名称:   ${orgName}`),
      createVNode('p', {}, `管理员账号:   ${accountNo}`),
    ]),
    onOk() {
      NsModal.confirm({
        title: '重置密码',
        icon: createVNode(ExclamationCircleOutlined),
        content: '是否确认重置该组织管理员密码',
        onOk() {
          const password = generateRandomString(6);
          http.post(enterPrise.resetPwd, { userId, password }).then(() => {
            NsModal.success({
              okText: '确认',
              content: createVNode('div', {}, [
                createVNode('p', {}, '重置成功'),
                createVNode('p', {}, '点击【确认】后自动复制密码并离开'),
              ]),
              onOk: () => copyText(password),
            });
          });
        },
      });
    },

    onCancel() {
      console.log('Cancel');
    },
  });
};

export const tableConfig = ({
  visible,
  formData,
  opType,
  getOrgRandomCode,
  borderVisible,
  serviceVisible,
  server: { getTree, currentServerRecord, getPermissionData },
  boundary: { getBoundaryTree, currentBoundaryRecord },
}: any) => {
  return {
    // title: '组织管理',
    api: enterPrise.list,
    headerActions: [
      {
        label: '新增',
        name: 'RoleTypeAdd',
        type: 'primary',
        handle: () => {
          formData.value = {};
          opType.value = 'add';
          getOrgRandomCode && getOrgRandomCode();
          visible.value = true;
        },
      },
    ],

    rowSelection: null,
    columns: [
      {
        title: '序号',
        dataIndex: 'address',
        width: 80,
        customRender: (text: any) => {
          return text.index + 1;
        },
      },
      {
        title: '组织ID',
        dataIndex: 'orgCode',
      },
      {
        title: '集团名称',
        dataIndex: 'cliqueName',
        textNumber: 8,
        textEllipsis: true,
      },
      {
        title: '组织名称',
        dataIndex: 'orgName',
        textNumber: 8,
        textEllipsis: true,
      },
      {
        title: '状态',
        dataIndex: 'status',
        customRender: ({ value }) => {
          return { NORMAL: '正常', FREEZE: '冻结' }[value as status];
        },
      },
      {
        title: '省市区',
        dataIndex: 'area',
        customRender: ({ record }) => {
          const { province = '', city = '', county = '' } = record;
          const result = [province, city];
          county && result.push(county);
          return result.join('/');
        },
      },
      {
        title: '地址',
        dataIndex: 'address',
        textNumber: 8,
        textEllipsis: true,
      },
      {
        title: '联系人',
        dataIndex: 'contacts',
      },
      {
        title: '联系电话',
        dataIndex: 'phoneNumber',
      },
    ],
    columnActions: {
      title: '操作',
      autoMergeAction: false,
      actionNumber: 1,
      actions: [
        {
          label: '编辑',
          name: 'RoleTypeEdit',
          handle: (record: any) => {
            const { province, city, county } = record;
            formData.value = record;
            formData.value.cityName = [province, city];
            county && formData.value.cityName.push(county);
            opType.value = 'edit';
            visible.value = true;
          },
        },
        {
          label: '组织边界',
          handle: (record: any) => {
            console.log(record, 'record');
            const { orgId, orgName } = (currentBoundaryRecord.value = record);
            getBoundaryTree({ orgId, orgName });
            nextTick(() => {
              borderVisible.value = true;
            });
          },
        },
        {
          label: '服务',
          handle: (record: any) => {
            const { orgId } = (currentServerRecord.value = record);
            opType.value = 'serverAdd';
            // getTree({});
            getPermissionData({ orgId });
            nextTick(() => {
              serviceVisible.value = true;
            });
          },
        },
        {
          label: '冻结',
          name: 'enterPriseFreeze',
          confirm: true,
          isReload: true,
          ifShow: ({ status }) => status === 'NORMAL',
          api: enterPrise.freeze,
          dynamicParams: 'orgId',
          defaultParams: {
            isFreeze: true,
          },
        },
        {
          label: '解冻',
          name: 'enterPriseUnFreeze',
          confirm: true,
          isReload: true,
          ifShow: ({ status }) => status === 'FREEZE',
          dynamicParams: 'orgId',
          api: enterPrise.freeze,
          defaultParams: {
            isFreeze: false,
          },
        },
        {
          label: '重置密码',
          name: 'RoleTypeEdit',
          handle,
        },
        {
          label: '删除',
          name: 'RoleTypeEdit',
          dynamicParams: 'orgId',
          confirm: true,
          isReload: true,
          api: enterPrise.del,
        },
      ],
    },

    formConfig: {
      schemas: [
        {
          field: 'orgCode',
          label: '组织ID',
          component: 'NsInput',
          componentProps: { placeholder: '请输入组织ID' },
        },
        {
          field: 'cliqueName',
          label: '集团名称',
          component: 'NsInput',
          componentProps: { placeholder: '请输入集团名称' },
        },
        {
          field: 'orgName',
          label: '组织名称',
          component: 'NsInput',
          componentProps: { placeholder: '请输入组织名称' },
        },
        {
          field: 'status',
          label: '状态',
          component: 'NsSelect',
          componentProps: {
            placeholder: '请选择状态',
            options: [
              {
                label: '正常',
                value: 'NORMAL',
              },
              {
                label: '冻结',
                value: 'FREEZE',
              },
            ],
          },
        },
        {
          field: 'cityName',
          label: '省市区',
          component: 'NsCascader',
          fieldMap: ['province', 'city', 'county'],
          componentProps: {
            placeholder: '请选择省市区',
            api: enterPrise.getArea,
            fieldNames: { label: 'regionName', value: 'regionName' },
            showSearch: true,
          },
        },
      ],
      params: {},
    },
    // pagination: { defaultPageSize: 10 },
    rowKey: 'id',
  };
};
