<template>
  <div class="ykkb-msList">
    <div class="searchDiv">
      <el-form ref="ruleFormRef" :inline="true" :model="ruleForm" class="demo-form-inline">
        <el-form-item label="名师姓名：" prop="ym_teacher_name">
          <el-input size="default" v-model="ruleForm.ym_teacher_name" placeholder="" />
        </el-form-item>
        <el-form-item label="时间：" prop="ym_date">
          <el-date-picker size="default" v-model="ruleForm.ym_date" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="年级：" prop="grade_id">
          <el-select v-model="ruleForm.grade_id" filterable placeholder="" size="default">
            <el-option v-for="(item, index) in gradeData" :key="index" :label="item.grade_name" :value="item.grade_id" />
          </el-select>
        </el-form-item>
        <el-form-item label="学科：" prop="subject_id">
          <el-select v-model="ruleForm.subject_id" filterable placeholder="" size="default">
            <el-option v-for="(item, index) in subjectData" :key="index" :label="item.subject_name" :value="item.subject_id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="default" type="primary" @click="initData">搜索</el-button>
          <el-button size="default" type="warning" @click="resetForm(ruleFormRef)">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="searchContent">
      <div class="searchBtn flex">
        <div class="flex">
          <el-button size="default" type="success" @click="handleDownload">导出</el-button>
        </div>
        <div class="flex">
          <img src="/images/print.png" alt="" />
          <el-input v-model="ruleForm.keyword" size="default" placeholder="请输入搜索的关键词" style="margin: 0 10px;" :suffix-icon="Search" />
          <el-button size="default" type="primary" @click="initData">搜索</el-button>
        </div>
      </div>
      <div class="tableList">
        <div class="tableDiv">
          <el-table ref="multipleTableRef" :data="tableData" height="100%" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="ym_teacher_name" width="" label="名师姓名" show-overflow-tooltip></el-table-column>
            <el-table-column width="" label="课程名称" show-overflow-tooltip>
              <template #default="{row}">
                <span style="color: #00A3FF;">{{row.ym_name}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="grade_name" width="" label="年级" show-overflow-tooltip></el-table-column>
            <el-table-column prop="subject_name" width="" label="学科" show-overflow-tooltip></el-table-column>
            <el-table-column prop="ym_date" label="时间" show-overflow-tooltip></el-table-column>
            <el-table-column prop="ym_sm_name" label="学校/机构名称" show-overflow-tooltip></el-table-column>
          </el-table>
        </div>
        <div class="paginationDiv">
          <el-pagination background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" :page-size="limit" :currentPage="currentPage"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { Search } from "@element-plus/icons-vue";
import { reactive, ref, onMounted } from "vue";
import { download } from "~/utils/request";
import { gradeList, subjectList, ykMainList } from "./api";

const ruleFormRef = ref(null);
const ruleForm = reactive({
  keyword: "",
  ym_teacher_name: "",
  grade_id: "",
  subject_id: "",
  ym_date: "",
});

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
  initData();
};

initSelectData();
const gradeData = ref([]);
const subjectData = ref([]);
async function initSelectData() {
  let data1 = await gradeList();
  gradeData.value = [{ grade_name: "全部", grade_id: "" }].concat(data1.page_data);

  let data2 = await subjectList();
  subjectData.value = [{ subject_name: "全部", subject_id: "" }].concat(data2.page_data);

  initData();
}

const tableData = ref([]);
const total = ref(0);
const limit = ref(10);
const currentPage = ref(1);
function handleCurrentChange(val) {
  currentPage.value = val;
  initData();
}

async function initData() {
  let obj = {
    limit: limit.value,
    page: currentPage.value,
    keyword: ruleForm.keyword,
    ym_teacher_name: ruleForm.ym_teacher_name,
    grade_id: ruleForm.grade_id,
    subject_id: ruleForm.subject_id,
    ym_date: ruleForm.ym_date
  };
  let data = await ykMainList(obj);
  total.value = parseInt(data.total_rows);
  tableData.value = data.page_data;
}

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

const handleDownload = () => {
  let arr = [];
  multipleSelection.value.map(value => {
    arr.push(value.ym_id);
  })
  let obj = {
    limit: limit.value,
    page: currentPage.value,
    keyword: ruleForm.keyword,
    ym_teacher_name: ruleForm.ym_teacher_name,
    grade_id: ruleForm.grade_id,
    subject_id: ruleForm.subject_id,
    ym_date: ruleForm.ym_date,
    checkedidstring: arr.join(",")
  };

  download("/yk/main/index", obj);
};

onMounted(() => {
  setTimeout(() => {
    let searchContent = document.querySelector(".searchContent");
    let offset = document.querySelector(".searchDiv");
    let parentObj = document.querySelector(".ykkb-msList");
    searchContent.style.height = parentObj.clientHeight - offset.clientHeight + "px";
    window.onresize = function () {
      let searchContent = document.querySelector(".searchContent");
      let parentObj = document.querySelector(".ykkb-msList");
      searchContent.style.height = parentObj.clientHeight - offset.clientHeight + "px";
    }
  }, 0)
})
</script>

<style lang="scss" scoped>
.ykkb-msList {
  height: 100%;
  .flex {
    display: flex;
    align-items: center;
  }
  .searchDiv {
    .el-form-item::v-deep {
      margin-bottom: 0;
      .el-form-item__label {
        padding: 0;
      }
    }
  }
  .searchContent {
    margin-top: 12px;
    height: calc(100% - 62px - 12px);
    background: #fff;
    border-radius: 6px;
    .searchBtn {
      justify-content: space-between;
      padding: 10px 0;
      border-bottom: 1px solid #e3e3e3;
      ::v-deep .el-button {
        height: 32px;
      }
    }
    .tableList {
      height: calc(100% - 52px - 10px);
      position: relative;
      .tableDiv {
        // background: #ddd;
        height: calc(100% - 42px);
        .el-button {
          padding: 0;
        }
        .split {
          color: #00a3ff;
          margin: 0 10px;
        }
      }
      .paginationDiv {
        position: absolute;
        bottom: -5px;
        padding: 3px;
        width: 100%;
        display: -webkit-box;
        -webkit-box-pack: end;
      }
    }
  }
}
</style>