<template>
  <div class="guns-layout">
    <div class="guns-layout-content" style="padding: 12px 0">
      <div class="guns-layout-content-header">
        <span>轮播图</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
                rowId="slideshowId"
                :isInit="false"
                ref="tableRef"
                url="/portalSlideshow/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 == 'slideshowTitle'">
                    <a @click="openAddEdit(record)">{{ record.slideshowTitle }}</a>
                  </template>
                  <template v-if="column.dataIndex == 'slideshowRedirectType'">
                    <span v-if="record.slideshowRedirectType == 5">无跳转</span>
                    <span v-if="record.slideshowRedirectType == 10">跳转到新闻</span>
                    <span v-if="record.slideshowRedirectType == 20">跳转到指定URL</span>
                  </template>
                  <template v-if="column.dataIndex == 'slideshowCoverFileId'">
                    <a-image width="40px" :src="filePreviewUrl + record.slideshowCoverFileId" />
                  </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>

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

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

const CarouselAddEdit = defineAsyncComponent(() => import('./components/carousel-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: 'slideshowTitle',
    title: '标题',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'slideshowCoverFileId',
    title: '轮播图图片',
    align: 'center',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'slideshowRedirectType',
    title: '跳转类型',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'slideshowNewsTitle',
    title: '关联文章',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'slideshowRedirectUrl',
    title: '外部URL',
    ellipsis: true,
    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 current = ref(null);
// 是否显示新增编辑弹框
const showAddEdit = ref(false);

onMounted(() => {
  if (props.designId) {
    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 CarouselApi.delete({ slideshowId: record.slideshowId });
      message.success(res.message);
      reload();
    }
  });
};
</script>

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