<template>
  <div class="jtjyContent">
    <div class="searchContent bg-white p-4 pb-0 my-4 rounded-6px flex justify-between">
      <el-form ref="ruleFormRef" :inline="true" :model="searchForm" size="small">
        <el-form-item>
          <remote-select placeholder="全部所属机构" v-model="searchForm.tjk_zrjg_id" url="/school/main/index" :fields="['id', 'sm_name']"></remote-select>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="initData">查询</el-button>
    </div>
    <div class="resultDiv bg-white p-4 rounded-6px">
      <div class="operateDiv flex justify-between">
        <div>
          <el-button type="primary" @click="linkTo('jtjy_add')">新增</el-button>
          <el-button type="primary" @click="handleExport">导出</el-button>
          <import-button url="/txwx/jtjy_kc" @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 v-for="(item, index) in columnData" :key="index" :prop="item.key" :label="item.name" :width="item.width" :min-width="item.minWidth" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" width="280">
              <template #default="{ row }">
                <el-button type="primary" text @click="linkTo('jtjy_edit', row.tjk_id)">编辑</el-button>
                <el-divider direction="vertical" />
                <el-button type="primary" text @click="handleDelete(row)">删除</el-button>
                <el-divider direction="vertical" />
                <el-button type="primary" text @click="linkTo('jtjy_chapter', row.tjk_id)">课程章节</el-button>
                <el-divider direction="vertical" />
                <el-button type="primary" text @click="linkTo('jtjy_team', row.tjk_id)">团队</el-button>
                <el-divider direction="vertical" />
                <el-button type="primary" text @click="linkTo('jtjy_detail', row.tjk_id)">详情</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 { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const router = useRouter();

const searchForm = ref({});

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

const total = ref(0)
const limit = ref(10)
const currentPage = ref(1);
const columnData = ref([
  {
    name: "课程ID",
    key: "tjk_id",
  },
  {
    name: "课程名称",
    key: "tjk_mc",
    minWidth: 180
  },
  {
    name: "年级",
    key: "tjzn_name",
  },
  {
    name: "学科",
    key: "tjzx_name",
  },
  {
    name: "责任教师",
    key: "tjk_zrjsmc",
  },
  {
    name: "所属机构",
    key: "tjk_zrjgmc",
  },
  {
    name: "报名量",
    key: "tjk_bml",
  },
  {
    name: "浏览量",
    key: "tjk_lll",
  },
]);

const tableData = ref([]);
initData();
async function initData() {
  let data = {
    page: currentPage.value,
    limit: limit.value,
  };
  let res = await request({ url: "/txwx/jtjy_kc/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 linkTo = (name, id, title) => {
  if (id == undefined) {
    router.push({ name });
  } else {
    router.push({ name, params: { id } });
  }
};

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


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

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