<template>
  <guns-split-layout width="292px">
    <NewTypeTree @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
                    rowId="newsId"
                    :isInit="false"
                    ref="tableRef"
                    url="/portalNews/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 == 'newsTitle'">
                        <a @click="openAddEdit(record)">{{ record.newsTitle }}</a>
                      </template>
                      <template v-if="column.dataIndex == 'newsStatus'">
                        <span v-if="record.newsStatus == 1" class="status-tag draft">草稿</span>
                        <span v-if="record.newsStatus == 2" class="status-tag">发布</span>
                      </template>
                      <template v-if="column.dataIndex == 'newsTopFlag'">
                        <span v-if="record.newsTopFlag == 'Y'">置顶</span>
                        <span v-if="record.newsTopFlag == 'N'">不置顶</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>

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

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

const NewTypeTree = defineAsyncComponent(() => import('./components/new-type-tree.vue'));
const NewAddEdit = defineAsyncComponent(() => import('./components/new-add-edit.vue'));

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

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'newsTitle',
    title: '新闻标题',
    ellipsis: true,
    width: 200,
    isShow: true
  },
  {
    dataIndex: 'newsStatus',
    title: '发布状态',
    align: 'center',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'showPublishUser',
    title: '新闻作者',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'showPublishTime',
    title: '发布时间',
    ellipsis: true,
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'newsTopFlag',
    title: '是否置顶',
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'newsSort',
    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.newsCategoryId = 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 NewApi.delete({ newsId: record.newsId });
      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: #f3f3f5;
  color: #60666b;
}
</style>
