<template>
  <div>
    <div class="bg-white rounded-md mt-4 px-23px py-16px">
      <div class="flex justify-between pb-4 btn">
        <div>
          <el-button type="primary" @click="handleAdd">新增</el-button>
          <el-button type="primary" @click="exportAll">导出</el-button>
          <import-button url="/txwx/tskc_zj" @success="initData">
            <template #text>批量导入</template>
          </import-button>
          <el-button
            type="danger"
            @click="handleMulDelete"
            :disabled="multipleSelection.length == 0"
            >删除</el-button
          >
        </div>
        <div class="flex content-center">
          <el-input
            v-model="form_query.ttz_mc"
            class="w-40 mx-2"
            size="small"
            placeholder="请输搜索关键词"
            :suffix-icon="Search"
            clearable
          />
          <el-button type="primary" @click="initData">搜索</el-button>
        </div>
      </div>
      <el-table :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="ttz_id"
          label="章节ID"
          width="100"
        ></el-table-column>
        <el-table-column prop=" ttz_img" label="章节图片">
          <template #default="scope">
            <img
              style="width: 46px; height: 30px; display: block"
              :src="getFullUrl(scope.row.ttz_img)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="ttz_mc"
          label="章节名称"
          width="atuo"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="245">
          <template #default="scope">
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑 |</el-button
            >
            <el-button type="text" @click="handleDelete(scope.row.ttz_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end my-10">
        <el-pagination
          v-model:current-page="form_query.page"
          v-model:page-size="form_query.limit"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
          :background="true"
          @current-change="initData"
          :hide-on-single-page="false"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { user } from "@/store/index";
import { tskc_zj_list, tskc_zj_del } from "./api";
const router = useRouter();
let keyword = $ref("");
let total = $ref(10);
let form_query = $ref({
  page: 1,
  limit: 6,
  ttz_mc: "",
});

let tableData = $ref([]);
function initData() {
  const data = {
    ...form_query,
    ttk_id: parentId,
  };
  tskc_zj_list(data).then((res) => {
    if (res.code == "1") {
      tableData = res.data.page_data;
      total = res.data.total_rows * 1;
    }
  });
}
const parentId = router.currentRoute.value.params.id;
if (parentId) {
  initData();
}

function handleAdd() {
  router.push({ name: "tskc_chapter_add", params: { id: parentId } });
}

// 编辑
function handleEdit(item) {
  router.push({
    name: "tskc_chapter_edit",
    params: {
      zjId: item.ttz_id,
      id: parentId,
    },
  });
}
let multipleSelection = $ref([]);
let handleSelectionChange = (val) => {
  multipleSelection = [];
  val.forEach((i) => {
    multipleSelection.push(i.ttz_id);
  });
};
// 批量删除
function handleMulDelete() {
  ElMessageBox.confirm("确认删除这些数据?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let data = { ttz_id: multipleSelection };
      tskc_zj_del(data).then((res) => {
        if (res.code == 1) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          initData();
        } else {
          ElMessage({
            message: res.msg,
            type: "error",
          });
        }
      });
    })
    .catch(() => {});
}
//单个删除
function handleDelete(id) {
  ElMessageBox.confirm("确认删除?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let data = { ttz_id: id };
      tskc_zj_del(data).then((res) => {
        if (res.code == 1) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          initData();
        } else {
          ElMessage({
            message: res.msg,
            type: "error",
          });
        }
      });
    })
    .catch(() => {});
}

function exportAll() {
  let ids = multipleSelection.join(",");

  let link =
    window.GLOBAL_CONFIG.api +
    "/txwx/tskc_zj/index?api=xls&page=1&limit=10000&token=" +
    user.value.token +
    "&ttk_id=" +
    parentId +
    "&ttz_id=" +
    ids;
  console.log(link);
  window.open(link, "_blank");
}
</script>

<style lang="scss" scoped>
.search {
  height: 72px;
  line-height: 85px;
  padding: 0 29px;
}
.btn {
  border-bottom: 1px solid #e3e3e3;
}
</style>·