<template>
  <div class="jtjyChapterContent">
    <div class="resultDiv bg-white p-4 rounded-6px">
      <div class="operateDiv flex justify-between">
        <div>
          <el-button type="primary" @click="linkTo('jtjy_addChapter')">新增</el-button>
          <el-button type="primary" @click="handleExport">导出</el-button>
          <import-button url="/txwx/jtjy_zj" :d="{tjk_id: parentId}" @success="initData">
            <template #text>批量导入</template>
          </import-button>
          <el-button type="danger" @click="handleDelete()">删除</el-button>
        </div>
        <div class="flex items-center space-x-4">
          <el-input v-model="searchForm.keyword" placeholder="请输入搜索的关键词" size="small" :suffix-icon="Search" />
          <el-button type="primary" @click="initData">搜索</el-button>
        </div>
      </div>
      <div class="tableList">
        <div class="tableDiv my-20px">
          <el-table :data="tableData" size="large" height="100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="tjz_id" label="章节ID" show-overflow-tooltip></el-table-column>
            <el-table-column prop="id" label="章节图片">
              <template #default="{ row }">
                <img v-if="row.tjz_img" :src="getFullUrl(row.tjz_img)" alt="" />
              </template>
            </el-table-column>
            <el-table-column prop="tjz_mc" label="章节名称" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" width="150">
              <template #default="{ row }">
                <el-button type="primary" text @click="linkTo('jtjy_editChapter', row.tjz_id)">编辑</el-button>
                <el-divider direction="vertical" />
                <el-button type="primary" text @click="handleDelete(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="flex justify-end">
          <el-pagination v-model:current-page="currentPage" v-model:page-size="limit" :page-sizes="[10, 20, 50, 100]" :pager-count="5" :small="true" layout="total,sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getFullUrl } from '~/utils/helper';
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const router = useRouter();
const parentId = router.currentRoute.value.params.id;

const searchForm = ref({
  keyword: ""
})

watch(() => searchForm.value, () => {
  initData();
}, { deep: true })

const total = ref(100)
const limit = ref(10)
const currentPage = ref(1);
const tableData = ref([]);

if (parentId) {
  initData();
}
async function initData() {
  let data = {
    page: currentPage.value,
    limit: limit.value,
    tjk_id: parentId
  };
  let res = await request({ url: "/txwx/jtjy_zj/index", data: Object.assign(data, searchForm.value) });
  if (res.code == 1) {
    tableData.value = res.data.page_data;
    total.value = Number(res.data.total_rows);
  }
}

function handleSizeChange(val) {
  limit.value = val;
  currentPage.value = 1;
  initData();
}

function handleCurrentChange(val) {
  currentPage.value = val;
  initData();
}

const multiSelection = ref([]);
const handleSelectionChange = (val) => {
  multiSelection.value = val;
}

const handleDelete = (row) => {
  if (!row && multiSelection.value.length == 0) {
    ElMessage.error("请先选择数据！");
    return
  }
  ElMessageBox.confirm('确定要删除该数据吗？', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: "warning",
  })
    .then(async () => {
      let tjz_id;
      if (row) {
        tjz_id = row.tjz_id;
      } else {
        tjz_id = [];
        multiSelection.value.map(item => {
          tjz_id.push(item.tjz_id);
        })
      }
      let res = await request({ url: "/txwx/jtjy_zj/delete", data: { tjz_id: tjz_id } });
      if (res.code == 1) {
        ElMessage.success("删除成功！");
        initData();
      }
    })
    .catch(() => { })
}

const linkTo = (name, id) => {
  if (id == undefined) {
    router.push({ name, params: { kcId: parentId } });
  } else {
    router.push({ name, params: { kcId: parentId, id } });
  }
};


const handleExport = () => {
  let arr = [];
  multiSelection.value.map((item) => {
    arr.push(item.tjz_id);
  });
  let obj = {
    limit: "99999",
    tjk_id: parentId,
    tjz_id: arr.join(",")
  };
  download("/txwx/jtjy_zj/index", obj);
}
</script>

<style lang="scss" scoped>
@use "~/styles/table.scss";
@use "~/styles/pagination.scss";
.jtjyChapterContent {
  .resultDiv {
    .tableList {
      height: calc(100% - 32px);
      .tableDiv {
        height: calc(100% - 32px - 50px);
        img {
          width: 58px;
          height: 36px;
        }
      }
    }
  }
}
</style>