<template>
  <div>
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <el-tab-pane label="待审核列表" name="first">
        <el-form :inline="true">
          <el-form-item prop="" label="教研名称: ">
            <el-input
              placeholder="请输入教研名称"
              v-model="searchData.keyword"
              clearable
              @clear="getListData"
            ></el-input>
          </el-form-item>
          <el-form-item prop="" label="主讲老师: ">
            <el-input
              placeholder="请输入主讲老师"
              v-model="searchData.teacher"
              clearable
              @clear="getListData"
            ></el-input>
          </el-form-item>
          <el-form-item prop="" label="年级：">
            <el-select
              v-model="searchData.grade"
              clearable
              size="large"
              placeholder="年级"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in gradeData"
                :key="item.grade_id"
                :label="item.grade_name"
                :value="item.grade_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" label="学科：">
            <el-select
              clearable
              v-model="searchData.subject"
              size="large"
              placeholder="学科"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in subjectData"
                :key="item.subject_id"
                :label="item.subject_name"
                :value="item.subject_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" label="时间">
            <el-date-picker
              v-model="searchData.time"
              value-format="YYYY-MM-DD HH:mm:ss"
              clearable
              type="datetime"
              placeholder="请输入时间"
              @clear="getListData"
              @change="timeClear"
            />
          </el-form-item>
          <el-form-item prop="" label="学校：">
            <el-select
              v-model="searchData.school"
              clearable
              size="large"
              placeholder="学校"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in schoolData"
                :key="item.id"
                :label="item.sm_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-button type="primary" style="padding-bottom" @click="searchList"
            >搜索</el-button
          >
          <el-button type="warning" @click="clearData">清空</el-button>
        </el-form>
        <div class="flex justify-between items-center border-bottom">
          <el-button @click="exportData" type="success">导出</el-button>
          <div class="print flex items-center">
            <img
              class="w-20px h-20px cursor-pointer"
              src="/images/print.png"
              alt=""
            />
          </div>
        </div>
        <el-table
          :data="tableData"
          tooltip-effect="dark"
          v-loading="loading"
          ref="multipleTable"
          @selection-change="handleSelectionChange"
          class="mt20"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="学校" prop="school_name" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="教研名称" show-overflow-tooltip>
            <template #default="scope">
              <div class="jy_name">
                {{ scope.row.zz_name }}
              </div>
            </template></el-table-column
          >
          <el-table-column
            label="年级"
            prop="grade_name"
            width="110"
          ></el-table-column>
          <el-table-column
            label="学科"
            prop="subject_name"
            width="110"
          ></el-table-column>
          <el-table-column
            label="主讲老师"
            prop="zz_jsxx"
            width="140"
          ></el-table-column>
          <el-table-column label="时间" prop="time" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="操作" width="100" align="center">
            <template #default="scope">
              <el-button type="text" @click="auditData(scope.row)"
                >审核
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <footer
          class="flex-item-none"
          style="display: flex; justify-content: flex-end; margin-top: 30px"
        >
          <el-pagination
            background
            layout="total,prev, pager, next"
            :page-size="limit"
            :total="total"
            v-model:current_page="currentPage"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </footer>
      </el-tab-pane>
      <el-tab-pane label="已通过" name="second">
        <el-form :inline="true">
          <el-form-item prop="" label="教研名称: ">
            <el-input
              placeholder="请输入教研名称"
              v-model="searchData.keyword"
              clearable
              @clear="getListData"
            ></el-input>
          </el-form-item>
          <el-form-item prop="" label="主讲老师: ">
            <el-input
              placeholder="请输入主讲老师"
              v-model="searchData.teacher"
              clearable
              @clear="getListData"
            ></el-input>
          </el-form-item>
          <el-form-item prop="" label="年级：">
            <el-select
              v-model="searchData.grade"
              size="large"
              clearable
              placeholder="年级"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in gradeData"
                :key="item.grade_id"
                :label="item.grade_name"
                :value="item.grade_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" label="学科：">
            <el-select
              v-model="searchData.subject"
              size="large"
              clearable
              placeholder="学科"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in subjectData"
                :key="item.subject_id"
                :label="item.subject_name"
                :value="item.subject_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" label="时间">
            <el-date-picker
              clearable
              v-model="searchData.time"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="datetime"
              placeholder="请选择时间"
              @change="timeClear"
            />
          </el-form-item>
          <el-form-item prop="" label="学校：">
            <el-select
              v-model="searchData.school"
              size="large"
              clearable
              placeholder="学校"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in schoolData"
                :key="item.id"
                :label="item.sm_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-button type="primary" style="padding-bottom" @click="searchList"
            >搜索</el-button
          >
          <el-button type="warning" @click="clearData">清空</el-button>
        </el-form>
        <div class="flex justify-between items-center border-bottom">
          <el-button @click="exportData" type="success">导出</el-button>
          <div class="print flex items-center">
            <img
              class="w-20px h-20px cursor-pointer"
              src="/images/print.png"
              alt=""
            />
          </div>
        </div>
        <el-table
          :data="tableData"
          tooltip-effect="dark"
          v-loading="loading"
          ref="multipleTable"
          @selection-change="handleSelectionChange"
          class="mt20"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="学校" prop="school_name" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="教研名称" show-overflow-tooltip>
            <template #default="scope">
              <div class="jy_name">
                {{ scope.row.zz_name }}
              </div>
            </template></el-table-column
          >
          <el-table-column
            label="年级"
            prop="grade_name"
            width="110"
          ></el-table-column>
          <el-table-column
            label="学科"
            prop="subject_name"
            width="110"
          ></el-table-column>
          <el-table-column
            label="主讲老师"
            prop="zz_jsxx"
            width="140"
          ></el-table-column>
          <el-table-column label="时间" prop="time" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="状态" width="100" align="center">
            <template #default="scope">
              <el-button type="success" link>已通过 </el-button>
            </template>
          </el-table-column>
        </el-table>

        <footer
          class="flex-item-none"
          style="display: flex; justify-content: flex-end; margin-top: 30px"
        >
          <el-pagination
            background
            layout="total,prev, pager, next"
            :page-size="limit"
            :total="total"
            v-model:current_page="currentPage"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </footer>
      </el-tab-pane>
      <el-tab-pane label="未通过" name="third">
        <el-form :inline="true">
          <el-form-item prop="" label="教研名称: ">
            <el-input
              placeholder="请输入教研名称"
              v-model="searchData.keyword"
              clearable
              @clear="getListData"
            ></el-input>
          </el-form-item>
          <el-form-item prop="" label="主讲老师: ">
            <el-input
              placeholder="请输入主讲老师"
              v-model="searchData.teacher"
              clearable
              @clear="getListData"
            ></el-input>
          </el-form-item>
          <el-form-item prop="" label="年级：">
            <el-select
              v-model="searchData.grade"
              size="large"
              clearable
              placeholder="年级"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in gradeData"
                :key="item.grade_id"
                :label="item.grade_name"
                :value="item.grade_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" label="学科：">
            <el-select
              v-model="searchData.subject"
              size="large"
              clearable
              placeholder="学科"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in subjectData"
                :key="item.subject_id"
                :label="item.subject_name"
                :value="item.subject_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" label="时间">
            <el-date-picker
              clearable
              v-model="searchData.time"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="datetime"
              placeholder="请选择时间"
              @change="timeClear"
            />
          </el-form-item>
          <el-form-item prop="" label="学校：">
            <el-select
              v-model="searchData.school"
              size="large"
              clearable
              placeholder="学校"
              @clear="getListData"
            >
              <el-option label="全部" value="" />
              <el-option
                v-for="item in schoolData"
                :key="item.id"
                :label="item.sm_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-button type="primary" style="padding-bottom" @click="searchList"
            >搜索</el-button
          >
          <el-button type="warning" @click="clearData">清空</el-button>
        </el-form>
        <div class="flex justify-between items-center border-bottom">
          <el-button @click="exportData" type="success">导出</el-button>
          <div class="print flex items-center">
            <img
              class="w-20px h-20px cursor-pointer"
              src="/images/print.png"
              alt=""
            />
          </div>
        </div>
        <el-table
          :data="tableData"
          tooltip-effect="dark"
          v-loading="loading"
          ref="multipleTable"
          @selection-change="handleSelectionChange"
          class="mt20"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="学校" prop="school_name" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="教研名称" show-overflow-tooltip>
            <template #default="scope">
              <div class="jy_name">
                {{ scope.row.zz_name }}
              </div>
            </template></el-table-column
          >
          <el-table-column
            label="年级"
            prop="grade_name"
            width="110"
          ></el-table-column>
          <el-table-column
            label="学科"
            prop="subject_name"
            width="110"
          ></el-table-column>
          <el-table-column
            label="主讲老师"
            prop="zz_jsxx"
            width="140"
          ></el-table-column>
          <el-table-column label="时间" prop="time" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="状态" width="100" align="center">
            <template #default="scope">
              <el-button type="danger" link>未通过 </el-button>
            </template>
          </el-table-column>
        </el-table>

        <footer
          class="flex-item-none"
          style="display: flex; justify-content: flex-end; margin-top: 30px"
        >
          <el-pagination
            background
            layout="total,prev, pager, next"
            :page-size="limit"
            :total="total"
            v-model:current_page="currentPage"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </footer>
      </el-tab-pane>
    </el-tabs>

    <el-dialog
      title="审核"
      v-loading="popLoading"
      v-model="dialogFormVisible"
      width="770px"
      class="pr-30px"
    >
      <el-form
        v-if="!popLoading"
        :model="form"
        disabled
        label-width="140px"
        ref="formRef"
        :rules="rules"
      >
        <el-form-item label="课程封面图" prop="zz_img">
          <img class="w-140px" :src="form.zz_img" alt="" />
          <!-- <file-upload-progress v-model:part="form.kz_img" :limit="1" list-type="picture-card" accept=".jpg,.png,.jpeg"
            :size-limit="200">
            <template #default>
              <i-akar-icons:plus class="text-4xl" />
            </template>
            <template #tip>
              <div class="mt-2">支持格式：jpg,png,jpeg（200kb以内） 建议尺寸600x320</div>
            </template>
          </file-upload-progress> -->
        </el-form-item>

        <el-form-item label="主讲老师" prop="zz_jsxx">
          <el-input v-model="form.zz_jsxx" />
        </el-form-item>
        <el-form-item label="教研名称" prop="zz_name">
          <el-input v-model="form.zz_name" />
        </el-form-item>
        <el-form-item label="课程简介" prop="zz_content">
          <el-input v-model="form.zz_content" type="textarea" :rows="4" />
        </el-form-item>
        <!-- <el-form-item label="课时数" prop="kz_kss">
          <el-input v-model.number="form.kz_kss" />
        </el-form-item> -->
        <div class="flex justify-between">
          <el-form-item label="年级" prop="grade_id">
            <remote-select v-model="form.grade_id" to="grade"></remote-select>
          </el-form-item>
          <el-form-item label="学科" prop="subject_id">
            <remote-select
              v-model="form.subject_id"
              to="subject"
            ></remote-select>
          </el-form-item>
        </div>

        <!-- <el-form-item label="开始时间" prop="kz_starttime">
          <el-date-picker v-model="form.kz_starttime" type="datetime" />
        </el-form-item>
        <el-form-item label="结束时间" prop="kz_endtime">
          <el-date-picker v-model="form.kz_endtime" type="datetime" />
        </el-form-item> -->
        <el-form-item label="开始时间 - 结束时间" prop="zz_time">
          <el-date-picker
            v-model="form.zz_time"
            type="datetimerange"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <div class="flex justify-center items-center text-sm mt-10">
          <div
            class="
              w-80px
              h-40px
              flex_center
              rounded
              bg-hex-F4F4F4
              text-hex-41387F
              cursor-pointer
              m-5
            "
            @click="handleBack"
          >
            取消
          </div>
          <div
            class="
              w-80px
              h-40px
              flex_center
              rounded
              bg-hex-00A3FF
              text-white
              cursor-pointer
              m-5
            "
            @click="handlePass"
          >
            通过
          </div>
          <div
            class="
              w-80px
              h-40px
              flex_center
              rounded
              bg-hex-F4F4F4
              text-hex-41387F
              cursor-pointer
              m-5
            "
            @click="handleUnPass"
          >
            不通过
          </div>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, nextTick } from "vue";
