<template>
  <div class="ykkb-xxList">
    <div class="searchDiv">
      <el-form ref="ruleFormRef" :inline="true" :model="ruleForm" class="demo-form-inline">
        <el-form-item label="学校：" prop="sm_id">
          <el-select v-model="ruleForm.sm_id" filterable placeholder=" " size="default">
            <el-option v-for="item in schoolData" :key="item.id" :label="item.sm_name" :value="item.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="searchTitle flex">
        <div>今天{{initCurrent()}}</div>
        <el-button size="default" type="success" @click="handleDownload(1)">导出</el-button>
      </div>
      <el-calendar ref="calendar" v-model="month">
        <template #header="{ date }">
          <el-date-picker v-model="month" type="month" size="default" placeholder="" />
          <div class="flex">
            <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>
            <el-button type="info" size="default" :icon="ArrowLeft" plain @click="selectDate('prev-month')" />
            <el-button type="info" size="default" :icon="ArrowRight" plain @click="selectDate('next-month')" />
          </div>
        </template>
        <template #dateCell="{ data }">
          {{initCurrentDay(data.day, 1)}}
          <div class="box" v-if="initCurrentDay(data.day, 2)!=0">
            <div>{{initCurrentDay(data.day, 2)}}节课</div>
            <span style="border-bottom: 1px solid;cursor: pointer;" @click="lookDetail(data.day)">去看看</span>
            <!-- <el-link type="primary" @click="lookDetail(data)">去看看</el-link> -->
          </div>
        </template>
      </el-calendar>
    </div>

    <el-dialog width="80%" v-model="dialogVisible" title="查看" custom-class="commonDialog" :close-on-click-modal="false">
      <div class="searchBtn flex">
        <div class="flex">
          <el-button size="default" type="success" @click="handleDownload(2)">导出</el-button>
        </div>
        <div class="flex">
          <img src="/images/print.png" alt="" />
          <el-input v-model="searchKey" size="default" placeholder="请输入搜索的关键词" style="margin: 0 10px;" :suffix-icon="Search" />
          <el-button size="default" type="primary" @click="initTableData">搜索</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>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search, ArrowLeft, ArrowRight } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { schoolList, ykMainList } from "./api";
import { download } from "~/utils/request";
import { dealWithMonth } from "~/utils/common-util.js";

const ruleFormRef = ref(null);
const ruleForm = reactive({
  sm_id: "",
  keyword: ""
});

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

initSelectData();
const schoolData = ref([]);
async function initSelectData() {
  let data5 = await schoolList();
  schoolData.value = [{ sm_name: "全部", id: "" }].concat(data5.page_data);
}

const totalMonthData = ref([]);
const month = ref(new Date());
initData();
async function initData() {
  let obj = {
    sm_id: ruleForm.sm_id,
    keyword: ruleForm.keyword,
    month: dealWithMonth(month.value),
    school_kb: 1,
    site: "tyyx"
  };
  let data = await ykMainList(obj);
  totalMonthData.value = data.page_data;
}

function initCurrent(param) {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth();
  month = month + 1;
  let day = date.getDate();
  return year + '年' + month + '月' + day + '日';
}

const calendar = ref()
const selectDate = (val) => {
  calendar.value.selectDate(val)
  initData();
}

function initCurrentDay(date, type) {
  let arr = date.split("-");
  if (type == 1) {
    return Number(arr[2]);
  } else {
    let res = 0;
    totalMonthData.value.map(item => {
      for (let key in item) {
        if (date == key) {
          res = item[key];
        }
      }
    })
    return res;
  }
}

const tableData = ref([]);
const total = ref(0);
const limit = ref(10);
const currentPage = ref(1);
const dialogVisible = ref(false);
const searchKey = ref("");
const currentDay = ref("");
function lookDetail(data) {
  currentDay.value = data;
  initTableData();
}

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

async function initTableData() {
  let obj = {
    limit: limit.value,
    page: currentPage.value,
    keyword: searchKey.value,
    ym_date: currentDay.value
  };
  let data = await ykMainList(obj);
  total.value = parseInt(data.total_rows);
  tableData.value = data.page_data;
  dialogVisible.value = true;
}

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

const handleDownload = (type) => {
  let obj = {};
  if (type == 1) {
    obj = {
      sm_id: ruleForm.sm_id,
      keyword: ruleForm.keyword,
      month: dealWithMonth(month.value),
      school_kb: 1,
      site: "tyyx"
    }
  } else if (type == 2) {
    let arr = [];
    multipleSelection.value.map(value => {
      arr.push(value.ym_id);
    })
    obj = {
      limit: limit.value,
      page: currentPage.value,
      keyword: searchKey.value,
      ym_date: currentDay.value,
      checkedidstring: arr.join(",")
    };
  }
  download("/yk/main/index", obj);
};
</script>

<style lang="scss" scoped>
@import "~/styles/dialog.scss";
.ykkb-xxList {
  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;
    .searchTitle {
      justify-content: space-between;
      border-bottom: 1px solid #e4e6e8;
      padding-bottom: 12px;
    }
    .el-calendar::v-deep {
      .el-calendar-day {
        height: 100px;
        font-size: 12px;
        text-align: right;
      }
      .box {
        margin-top: 8px;
        background: rgba($color: #3e7eff, $alpha: 0.1);
        border-radius: 6px;
        color: #3e7eff;
        font-size: 14px;
        padding: 8px;
        text-align: left;
      }
    }
  }

  ::v-deep .commonDialog {
    .el-dialog__body {
      padding: 5px 15px;
    }
    .searchBtn {
      justify-content: space-between;
      padding: 10px 0;
      border-bottom: 1px solid #e3e3e3;
      ::v-deep .el-button {
        height: 32px;
      }
    }
    .tableList {
      height: 600px;
      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>