<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="请选择时间"
              @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.zx_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="zx_bkjs_json"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="创建人"
            prop="user_name"
            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"
              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
              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 show-overflow-tooltip label="学校" prop="school_name" >
          </el-table-column>
          <el-table-column show-overflow-tooltip label="课程名称">
            <template #default="scope">
              <div class="jy_name">
                {{ scope.row.zx_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="备课教师"
            show-overflow-tooltip
            prop="zx_bkjs_json"
            width="180"
          ></el-table-column>
          <el-table-column
            label="创建人"
            prop="user_name"
            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 show-overflow-tooltip label="学校" prop="school_name">
          </el-table-column>
          <el-table-column show-overflow-tooltip label="课程名称" width="180">
            <template #default="scope">
              <div class="jy_name">
                {{ scope.row.zx_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="zx_bkjs_json"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="创建人"
            prop="user_name"
            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="zx_img">
          <img class="w-140px" :src="form.zx_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="user_name">
          <el-input v-model="form.user_name" />
        </el-form-item>
        <el-form-item label="课程名称" prop="zx_name">
          <el-input v-model="form.zx_name" />
        </el-form-item>
        <el-form-item label="课程简介" prop="zx_brief">
          <el-input v-model="form.zx_brief" type="textarea" :rows="4" />
        </el-form-item>
        <div class="flex justify-between">
          <el-form-item label="年级" prop="grade_id">
            <el-input v-model="form.grade_name" style="width: 200px;" />
            <!-- <remote-select v-model="form.grade_id" to="grade"></remote-select> -->
          </el-form-item>
          <el-form-item label="学科" prop="subject_id">
            <el-input v-model="form.subject_name" style="width: 200px;" />
            <!-- <remote-select
              v-model="form.subject_id"
              to="subject"
            ></remote-select> -->
          </el-form-item>
        </div>
        <el-form-item label="开始时间 - 结束时间" prop="zx_time">
          <el-date-picker
            v-model="form.zx_time"
            type="datetimerange"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="备课教师" prop="zx_bkjs_json">
          <el-input v-model="form.zx_bkjs_json" type="text" :rows="4" />
        </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 {
  grade_list,
  subject_list,
  school_list,
  teacher_list,
  wlzb_list,
  wlzb_detail,
  wlzb_edit,
} from "./api";
import { download } from "~/utils/request";
import { user } from "@/store/user";
let shstatus = $ref("1");
const activeName = ref("first");
let form = $ref({
  zx_img: "",
  user_name: "",
  zx_bkjs_json: "",
  zx_name: "",
  zx_brief: "",
  grade_id: "",
  grade_name: "",
  subject_id: "",
  subject_name: "",
  zx_starttime: "",
  zx_endtime: "",
  zx_time: undefined,
  zx_shzt: "",
});
const rules = {
  zx_img: [{ required: true, message: "请选择课程封面图", trigger: "submit" }],
  zx_master_teacher: [
    { required: true, message: "请输入创建人", trigger: "submit" },
  ],
  zx_master_user_name: [
    { required: true, message: "请输入主讲账号", trigger: "submit" },
  ],
  zx_name: [{ required: true, message: "请输入课程名称", trigger: "submit" }],
  zx_brief: [{ 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: "",
  // user_name: "",
  // sm_id: "",
  // create_dateline: "",
  grade_id: "",
  // subject_id: "",
  zx_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.zx_kssj + "-" + item.zx_jssj;
        if (item.zx_bkjs_json)
        {
          //  let a = item.zx_bkjs_json.replace(/\\/g, "");
          let a = item.zx_bkjs_json;
           item.zx_bkjs_json = JSON.parse(a);
           let b = [];
          item.zx_bkjs_json.forEach((n) => {
            b.push(n.user_realname);
          });
          item.zx_bkjs_json = b;
        }
        // <empty string>应该就是这个报错的 
        else {
         item.zx_bkjs_json = []
       }
      });
      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.zx_shzt = "2";
  } else if (event.target.id === "tab-second") {
    zbdata.zx_shzt = "1";
  } else {
    zbdata.zx_shzt = "3";
  }
  console.log(zbdata);
  getZbData();
};
// 搜索
function searchList() {
  const { keyword, teacher, grade, subject, time, school } = searchData;
  Object.assign(zbdata, {
    keyword: keyword,
    create_user_name: 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.zx_id) ;
  })
}
function exportData() {
  const { keyword, teacher, grade, subject, time, school } = searchData;
  let data = {
    admin: "1",
    site: "tyyx",
    keyword,
    user_name: teacher,
    grade_id: grade,
    subject_id: subject,
    zx_shzt: zbdata.zx_shzt,
    zx_id:multipleSelection
  };
  download("/zhjy/xtbk/index", data);
}
// 审核
let popLoading = ref(false);
let dialogFormVisible = $ref(false);
let zx_id = "";
function auditData(item) {
  zx_id = item.zx_id;
  dialogFormVisible = true;
  popLoading.value = true;
  Object.keys(form).forEach((key) => {
    form[key] = item[key];
  });
  form.zx_time = [item.zx_kssj, item.zx_jssj];
  popLoading.value = false;
}

function handleBack() {
  dialogFormVisible = false;
}
function handlePass() {
  let data = {
    admin: "1",
    site: "tyyx",
    issubmit: "1",
    zx_id,
    zhjy_xtbk: {
      zx_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",
    zx_id,
    zhjy_xtbk: {
      zx_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;
  }
}
// ::v-deep .el-table_1_column_3 {
//   .cell {
//     color: #00a3ff;
//   }
// }
.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>