import { Search } from "@element-plus/icons-vue";
import { download } from "~/utils/request";
import {
  grade_list,
  subject_list,
  school_list,
  teacher_list,
  wlzb_list,
  wlzb_detail,
  wlzb_edit,
} from "./api";
import { user } from "@/store/user";
let shstatus = $ref("1");
const activeName = ref("first");
let form = $ref({
  zz_name: "",
  zz_img: "",
  grade_id: "",
  subject_id: "",
  zz_jsxx: "",
  zz_content: "",
  zz_starttime: "",
  zz_endtime: "",
  zz_time: undefined,
  zz_shzt: "",
});
const rules = {
  zz_img: [{ required: true, message: "请选择课程封面图", trigger: "submit" }],
  zz_jsxx: [{ required: true, message: "请输入主讲老师", trigger: "submit" }],
  zz_name: [{ required: true, message: "请输入教研名称", trigger: "submit" }],
  zz_content: [
    { required: true, message: "请输入课程简介", trigger: "submit" },
  ],
};
let searchData = $ref({
  keyword: "",
  teacher: "",
  grade: "",
  subject: "",
  time: "",
  school: "",
});
// 年级
let gradeData = ref([]);
function getGrade() {
  grade_list({ site: "tyyx" }).then((res) => {
    gradeData.value = res.data.page_data;
  });
}
// 学科
let subjectData = ref([]);
function getSubject() {
  subject_list({ site: "tyyx" }).then((res) => {
    subjectData.value = res.data.page_data;
  });
}
// 学校
let schoolData = ref([]);
function getSchool() {
  school_list({ site: "tyyx" }).then((res) => {
    schoolData.value = res.data.page_data;
  });
}
getGrade();
getSubject();
getSchool();
// 直播列表
const loading = ref(false);
let tableData = ref([]);
// 分页
let total = $ref(0);
const limit = $ref(10);
let currentPage = $ref("1");

