<template>
  <div>
    <div class="search flex justify-between bg-white rounded-md">
      <el-form :inline="true" class="demo-form-inline" size="default">
        <el-form-item>
          <el-select
            v-model="form_query.ttk_zrjgmc"
            filterable
            clearable
            placeholder="全部所属机构"
            @change="initData"
          >
            <el-option
              v-for="(item, index) in tableSchoolData"
              :key="index"
              :label="item.sm_name"
              :value="item.sm_name"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="initData"> 查询 </el-button>
      </div>
    </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_kc" @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.keyword"
            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="ttk_id"
          label="课程ID"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="ttk_mc"
          label="课程名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="ttk_tag" label="标签"></el-table-column>
        <el-table-column prop="ttk_kcgjz" label="课程关键字"></el-table-column>
        <el-table-column prop="ttk_zrjsmc" label="责任教师 "></el-table-column>
        <el-table-column prop="ttk_zrjgmc" label="所属机构"></el-table-column>
        <el-table-column
          prop="ttk_sort"
          label="课程排序"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="ttk_lll"
          label="浏览量"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="ttk_dzl"
          label="点赞量"
          width="150"
        ></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.ttk_id)"
              >删除 |</el-button
            >
            <el-button type="text" @click="handleChapter(scope.row)"
              >课程章节 |</el-button
            >
            <el-button type="text" @click="handleTeam(scope.row)"
              >团队
            </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 { school_list, tskc_list, tskc_del } from "./api";
import { user } from "@/store/index";
const router = useRouter();
//学校列表
let tableSchoolData = $ref([]);
function initSchoolData() {
  school_list().then((res) => {
    if (res.code === "1") {
      tableSchoolData = res.data.page_data;
    }
  });
}
initSchoolData();
let total = $ref(10);
let form_query = $ref({
  page: 1,
  limit: 6,
  keyword: "",
  ttk_zrjgmc: "",
});

let tableData = $ref([]);
function initData() {
  const data = {
    ...form_query,
  };
  tskc_list(data).then((res) => {
    if (res.code == "1") {
      tableData = res.data.page_data;
      total = res.data.total_rows * 1;
    }
  });
}
initData();
let keyword = $ref("");

let multipleSelection = $ref([]);
let handleSelectionChange = (val) => {
  multipleSelection = [];
  val.forEach((i) => {
    multipleSelection.push(i.ttk_id);
  });
};

function exportAll() {
  let ids = multipleSelection.join(",");
  let link =
    window.GLOBAL_CONFIG.api +
    "/txwx/tskc_kc/index?api=xls&page=1&limit=10000&token=" +
    user.value.token +
    "&ttk_id=" +
    ids;
  console.log(link);
  window.open(link, "_blank");
}
function handleAdd() {
  router.push({ name: "tskc_add" });
}
// 编辑
function handleEdit(item) {
  router.push({
    name: "tskc_edit",
    params: {
      id: item.ttk_id,
    },
  });
}
//课程章节
function handleChapter(item) {
  router.push({
    name: "tskc_chapter",
    params: {
      id: item.ttk_id,
    },
  });
}
//团队
function handleTeam(item) {
  router.push({
    name: "tskc_team",
    params: {
      id: item.ttk_id,
    },
  });
}
// 批量删除
function handleMulDelete() {
  ElMessageBox.confirm("确认删除这些数据?", "", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let data = { ttk_id: multipleSelection };
      tskc_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 = { ttk_id: id };
      tskc_del(data).then((res) => {
        if (res.code == 1) {
          ElMessage({
            message: "删除成功",
            type: "success",
          });
          initData();
        } else {
          ElMessage({
            message: res.msg,
            type: "error",
          });
        }
      });
    })
    .catch(() => {});
}
</script>

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