<template>
  <div class="page">
    <div class="ele-body">
      <a-card title="客户管理" :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="companyId"
          :columns="columns"
          :datasource="datasource"
          v-model:selection="selection"
          :customRow="customRow"
          tool-class="ele-toolbar-form"
          :scroll="{ x: 1200 }"
          class="sys-org-table"
          :striped="true"
        >
          <template #toolbar>
            <search
              @search="reload"
              :selection="selection"
              @add="openEdit"
              @remove="removeBatch"
            />
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'companyName'">
              <div class="company-box">
                <a-avatar
                  v-if="record.companyLogo"
                  :src="FILE_THUMBNAIL + record.companyLogo"
                />
                <div class="company-name">
                  <span>{{ record.shortName }}</span>
                  <span class="ele-text-placeholder">{{
                    record.companyName
                  }}</span>
                </div>
              </div>
            </template>
            <template v-if="column.key === 'members'">
              <span>{{ record.users }}/{{ record.members }}</span>
            </template>
            <template v-if="column.key === 'storage'">
              {{ getFileSize(record.storage) }}/{{
                getFileSize(record.storageMax)
              }}
            </template>
            <template v-if="column.key === 'version'">
              <a-tag v-if="record.version === 20" color="green">授权版</a-tag>
              <a-tag v-if="record.version === 10">体验版</a-tag>
            </template>
            <template v-if="column.key === 'departments'">
              <span>{{ record.departments }}个部门</span>
            </template>
            <template v-if="column.key === 'createTime'">
              <div class="create-time">
                <span class="ele-text-placeholder">{{
                  record.createTime
                }}</span>
                <span class="ele-text-placeholder">{{
                  record.expirationTime
                }}</span>
              </div>
            </template>
            <template v-if="column.key === 'authentication'">
              <a-tag v-if="record.authentication === 1" color="green"
                >已认证</a-tag
              >
              <a-tag v-if="record.authentication === 0">未认证</a-tag>
            </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>
              <a-tag v-if="record.status === 2" color="purple">注销</a-tag>
              <a-tag v-if="record.status === 3" color="orange">转卖</a-tag>
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <!--                <a @click="openEdit(record)">编辑</a>-->
                <!--                <a-divider type="vertical" />-->
                <a @click="openInfo(record)">详情</a>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <CompanyEdit v-model:visible="showEdit" :data="current" @done="reload" />
    <!-- 企业详情 -->
    <CompanyInfo v-model:visible="showInfo" :data="current" @done="reload" />
  </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 type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { toDateString } from 'ele-admin-pro';
  import Search from './components/search.vue';
  import CompanyEdit from './components/company-edit.vue';
  import CompanyInfo from './components/company-info.vue';
  import { pageCompany, removeBatchCompany } from '@/api/system/company';
  import { FILE_THUMBNAIL } from '@/config/setting';
  import { getFileSize } from '@/utils/common';
  import { Company, CompanyParam } from '@/api/system/company/model';
  import { message, Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';

  defineProps<{
    activeKey?: boolean;
    data?: any;
  }>();

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

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '企业名称',
      dataIndex: 'companyName',
      key: 'companyName',
      width: 280
    },
    // {
    //   title: '企业简称',
    //   dataIndex: 'shortName',
    //   key: 'shortName'
    // },
    {
      title: '所在城市',
      dataIndex: 'city',
      key: 'city'
    },
    {
      title: '企业域名',
      dataIndex: 'domain',
      key: 'domain'
    },
    {
      title: '联系电话',
      dataIndex: 'phone',
      key: 'phone',
      hideInTable: true
    },
    {
      title: '企业成员',
      dataIndex: 'members',
      key: 'members'
    },
    {
      title: '企业部门',
      dataIndex: 'departments',
      key: 'departments',
      hideInTable: true
    },
    {
      title: '存储空间',
      dataIndex: 'storage',
      key: 'storage',
      hideInTable: true
    },
    {
      title: '实名认证',
      dataIndex: 'authentication',
      key: 'authentication'
    },
    {
      title: '版本',
      dataIndex: 'version',
      key: 'version'
    },
    {
      title: '注册时间 / 到期时间',
      dataIndex: 'createTime',
      key: 'createTime',
      align: 'center',
      sorter: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center',
      hideInSetting: true
    }
  ]);

  // 表格选中数据
  const selection = ref<Company[]>([]);
  // 当前编辑数据
  const current = ref<Company | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 是否显示详情弹窗
  const showInfo = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    // 搜索条件
    return pageCompany({
      ...where,
      ...orders,
      page,
      limit
    });
  };

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

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

  /* 打开详情弹窗 */
  const openInfo = (row?: Company) => {
    current.value = row ?? null;
    showInfo.value = true;
  };

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

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

  reload();
</script>

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

<style lang="less" scoped>
  p {
    line-height: 0.8;
  }
  .sys-org-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .sys-org-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
  .price-edit {
    padding-right: 5px;
  }
  .comments {
    max-width: 200px;
  }
  .company-box {
    display: flex;
    align-items: center;
    .company-name {
      padding-left: 8px;
      display: flex;
      flex-direction: column;
    }
  }
  .create-time {
    display: flex;
    flex-direction: column;
  }
</style>
