<template>
  <a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
    <template #extra>
      <Extra/>
    </template>
    <a-spin :spinning="loading" class="page">
      <a-card :bordered="false" :body-style="{ padding: '16px' }">
        <ele-pro-table
          ref="tableRef"
          row-key="websiteId"
          :columns="columns"
          :datasource="datasource"
          :parse-data="parseData"
          :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 #headerCell="{ column }">
            <template v-if="column.key === 'version'">
              <a-tooltip title="授权版是已经付费的应用实例"> 当前版本
                <QuestionCircleOutlined/>
              </a-tooltip>
            </template>
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'websiteLogo'">
              <a-tooltip :title="record.tenantId">
                <a-avatar :src="record.websiteLogo"/>
              </a-tooltip>
            </template>
            <template v-if="column.key === 'websiteType'">
              <a-tag class="cursor-pointer" v-if="record.companyId > 0"
                     @click="openUrl(`https://${record.adminUrl}/login?loginPhone=${record.superAdminPhone}`)">
                <a-tooltip :title="`${record.developer || '超管登录'}`">
                  {{ record.websiteType }}
                </a-tooltip>
              </a-tag>
              <a-tag v-else>未绑定</a-tag>
            </template>
            <template v-if="column.key === 'domain'">
              <template v-if="record.domain">
                <a-tooltip title="访问PC版">
                  <DesktopOutlined class="text-xl" @click="openUrl(`${record.prefix}${record.domain}`)"/>
                </a-tooltip>
                <a-divider type="vertical"/>
                <a-tooltip title="访问移动版">
                  <MobileOutlined class="text-xl" @click="onShare(record)"/>
                </a-tooltip>
              </template>
            </template>
            <template v-if="column.key === 'type'">
              <a-tag v-if="record.type === 1" color="green">是</a-tag>
              <a-tag
                v-if="record.type === 0"
                @click="updateType(record)"
                class="cursor-pointer"
              >否
              </a-tag
              >
            </template>
            <template v-if="column.key === 'version'">
              <a-tag v-if="record.version === 10">免费版</a-tag>
              <a-tag color="blue" v-if="record.version === 20">授权版</a-tag>
              <a-tag color="cyan" v-if="record.version === 30">永久授权</a-tag>
            </template>
            <template v-if="column.key === 'status'">
              <a-tag v-if="record.running === 0" color="red">未开通</a-tag>
              <a-tag v-if="record.running === 1" color="green">运行中</a-tag>
              <a-tag v-if="record.running === 2" color="orange">维护中</a-tag>
              <a-tag v-if="record.running === 3" color="red">已关闭</a-tag>
              <a-tag v-if="record.running === 4" color="red">已欠费停机</a-tag>
              <a-tag v-if="record.running === 5" color="red">违规关停</a-tag>
            </template>
            <template v-if="column.key === 'comments'">
              <div class="text-gray-400 line-clamp-3">{{ record.comments }}</div>
            </template>
            <template v-if="column.key === 'expirationTime'">
              <div class="text-gray-400">{{ toDateString(record.createTime, 'YYYY-MM-dd') }}</div>
              <div class="text-gray-400">{{ toDateString(record.expirationTime, 'YYYY-MM-dd') }}</div>
              <!--              <a-tag v-if="record.status < 0 && record.soon < 0" color="red">已过期</a-tag>-->
              <!--              <a-tag v-if="record.status > 0 && record.soon < 0" color="orange">即将过期</a-tag>-->
            </template>
            <template v-if="column.key === 'action'">
              <a @click="openEdit(record)">
                编辑
              </a>
              <a-popconfirm
                v-if="record.version == 10"
                title="确定要删除此记录吗？"
                @confirm="remove(record)"
              >
                <a-divider type="vertical"/>
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </template>
          </template>
          <template #emptyText>
            <a-empty
              :image-style="{
                height: '240px',
              }"
            >
              <template #description>
                <span>
                  欢迎使用本产品，请先创建站点并完成初始化。
                </span>
              </template>
              <div class="pb-10">
                <a-button type="primary" @click="openEdit">立即创建</a-button>
              </div>
            </a-empty>
          </template>
        </ele-pro-table>
      </a-card>

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

      <!-- 二维码 -->
      <Qrcode v-model:visible="showQrcode" :data="`${qrcode}`" @done="hideShare"/>

    </a-spin>
  </a-page-header>
</template>

