<template>
  <div>
    <n-card>
      <headSearch :info="search" @search="handleSearch" @reset="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        :scroll-x="1700"
        size="large"
        @update:checked-row-keys="handleRowCheck"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleModal"> 新建 </n-button>
          <n-popconfirm
            negative-text="取消"
            positive-text="确认"
            @positive-click="handleRowDelete"
          >
            <template #trigger>
              <n-button type="primary">删除</n-button>
            </template>
            确定删除选中的数据吗?
          </n-popconfirm>
        </template>
      </data-table>
    </n-card>
  </div>
  <!-- 新增、编辑弹窗 -->
  <NoticeModal v-if="modalShow" v-model:visible="modalShow" :data="rowData" :type="modalType" @reload="handleSearch" />
</template>

<script>
import search from './tools/search.js'
import table from './tools/table.js'
import headSearch from '@/components/Search/index.vue'
import dataTable from '@/components/DataTable/index.vue'
import NoticeModal from './components/NoticeModal.vue'
import { fetchNoticeList } from '@/api/setting/notice.js'
import { ref, unref, toRefs, reactive, onUnmounted } from 'vue'

export default {
  name: 'NotciePage',
  components: { dataTable, NoticeModal, headSearch },
  setup() {
    const data = reactive({
      ...toRefs(table),
      search
    })

    const loadDataTable = async(res) => {
      const _params = {
        ...unref(data.searchParams),
        ...res
      }
      return await fetchNoticeList(_params)
    }

    // 新增
    function handleModal() {
      data.rowData = null
      data.modalType = 'create'
      data.modalShow = true
    }

    const selectedIds = ref([])
    function handleRowCheck(rowKeys) {
      selectedIds.value = rowKeys
    }

    // 批量删除
    function handleRowDelete() {
      if (selectedIds.value.length) {
        data.deleteData(selectedIds.value)
      } else {
        $message.warning('请至少选中一条数据')
      }
    }

    onUnmounted(() => {
      data.searchParams = null
    })

    return {
      ...toRefs(data),
      loadDataTable,
      handleModal,
      handleRowCheck,
      handleRowDelete
    }
  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
