<!-- 公开年报管理 -->
<template>
  <div class="pricemoitorrecord">
    <a-card :bordered="false">
      <a-space class="search">
        <div class="silt-title">
          <span style="width: 70px; margin-right: 8px; text-align: right; display: block; line-height: 30px">{{
            '标题'
          }}</span>
          <a-input style="width: 220px" v-model:value="SearchFormDel.title" placeholder="请输入" />
        </div>
        <div class="btnBox">
          <a-button type="primary" class="primarySele" @click="Search"> 查询 </a-button>
          <a-button class="primarySele snowy-buttom-left" @click="onReset"> 重置 </a-button>
          <a-button type="primary" class="primarySele" @click="onView('add', {})"> 新增 </a-button>
          <a-button class="btnl colro primarySele" type="primary" :disabled="!hasSelected" :loading="state.loading"
            @click="start">
            批量操作(启用/禁用)
          </a-button>
        </div>
      </a-space>
    </a-card>

    <div class="list" style="margin-top: 10px">
      <a-table :data-source="tableData" :pagination="pagination" :columns="tableColumns" :row-key="(record) => record"
        :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'status'">
            <div class="switch_del">
              <a-switch v-if="record.status !== 1" :checked="record.status == 2 ? true : false" :checked-children="'启用'"
                :un-checked-children="'禁用'" @change="handleSwitchChange(record)" size="default" />
              <a-switch v-if="record.status == 1" :checked="record.status == 1 ? false : true" :checked-children="'启用'"
                :un-checked-children="'保存'" @change="handleSwitchChange(record)" size="default" />
            </div>
          </template>
          <template v-if="column.key === '操作'">
            <a-button type="link" @click="onView('view', record)">查看</a-button>
            <a-button type="link" @click="onView('edit', record)">编辑</a-button>
            <a-popconfirm title="确定要删除吗？" @confirm="onView('del', record)">
              <a-button type="link">删除</a-button>
            </a-popconfirm>
          </template>
        </template>
      </a-table>
    </div>
    <Form ref="formRef" @successful="tableRef.refresh()" @change="change" />
  </div>
</template>
<script setup>
import business from '@/api/policy.info/publicAnnualReportApi.js'
import { listPagination } from '@/hook/listlistPagination.js'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { defineComponent } from 'vue'
import { message } from 'ant-design-vue'
import Form from './form.vue'
const router = useRouter()

const tableColumns = [
  {
    title: '序号',
    dataIndex: 'INDEX',
    align: 'center',
    customRender: ({ text, record, index }) => {
      return `${index + 1}`
    }
  },
  { title: '标题', align: 'center', dataIndex: 'title' },
  { title: '来源', align: 'center', dataIndex: 'source' },
  { title: '发布时间', align: 'center', dataIndex: 'publishTime' },
  { title: '状态', align: 'center', dataIndex: 'status' },
  { title: '操作', align: 'center', key: '操作' }
]

//共享 //状态的禁用和启用的开关并调用接口
const handleSwitchChange = async (v) => {
  if (v.status == 1) {
    const res = await business.reportType([{ id: v.id, status: 2 }])
  } else if (v.status == 2) {
    const res = await business.reportType([{ id: v.id, status: 3 }])
  } else if (v.status == 3) {
    const res = await business.reportType([{ id: v.id, status: 2 }])
  }
  await onSearch()
}
// 初始数据存放
const state = reactive({
  selectedRowKeys: [],
  loading: false
})
// 判断批量操作启用禁用的disabled属性
const hasSelected = computed(() => state.selectedRowKeys.length > 0)
// 批量操作的启用和禁用
const start = async () => {
  state.loading = true
  const res = await business.reportType(del.value)
  setTimeout(() => {
    state.selectedRowKeys = []
    state.loading = false
    onSearch()
  }, 300)
}
const del = ref([])
const onSelectChange = (selectedRowKeys) => {
  state.selectedRowKeys = []
  del.value = []
  state.selectedRowKeys = selectedRowKeys
  del.value = state.selectedRowKeys.map((item) => {
    return {
      id: item.id,
      status: item.status === 1 ? 2 : item.status === 2 ? 3 : 2
    }
  })
  console.log(del.value)
}
// 封装传递的数据
const { DEl, change, tableData, onSearch, handlePageChange, pagination, Search, onReset, SearchFormDel } =
  listPagination({ title: '' }, business.reportPage, business.reportDel, {
    sortOrder: 'ASCEND'
  })

// 查看表单数据
const formRef = ref(null)
// 新增 查看 编辑 删除 等功能按钮
const onView = (record, row) => {
  switch (record) {
    case 'add':
      formRef.value.onOpen(record, true, false)
      break
    case 'del':
      DEl(row)
      break
    case 'view':
      formRef.value.onOpen(record, true, true, row)
      break
    case 'edit':
      formRef.value.onOpen(record, true, false, row)
      break
    default:
      break
  }
}
</script>
<style lang="less" scoped>
.search {
  padding-top: 20px;
  // padding-left: 50px;
  margin-bottom: 32px;

  .btnBox {
    width: 100%;
    display: inline-block;
    display: flex;
    justify-content: space-around;
    align-items: center;

    .primarySele {
      margin-left: 20px;
      margin-right: 20px;
    }
  }
}

.silt-title {
  display: flex;
}

.switch_del {
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>