<script lang="ts" setup>
import {createVNode, ref} from 'vue';
import {message, Modal} from 'ant-design-vue';
import {
  ExclamationCircleOutlined,
  DesktopOutlined,
  QuestionCircleOutlined,
  MobileOutlined
} 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 Qrcode from '@/components/QrCode/index.vue';
import {
  removeBatchCmsWebsite,
  updateCmsWebsite
} from '@/api/cms/cmsWebsite';
import type {CmsWebsite, CmsWebsiteParam} from '@/api/cms/cmsWebsite/model';
import {getPageTitle, openUrl} from '@/utils/common';
import {PageResult} from '@/api';
import {pageOrderGoods} from "@/api/system/orderGoods";
import {OrderGoods} from "@/api/system/orderGoods/model";
import WebsiteEdit from '@/views/cms/cmsWebsite-10257/components/websiteEdit.vue';
import Extra from "@/views/cms/cmsWebsite-10257/components/Extra.vue";
import Search from "@/views/cms/cmsWebsite-10257/components/search.vue";
import {getAuthorizedDomain} from "@/api/cms/cmsDomain";
import {pageCmsWebsiteAll, updateCmsWebsiteAll} from "@/api/cms/cmsWebsiteAll";

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

// 表格选中数据
const selection = ref<CmsWebsite[]>([]);
// 当前编辑数据
const current = ref<CmsWebsite | null>(null);
// 是否显示编辑弹窗
const showEdit = ref(false);
// 是否显示批量移动弹窗
const showMove = ref(false);
// 是否显示二维码
const showQrcode = ref(false);
// 二维码内容
const qrcode = ref();
// 加载状态
const loading = ref(true);
// 当前网站项目
const website = ref<CmsWebsite>();
// 已购买且未安装的网站
const orderGoods = ref<OrderGoods>();
// 默认域名
const domain = ref<string>();

// 表格数据源
const datasource: DatasourceFunction = ({
                                          page,
                                          limit,
                                          where,
                                          orders
                                        }) => {
  where.templateId = 10257;
  where.showAdminPhone = true;
  return pageCmsWebsiteAll({
    ...where,
    ...orders,
    page,
    limit
  });
};

// 表格列配置
const columns = ref<ColumnItem[]>([
  {
    title: 'LOGO',
    dataIndex: 'websiteLogo',
    key: 'websiteLogo',
    align: 'center',
    width: 120
  },
  {
    title: '名称',
    dataIndex: 'websiteName',
    key: 'websiteName'
  },
  {
    title: '产品',
    dataIndex: 'websiteType',
    key: 'websiteType',
    align: 'center',
    width: 180
  },
  {
    title: '网站描述',
    dataIndex: 'comments',
    key: 'comments',
    hideInTable: true
  },
  {
    title: '访问域名',
    dataIndex: 'domain',
    key: 'domain',
    align: 'center'
  },
  {
    title: '当前版本',
    dataIndex: 'version',
    key: 'version',
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'expirationTime',
    key: 'expirationTime',
    align: 'center',
    sorter: true,
    ellipsis: true,
    customRender: ({text}) => toDateString(text, 'yyyy-MM-dd')
  },
  {
    title: '操作',
    key: 'action',
    width: 180,
    fixed: 'right',
    align: 'center',
    hideInSetting: true
  }
]);

// 整理数据
const parseData = (data: PageResult<CmsWebsite>) => {
  if (data?.count > 0) {
    website.value = data.list[0];
    localStorage.setItem('WebsiteId', `${website.value?.websiteId}`);
  }
  return data;
};

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

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

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

const updateType = (row: CmsWebsite) => {
  updateCmsWebsite(row).then((msg) => {
    message.success(msg);
    reload();
  });
};

const onShare = (row?: CmsWebsite) => {
  qrcode.value = `https://${row?.websiteCode}.wsdns.cn`
  if (localStorage.getItem('Domain')) {
    qrcode.value = `https://${localStorage.getItem('Domain')}`
  }
  showQrcode.value = true;
}

const hideShare = () => {
  showQrcode.value = false;
}

/* 删除单个 */
const remove = (row: CmsWebsite) => {
  const hide = message.loading('请求中..', 0);
  updateCmsWebsiteAll({
    websiteId: row.websiteId,
    deleted: 1
  })
    .then(() => {
      hide();
      message.success('删除成功');
      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);
      removeBatchCmsWebsite(selection.value.map((d) => d.websiteId))
        .then((msg) => {
          hide();
          message.success(msg);
          reload();
        })
        .catch((e) => {
          hide();
          message.error(e.message);
        });
    }
  });
};

/* 查询 */
const query = () => {
  // 查询订单
  pageOrderGoods({itemId: 10297, payStatus: true, orderStatus: 0}).then(res => {
    loading.value = false;
    if (res?.count && res?.count > 0) {
      orderGoods.value = res?.list[0];
    }
  })
  // 查询授权域名
  if (localStorage.getItem('Domain')) {
    getAuthorizedDomain().then((data) => {
      if (data) {
        domain.value = data.domain;
      }
    })
  }
};

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

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

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