<template>
  <a-page-header :title="title" @back="() => $router.go(-1)">
    <a-card :bordered="false" :body-style="{ padding: '16px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="companyId"
        :columns="columns"
        :datasource="datasource"
        :scroll="{ x: 1200 }"
        cache-key="proSystemCompanyTable"
      >
        <template #toolbar>
          <search
            @search="reload"
            :selection="selection"
            @add="openEdit"
            @remove="removeBatch"
            @advanced="openAdvanced"
          />
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'shortName'">
            <div class="flex flex-row">
              <StarOutlined
                @click="onCollection(record)"
                class="tag-icon mt-1"
                :class="
                  record.collection
                    ? 'ele-text-warning'
                    : 'ele-text-placeholder'
                "
              />
              <div class="tenant-name-box flex flex-col">
                <a
                  :href="`https://${record.tenantId}.websoft.top`"
                  target="_blank"
                  class="tenant-name"
                >
                  {{ record.shortName }}
                </a>
                <span class="text-gray-400">{{ record.comments }}</span>
              </div>
            </div>
          </template>
          <template v-if="column.key === 'companyLogo'">
            <div class="company-box">
              <a-image
                :height="45"
                :width="45"
                :preview="false"
                :src="record.companyLogo"
                fallback="https://file.wsdns.cn/20230218/550e610d43334dd2a7f66d5b20bd58eb.svg"
              />
            </div>
          </template>
          <template v-if="column.key === 'phone'">
            <div class="ele-text-secondary">{{ record.companyName }}</div>
            <div
              v-any-role="['merchant', 'superAdmin']"
              class="ele-text-secondary"
              >{{ record.phone }}
            </div>
            <div v-role="'guest'" class="ele-text-secondary"
              >{{ record.mobile }}
            </div>
          </template>
          <template v-if="column.key === 'comments'">
            <span class="ele-text-secondary">{{ record.comments }}</span>
          </template>
          <template v-if="column.key === 'status'">
            <template v-if="record.status == 0">
              <a-tag>已下架</a-tag>
            </template>
            <template v-if="record.status == 1">
              <a-tag color="green">已上架</a-tag>
            </template>
          </template>
          <template v-if="column.key === 'expirationTime'">
            <template v-if="record.version == 30"> -</template>
            <template v-else>
              <div>{{ toDateString(record.createTime, 'yyyy-MM-dd') }}</div>
              <div>{{ toDateString(record.expirationTime, 'yyyy-MM-dd') }}</div>
              <div
                v-if="expirationTime(record.expirationTime) < 0"
                class="ele-text-placeholder"
              >
                {{ expirationTimeText(record) }}
              </div>
              <div
                v-if="
                  expirationTime(record.expirationTime) > 0 &&
                  expirationTime(record.expirationTime) < 30
                "
                class="ele-text-warning"
              >
                {{ expirationTimeText(record) }}
              </div>
            </template>
          </template>
          <template v-if="column.key === 'version'">
            <span v-if="record.version == 10"> 免费版 </span>
            <span v-if="record.version == 20"> 商业版 </span>
            <span v-if="record.version == 30"> 永久授权 </span>
          </template>
          <template v-if="column.key === 'appUrl'">
            <DesktopOutlined
              @click="openNew(`https://${record.tenantId}.websoft.top`)"
            />
            <a-divider type="vertical" />
            <GlobalOutlined
              @click="openNew(`https://${record.tenantCode}.wsdns.cn`)"
            />
            <template v-if="record.h5Code">
              <a-divider type="vertical" />
              <a-popover>
                <template #content>
                  <div class="qrcode">
                    <ele-qr-code
                      :value="`https://${record.tenantCode}.wsdns.cn`"
                      :size="160"
                      level="M"
                      :margin="1"
                    />
                    <span>手机扫一扫</span>
                  </div>
                </template>
                <TabletOutlined />
              </a-popover>
            </template>
            <template v-if="record.mpWeixinCode">
              <a-divider type="vertical" />
              <a-popover>
                <template #content>
                  <div class="qrcode">
                    <ele-qr-code
                      :value="`微信小程序未配置`"
                      :size="160"
                      level="M"
                      :margin="1"
                    />
                    微信扫一扫
                  </div>
                </template>
                <WechatOutlined class="ele-text-success" />
              </a-popover>
            </template>
            <template v-if="record.mpAlipayCode">
              <a-divider type="vertical" />
              <a-popover>
                <template #content>
                  <div class="qrcode">
                    <ele-qr-code
                      :value="`支付宝小程序未配置`"
                      :size="160"
                      level="M"
                      :margin="1"
                    />
                    支付宝扫一扫
                  </div>
                </template>
                <a-button :size="`small`">
                  <AlipayOutlined class="ele-text-primary" />
                  支付宝
                </a-button>
              </a-popover>
            </template>
          </template>
          <template v-if="column.key === 'action'">
            <template v-if="record.deleted == 1">
              <a-popconfirm
                placement="topRight"
                title="确定从回收站放回原处吗？"
                @confirm="recovery(record)"
              >
                <a class="ele-text-success" style="font-weight: bold">恢复</a>
              </a-popconfirm>
            </template>
            <template v-if="record.deleted == 0">
              <a-popconfirm
                placement="topRight"
                title="确定要删除此租户吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </template>
            <template v-if="record.deleted == 1 && getUserId() == 3731">
              <a-divider type="vertical" />
              <a-popconfirm
                placement="topRight"
                title="确定要销毁该租户吗？此操作将永久删除且不可恢复"
                @confirm="destruction(record)"
              >
                <a class="ele-text-danger" style="font-weight: bold">销毁</a>
              </a-popconfirm>
            </template>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <CompanyEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </a-page-header>
