<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false" :body-style="{ padding: '16px' }">
        <ele-pro-table
          ref="tableRef"
          row-key="oaCompanyId"
          :columns="columns"
          :datasource="datasource"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          class="sys-org-table"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
              @batchMove="openMove"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'image'">
              <a-image :src="record.image" :width="50" />
            </template>
            <template v-if="column.key === 'status'">
              <a-tag v-if="record.status === 0" color="green">显示</a-tag>
              <a-tag v-if="record.status === 1" color="red">隐藏</a-tag>
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a-popconfirm
                  title="确定要删除此记录吗？"
                  @confirm="remove(record)"
                >
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>

      <!-- 编辑弹窗 -->
      <OaCompanyEdit v-model:visible="showEdit" :data="current" @done="reload" />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import { toDateString } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import Search from './components/search.vue';
  import OaCompanyEdit from './components/oaCompanyEdit.vue';
  import { pageOaCompany, removeOaCompany, removeBatchOaCompany } from '@/api/oa/oaCompany';
  import type { OaCompany, OaCompanyParam } from '@/api/oa/oaCompany/model';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格选中数据
  const selection = ref<OaCompany[]>([]);
  // 当前编辑数据
  const current = ref<OaCompany | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 是否显示批量移动弹窗
  const showMove = ref(false);
  // 加载状态
  const loading = ref(true);

  // 表格数据源
  const datasource: DatasourceFunction = ({
    page,
    limit,
    where,
    orders,
    filters
  }) => {
    if (filters) {
      where.status = filters.status;
    }
    return pageOaCompany({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: '企业id',
      dataIndex: 'companyId',
      key: 'companyId',
      align: 'center',
      width: 90,
    },
    {
      title: '企业简称',
      dataIndex: 'shortName',
      key: 'shortName',
      align: 'center',
    },
    {
      title: '企业全称',
      dataIndex: 'companyName',
      key: 'companyName',
      align: 'center',
    },
    {
      title: '企业标识',
      dataIndex: 'companyCode',
      key: 'companyCode',
      align: 'center',
    },
    {
      title: '类型 10企业 20政府单位',
      dataIndex: 'companyType',
      key: 'companyType',
      align: 'center',
    },
    {
      title: '企业类型多选',
      dataIndex: 'companyTypeMultiple',
      key: 'companyTypeMultiple',
      align: 'center',
    },
    {
      title: '应用标识',
      dataIndex: 'companyLogo',
      key: 'companyLogo',
      align: 'center',
    },
    {
      title: '应用类型',
      dataIndex: 'appType',
      key: 'appType',
      align: 'center',
    },
    {
      title: '绑定域名',
      dataIndex: 'domain',
      key: 'domain',
      align: 'center',
    },
    {
      title: '联系电话',
      dataIndex: 'phone',
      key: 'phone',
      align: 'center',
    },
    {
      title: '座机电话',
      dataIndex: 'tel',
      key: 'tel',
      align: 'center',
    },
    {
      title: '邮箱',
      dataIndex: 'email',
      key: 'email',
      align: 'center',
    },
    {
      title: '发票抬头',
      dataIndex: 'invoiceHeader',
      key: 'invoiceHeader',
      align: 'center',
    },
    {
      title: '企业法人',
      dataIndex: 'businessEntity',
      key: 'businessEntity',
      align: 'center',
    },
    {
      title: '服务开始时间',
      dataIndex: 'startTime',
      key: 'startTime',
      align: 'center',
    },
    {
      title: '服务到期时间',
      dataIndex: 'expirationTime',
      key: 'expirationTime',
      align: 'center',
    },
    {
      title: '应用版本 10体验版 20授权版 30旗舰版',
      dataIndex: 'version',
      key: 'version',
      align: 'center',
    },
    {
      title: '成员数量(人数上限)',
      dataIndex: 'members',
      key: 'members',
      align: 'center',
    },
    {
      title: '成员数量(当前)',
      dataIndex: 'users',
      key: 'users',
      align: 'center',
    },
    {
      title: '行业类型(父级)',
      dataIndex: 'industryParent',
      key: 'industryParent',
      align: 'center',
    },
    {
      title: '行业类型(子级)',
      dataIndex: 'industryChild',
      key: 'industryChild',
      align: 'center',
    },
    {
      title: '部门数量',
      dataIndex: 'departments',
      key: 'departments',
      align: 'center',
    },
    {
      title: '存储空间',
      dataIndex: 'storage',
      key: 'storage',
      align: 'center',
    },
    {
      title: '存储空间(上限)',
      dataIndex: 'storageMax',
      key: 'storageMax',
      align: 'center',
    },
    {
      title: '所在国家',
      dataIndex: 'country',
      key: 'country',
      align: 'center',
    },
    {
      title: '所在省份',
      dataIndex: 'province',
      key: 'province',
      align: 'center',
    },
    {
      title: '所在城市',
      dataIndex: 'city',
      key: 'city',
      align: 'center',
    },
    {
      title: '所在辖区',
      dataIndex: 'region',
      key: 'region',
      align: 'center',
    },
    {
      title: '街道地址',
      dataIndex: 'address',
      key: 'address',
      align: 'center',
    },
    {
      title: '经度',
      dataIndex: 'longitude',
      key: 'longitude',
      align: 'center',
    },
    {
      title: '纬度',
      dataIndex: 'latitude',
      key: 'latitude',
      align: 'center',
    },
    {
      title: '备注',
      dataIndex: 'comments',
      key: 'comments',
      align: 'center',
    },
    {
      title: '是否实名认证',
      dataIndex: 'authentication',
      key: 'authentication',
      align: 'center',
    },
    {
      title: '企业默认主体',
      dataIndex: 'authoritative',
      key: 'authoritative',
      align: 'center',
    },
    {
      title: 'request合法域名',
      dataIndex: 'requestUrl',
      key: 'requestUrl',
      align: 'center',
    },
    {
      title: 'socket合法域名',
      dataIndex: 'socketUrl',
      key: 'socketUrl',
      align: 'center',
    },
    {
      title: '主控端域名',
      dataIndex: 'serverUrl',
      key: 'serverUrl',
      align: 'center',
    },
    {
      title: '业务域名',
      dataIndex: 'modulesUrl',
      key: 'modulesUrl',
      align: 'center',
    },
    {
      title: '是否推荐',
      dataIndex: 'recommend',
      key: 'recommend',
      align: 'center',
    },
    {
      title: '点赞数量',
      dataIndex: 'likes',
      key: 'likes',
      align: 'center',
    },
    {
      title: '点击数量',
      dataIndex: 'clicks',
      key: 'clicks',
      align: 'center',
    },
    {
      title: '购买数量',
      dataIndex: 'buys',
      key: 'buys',
      align: 'center',
    },
    {
      title: '是否含税, 0不含, 1含',
      dataIndex: 'isTax',
      key: 'isTax',
      align: 'center',
    },
    {
      title: '当前克隆的租户ID',
      dataIndex: 'planId',
      key: 'planId',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
    },
    {
      title: '排序号',
      dataIndex: 'sortNumber',
      key: 'sortNumber',
      align: 'center',
    },
    {
      title: '用户ID',
      dataIndex: 'userId',
      key: 'userId',
      align: 'center',
    },
    {
      title: '是否删除, 0否, 1是',
      dataIndex: 'deleted',
      key: 'deleted',
      align: 'center',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      align: 'center',
      sorter: true,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd')
    },
    {
      title: '修改时间',
      dataIndex: 'updateTime',
      key: 'updateTime',
      align: 'center',
    },
    {
      title: '操作',
      key: 'action',
      width: 180,
      fixed: 'right',
      align: 'center',
      hideInSetting: true
    }
  ]);

  /* 搜索 */
  const reload = (where?: OaCompanyParam) => {
    selection.value = [];
    tableRef?.value?.reload({ where: where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: OaCompany) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 打开批量移动弹窗 */
  const openMove = () => {
    showMove.value = true;
  };

  /* 删除单个 */
  const remove = (row: OaCompany) => {
    const hide = message.loading('请求中..', 0);
    removeOaCompany(row.oaCompanyId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的记录吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = message.loading('请求中..', 0);
        removeBatchOaCompany(selection.value.map((d) => d.oaCompanyId))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };

  /* 查询 */
  const query = () => {
    loading.value = true;
  };

  /* 自定义行属性 */
  const customRow = (record: OaCompany) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        openEdit(record);
      }
    };
  };
  query();
</script>

<script lang="ts">
  export default {
    name: 'OaCompany'
  };
</script>

<style lang="less" scoped></style>