let zbdata = {
  admin: 1,
  site: "tyyx",
  limit,
  page: currentPage,
  //   keyword: "",
  //   zz_jsxx: "",
  //   sm_id: "",
  //   create_dateline: "",
  //   grade_id: "",
  //   subject_id: "",
  zz_shzt: "2",
};
function handleCurrentChange(val) {
  currentPage = val;
  zbdata.page = currentPage;
  getZbData();
}

// 初始化
function getZbData() {
  loading.value = true;
  wlzb_list(zbdata).then((res) => {
    if (res.code == 1) {
      res.data.page_data.forEach((item) => {
        item.time = item.zz_starttime + "-" + item.zz_endtime;
        // let a = item.zt_cyjyjs_json.replace(/\\/g, "");
        // item.zt_cyjyjs_json = JSON.parse(a);
        // item.zt_cyjyjs_json.forEach((n) => {
        //   item.zt_cyjyjs_json = n.user_name;
        // });
      });
      tableData.value = res.data.page_data;
      total = parseInt(res.data.total_rows);
      loading.value = false;
    }
  });
}
getZbData();
// 切换tab
const handleClick = (tab, event) => {
  if (event.target.id === "tab-first") {
    zbdata.zz_shzt = "2";
  } else if (event.target.id === "tab-second") {
    zbdata.zz_shzt = "1";
  } else {
    zbdata.zz_shzt = "3";
  }
  console.log(zbdata);
  getZbData();
};
// 搜索
function searchList() {
  const { keyword, teacher, grade, subject, time, school } = searchData;
  Object.assign(zbdata,{
    keyword: keyword,
    zz_jsxx: teacher,
    sm_id: school,
    create_dateline: time,
    grade_id: grade,
    subject_id: subject,
  });
  getZbData();
}
function timeClear(time) {
  if (!time) {
    time = '';
    searchList();
  }
}
function getListData() {
  searchList();
}
// 清空搜索
function clearData() {
  searchData = {
    keyword: "",
    teacher: "",
    grade: "",
    subject: "",
    time: "",
    school: "",
  };
  searchList();
}
// 导出
let multipleSelection = $ref([]);
const handleSelectionChange = (val) => {
  multipleSelection = [];
  val.forEach(item => {
    multipleSelection.push(item.zz_id) ;
  })
}
function exportData() {
  const { keyword, teacher, grade, subject, time, school } = searchData;
  let data = {
    admin: "1",
    site: "tyyx",
    keyword,
    zz_jsxx: teacher,
    grade_id: grade,
    subject_id: subject,
    zz_fbsj: time,
    zz_shzt: zbdata.zz_shzt,
    zz_id: multipleSelection
  };
  download("zhjy/zxxx/index", data);
}
// 审核
let popLoading = ref(false);
let dialogFormVisible = $ref(false);
let zz_id = "";
function auditData(item) {
  zz_id = item.zz_id;
  dialogFormVisible = true;
  popLoading.value = true;
  let data = {
    site: "tyyx",
    admin: 1,
    zz_id: item.zz_id,
  };
  wlzb_detail(data).then((res) => {
    if (res.code === "1") {
      Object.keys(form).forEach((key) => {
        form[key] = res.data.one_info[key];
      });
      form.zz_time = [
        res.data.one_info.zz_starttime,
        res.data.one_info.zz_endtime,
      ];
      //   let a = res.data.one_info.zt_cyjyjs_json.replace(/\\/g, "");
      //   form.zt_cyjyjs_json = JSON.parse(a);
      //   form.zt_cyjyjs_json.forEach((n) => {
      //     form.zt_cyjyjs_json = n.user_name;
      //   });
      // let a = JSON.parse(res.data.one_info.zt_cyjyjs_json);
      // console.log(a);
      popLoading.value = false;
    }
  });
}