</template>

<script lang="ts" setup>
  import { createVNode, ref, watch } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import {
    ExclamationCircleOutlined,
    IdcardOutlined,
    TabletOutlined,
    DesktopOutlined,
    GlobalOutlined
  } 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 Search from './components/search.vue';
  import CompanyEdit from './components/company-edit.vue';
  import {
    destructionCompany,
    removeBatchCompany,
    removeCompany,
    undeleteCompany
  } from '@/api/oa/company';
  import { Company, CompanyParam } from '@/api/system/company/model';
  import { toDateString } from 'ele-admin-pro';
  import { useRouter } from 'vue-router';

  const { currentRoute } = useRouter();
  import { pageCompanyAll } from '@/api/system/company';
  import {
    copyText,
    getMerchantId,
    getPageTitle,
    getUserId,
    openNew
  } from '@/utils/common';
  import { addUserCollection } from '@/api/system/user-collection';

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

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

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: '租户ID',
      fixed: 'left',
      dataIndex: 'tenantId',
      align: 'center',
      width: 90
    },
    {
      title: 'Logo',
      dataIndex: 'companyLogo',
      key: 'companyLogo',
      align: 'center',
      width: 180
    },
    {
      title: '租户名称',
      dataIndex: 'shortName',
      key: 'shortName'
    },
    {
      title: '版本',
      dataIndex: 'version',
      align: 'center',
      key: 'version'
    },
    {
      title: '到期时间',
      dataIndex: 'expirationTime',
      key: 'expirationTime',
      align: 'center',
      width: 160,
      sorter: true,
      customRender: ({ text }) => toDateString(text, 'yyyy-MM-dd')
    },
    {
      title: '操作',
      align: 'center',
      key: 'appUrl',
      width: 180
    },
    // {
    //   title: '状态',
    //   dataIndex: 'status',
    //   align: 'center',
    //   width: 120,
    //   key: 'status',
    //   customRender: ({ text }) => ['已上线', '开发中'][text]
    // }
  ]);

  // 表格选中数据
  const selection = ref<Company[]>([]);
  // 当前编辑数据
  const current = ref<Company | null>(null);
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 是否显示高级搜索
  const showAdvancedSearch = ref(false);
  // 页面标题
  const title = getPageTitle();

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    where.merchantId = getMerchantId();

    // 默认显示我的收藏
    if (where.version == undefined) {
      where.version = 99;
    }
    // 回收站
    if (where.version == 88) {
      where.version = undefined;
      where.deleted = 1;
    }
    // 查询全部
    if (where.version == 100) {
      where.version = undefined;
    }
    return pageCompanyAll({
      ...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 expirationTime = (dateTime) => {
    const now = new Date().getTime();
    const expiration = new Date(dateTime).getTime();
    const mss = expiration - now;
    let days = Math.floor(mss / (1000 * 60 * 60 * 24));
    let hours = Math.floor((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = Math.floor((mss % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = Math.round((mss % (1000 * 60)) / 1000);
    return days;
  };

  const expirationTimeText = (item: any) => {
    const now = new Date().getTime();
    const expiration = new Date(item.expirationTime).getTime();
    const mss = expiration - now;
    let days = Math.floor(mss / (1000 * 60 * 60 * 24));
    let hours = Math.floor((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = Math.floor((mss % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = Math.round((mss % (1000 * 60)) / 1000);
    if (item.version == 30) {
      return `永久授权`;
    }
    if (days < 0) {
      // item.status = 1;
      // updateCompany(item);
      return `(已过期${Math.abs(days)}天)`;
    }
    if (days < 30) {
      return `(剩余${days}天)`;
    }
  };

  /* 打开高级搜索 */
  const openAdvanced = () => {
    showAdvancedSearch.value = !showAdvancedSearch.value;
  };

  const onCode = (text: string) => {
    console.log(text);
  };

  const onCollection = (item: Company) => {
    addUserCollection({ tid: item.companyId }).then((msg) => {
      reload();
    });
  };

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

  /* 批量删除 */
  const removeBatch = () => {
    console.log(selection.value);
    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 recovery = (row: Company) => {
    undeleteCompany(row.companyId)
      .then((msg) => {
        message.success(msg);
        reload();
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  // 销毁租户
  const destruction = (row: Company) => {
    destructionCompany(row.tenantId)
      .then((msg) => {
        message.success(msg);
        reload();
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  watch(
    currentRoute,
    () => {
      reload();
    },
    { immediate: true }
  );
</script>

<script lang="ts">
  import * as MenuIcons from '@/layout/menu-icons';

  export default {
    name: 'CompanyIndex',
    components: MenuIcons
  };
</script>

<style lang="less" scoped>
  .tag-icon {
    padding-right: 6px;
  }

  .qrcode {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
</style>
