<template>
  <guns-split-layout width="292px">
    <AppTypeTree @treeSelect="treeSelect" :designId="props.designId" />
    <template #content>
      <div class="guns-layout">
        <div class="guns-layout-content">
          <div class="guns-layout-content-header">
            <span>{{ currentCategory?.categoryName || '应用链接' }}</span>
            <a-space :size="16">
              <a-button type="primary" class="border-radius" @click="openAddEdit()"><plus-outlined />新建</a-button>
            </a-space>
          </div>
          <div class="guns-layout-content-application">
            <div class="content-mian">
              <div class="content-mian-body">
                <div class="table-content">
                  <common-table
                    :columns="columns"
                    :where="where"
                    :showToolTotal="false"
                    :rowSelection="false"
                    showTableTool
                    :isInit="false"
                    rowId="appLinkId"
                    ref="tableRef"
                    url="/portalAppLink/page"
                  >
                    <template #toolLeft>
                      <a-input
                        v-model:value="where.searchText"
                        placeholder="请输入关键字"
                        @pressEnter="reload"
                        class="search-input"
                        :bordered="false"
                      >
                        <template #prefix>
                          <icon-font iconClass="icon-opt-search"></icon-font>
                        </template>
                      </a-input>
                    </template>
                    <template #bodyCell="{ column, record }">
                      <template v-if="column.dataIndex == 'linkName'">
                        <a @click="openAddEdit(record)">{{ record.linkName }}</a>
                      </template>
                      <template v-if="column.dataIndex == 'linkIconFileId'">
                        <a-image width="40px" :src="filePreviewUrl + record.linkIconFileId" />
                      </template>
                      <template v-if="column.dataIndex == 'redirectType'">
                        <span v-if="record.redirectType == 1" class="status-tag draft">内部路由</span>
                        <span v-if="record.redirectType == 2" class="status-tag">外部链接</span>
                      </template>
                      <template v-if="column.dataIndex == 'openType'">
                        <span v-if="record.openType == 1" class="status-tag draft">本页面打开</span>
                        <span v-if="record.openType == 2" class="status-tag">新标签页打开</span>
                      </template>
                      <template v-if="column.key == 'action'">
                        <a-space :size="16">
                          <icon-font
                            iconClass="icon-opt-bianji"
                            font-size="24px"
                            title="编辑"
                            color="#60666b"
                            @click="openAddEdit(record)"
                          ></icon-font>
                          <icon-font
                            iconClass="icon-opt-shanchu"
                            font-size="24px"
                            title="删除"
                            color="#60666b"
                            @click="remove(record)"
                          ></icon-font>
                        </a-space>
                      </template>
                    </template>
                  </common-table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
  </guns-split-layout>

  <!-- 新增编辑弹框 -->
  <AppAddEdit
    v-model:visible="showAddEdit"
    v-if="showAddEdit"
    :currentCategory="currentCategory"
    :designId="props.designId"
    :data="current"
    @done="reload"
  />
</template>

<script setup>
import { AppApi } from './api/AppApi';
import { API_BASE_PREFIX } from '@/config/setting';
import { ref, createVNode, onMounted, defineAsyncComponent } from 'vue';
import { message, Modal } from 'ant-design-vue/es';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';

const AppTypeTree = defineAsyncComponent(() => import('./components/app-type-tree.vue'));
const AppAddEdit = defineAsyncComponent(() => import('./components/app-add-edit.vue'));

const props = defineProps({
  designId: String
});

const filePreviewUrl = ref(`${API_BASE_PREFIX}/sysFileInfo/public/preview?fileId=`);

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'linkName',
    title: '链接名称',
    ellipsis: true,
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'linkIconFileId',
    title: '链接图标',
    align: 'center',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'linkCategoryName',
    title: '分类',
    align: 'center',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'redirectType',
    title: '跳转类型',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'redirectUrl',
    title: '跳转地址',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'openType',
    title: '打开方式',
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'fldSort',
    title: '排序',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'createTime',
    title: '创建时间',
    width: 150,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 100,
    isShow: true
  }
]);
// ref
const tableRef = ref(null);

// 搜索条件
const where = ref({
  designId: props.designId,
  searchText: ''
});
const currentCategory = ref(null);
// 当前行数据
const current = ref(null);
// 是否显示新增编辑弹框
const showAddEdit = ref(false);

onMounted(() => {});

const treeSelect = node => {
  currentCategory.value = node;
  where.value.linkCategoryId = node?.categoryId;
  reload();
};

const reload = () => {
  tableRef.value.reload();
};

// 新增编辑点击
const openAddEdit = record => {
  current.value = record;
  showAddEdit.value = true;
};

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的应用链接吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await AppApi.delete({ appLinkId: record.appLinkId });
      message.success(res.message);
      reload();
    }
  });
};
</script>

<style scoped lang="less">
.status-tag {
  padding: 2px 8px;
  border-radius: 4px;
  background: #e9ffe3;
  color: #00b42a;
  font-size: 14px;
}
.draft {
  background: #fffbe6;
  color: #c18000;
}
</style>