function handleBack() {
  dialogFormVisible = false;
}
function handlePass() {
  let data = {
    admin: "1",
    site: "tyyx",
    issubmit: "1",
    zz_id,
    zhjy_zxxx: {
      zz_shzt: "1",
    },
  };
  wlzb_edit(data).then((res) => {
    if (res.code === "1") {
      ElMessage({
        message: "操作成功",
        type: "success",
      });
    } else {
      ElMessage.error("操作失败");
    }
    handleBack();
    getZbData();
  });
}
function handleUnPass() {
  let data = {
    admin: "1",
    site: "tyyx",
    issubmit: "1",
    zz_id,
    zhjy_zxxx: {
      zz_shzt: "3",
    },
  };
  wlzb_edit(data).then((res) => {
    if (res.code === "1") {
      ElMessage({
        message: "操作成功",
        type: "success",
      });
    } else {
      ElMessage.error("操作失败");
    }
    handleBack();
    getZbData();
  });
}
</script>

<style lang="scss" scoped>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #00a3ff;
  font-size: 32px;
  font-weight: 600;
}

::v-deep .el-form-item__label {
  font-size: 14px;
  font-family: PingFangSC, PingFangSC-Regular;
  font-weight: 400;
  text-align: left;
  color: #000000;
}
::v-deep .el-input__wrapper {
  width: 160px;
}
.form-list {
  ::v-deep .el-form-item__label {
    margin-left: 45px;
  }
  ::v-deep .el-form-item {
    margin-right: 0;
  }
}
.jy_name {
  color: #00a3ff;
  width: 180px;
  padding-right: 10px;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.el-dialog__body {
  .el-input,
  .el-textarea {
    width: 560px;
  }
  .el-select {
    width: 210px;
  }
}
</style>
