<!-- <filter-search @reload="reloadSearch" searchKey="ids" />  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      学生考勤列表
    </h2>
    <div class="m-core">
      <save-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="studentID">
          <el-input
            v-model.trim="searchForm.studentID"
            clearable
            @keyup.enter.native="search"
            placeholder="学生编号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="teacherId">
          <remote-search
            :commit="'setTeachers'"
            v-model="searchForm.teacherId"
          />
        </el-form-item>

        <el-form-item prop="organId">
          <el-select
            class="multiple"
            filterable
            style="width: 180px !important"
            v-model.trim="searchForm.organId"
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item, index) in selects.branchs"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="musicGroupId">
          <el-input
            v-model.trim="searchForm.musicGroupId"
            clearable
            @keyup.enter.native="search"
            placeholder="乐团编号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="courseScheduleId">
          <el-input
            v-model.trim="searchForm.courseScheduleId"
            clearable
            @keyup.enter.native="search"
            placeholder="课程编号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="groupType">
          <el-select
            v-model.trim="searchForm.groupType"
            placeholder="请选择课程组类型"
          >
            <el-option
              v-for="(item, index) in courseListType"
              :key="index"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="courseScheduleType">
          <el-select
            v-model.trim="searchForm.courseScheduleType"
            clearable
            placeholder="请选择课程类型"
          >
            <el-option
              v-for="(item, index) in courseType"
              :key="index"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="visitFlag">
          <el-select
            v-model.trim="searchForm.visitFlag"
            placeholder="请选择是否回访"
            clearable
          >
            <el-option label="否" value="0"></el-option>
            <el-option label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="status">
          <el-select
            v-model.trim="searchForm.status"
            placeholder="请选择考勤状态"
            clearable
          >
            <el-option
              v-for="(item, index) in attendanceStatus"
              :key="index"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="dates">
          <el-date-picker
            v-model="searchForm.dates"
            type="daterange"
            style="width: 405px"
            range-separator="至"
            start-placeholder="课程开始日期"
            end-placeholder="课程结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="danger">搜索</el-button>
          <el-button native-type="reset" type="primary">重置</el-button>
          <!-- <el-button
            @click="onExport"
            type="primary"
            v-permission="'export/queryTeacherAttendances'"
            style="background-color: #14928a; border: 1px solid #14928a"
            >导出</el-button> -->
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="courseSchedule.organization.name"
            label="分部"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="username"
            label="学生姓名"
            width="110px"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.username }}
                <p style="color: #f56c6c">
                  (<copy-text>{{ scope.row.userId }}</copy-text
                  >)
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="courseSchedule.teacherName"
            width="120px"
            label="老师姓名"
          >
            <template slot-scope="scope">
              {{ scope.row.courseSchedule.teacherName }}
              <p style="color: #f56c6c" v-if="scope.row.teacherId">
                (<copy-text>{{ scope.row.teacherId }}</copy-text
                >)
              </p>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="musicGroupId" label="乐团编号">
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.musicGroupId }}</copy-text>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="courseScheduleId"
            label="课程编号"
          >
            <template slot-scope="scope">
              <div>
                <copy-text>{{ scope.row.courseScheduleId }}</copy-text>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="courseSchedule.name"
            label="课程名称"
          ></el-table-column>
          <!-- <el-table-column
            align="center"
            prop="courseSchedule.classDate"
            label="上课日期"
          >
            <template slot-scope="scope">
              <div>

              </div>
            </template>
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="startClassTime"
            label="课程组类型"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.groupType | coursesType }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="startClassTime"
            label="课程类型"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.courseSchedule.type | coursesType }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="合并类型">
            <template slot-scope="scope">
              <div>
                <!-- {{ scope.row.courseSchedule.newCourseId > 0 ? "是" : "否" }} -->
                {{
                  scope.row.courseSchedule.newCourseId > 0 &&
                  scope.row.courseSchedule.newCourseId ==
                    scope.row.courseScheduleId
                    ? "合并课"
                    : null
                }}
                {{
                  scope.row.courseSchedule.newCourseId > 0 &&
                  scope.row.courseSchedule.newCourseId !=
                    scope.row.courseScheduleId
                    ? "被合并课"
                    : null
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="180px"
            align="center"
            prop="startClassTime"
            label="上课时间"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.courseSchedule.classDate | dayjsFormat }}
                {{
                  scope.row.courseSchedule.startClassTime | dayjsFormatMinute
                }}-{{
                  scope.row.courseSchedule.endClassTime | dayjsFormatMinute
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="签到时间" width="180px">
            <template slot-scope="scope">
              <div>
                {{ scope.row.signInTime }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="startClassTime"
            width="180px"
            label="签退时间"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.signOutTime }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="考勤回访">
            <template slot-scope="scope">
              <div>{{ scope.row.visitFlag | yesOrNo }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="考勤状态">
            <template slot-scope="scope">
              <div
                v-if="
                  scope.row.courseSchedule &&
                  scope.row.courseSchedule.status != 'NOT_START'
                "
              >
                {{ scope.row.status | clockingIn }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            v-if="permission(getFullPermission('visit/add'))"
          >
            <template slot-scope="scope">
              <div>
                <el-button
                  type="text"
                  v-if="
                    permission(getFullPermission('visit/add')) &&
                    !scope.row.visitFlag
                  "
                  @click="addVisit(scope.row)"
                  >新增回访</el-button
                >
                <el-button
                  type="text"
                  @click="lookVisit(scope.row)"
                  v-if="
                    scope.row.visitFlag &&
                    permission(getFullPermission('visit/queryPage'))
                  "
                  >查看回访</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
        <el-dialog title="新增回访" width="500px" :visible.sync="visitVisible">
          <visit
            v-if="visitVisible && detail"
            :detail="detail"
            :username="detail.username"
            @close="visitVisible = false"
            @submited="getList"
            :isMainGo="true"
          />
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { getTimes } from "@/utils";
import qs from "qs";
import { permission } from "@/utils/directivePage";
import { findStudentAttendance } from "@/api/buildTeam";
import { Export } from "@/utils/downLoadFile"; 
import cleanDeep from "clean-deep";
// import { queryTeacherAttendances } from "@/api/recodeManager";
import { courseType, courseListType, stuAttendance } from "@/utils/searchArray";
import visit from "@/views/withdrawal-application/modals/visit";
export default {
  components: { pagination, visit },
  data() {
    return {
      visitVisible: false,
      detail: null,
      searchForm: {
        studentID: "",
        groupType: "MUSIC",
        musicGroupId: "",
        courseScheduleId: "",
        status: "",
        teacherId: "",
        courseScheduleType: "",
        organId: "",
        visitFlag: "",
        dates: [],
      },

      courseType,
      courseListType,
      attendanceStatus: stuAttendance,
      // teacherList: [],
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    const { query } = this.$route;
    if (this.searchForm.dates?.length < 1) {
      const start = query.start || new Date();
      const end = query.end || new Date();
      this.searchForm.dates = [start, end];
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    const { query } = this.$route;

    if (query.visitFlag == 1 || query.visitFlag == 0) {
      this.searchForm.visitFlag = String(query.visitFlag);
    }
    if (query.status) {
      this.searchForm.status = query.status;
    }
    this.init();
  },

  methods: {
    permission,
    reloadSearch() {
      this.rules.page = 1;
      this.getList();
    },
    async init() {
      await this.$store.dispatch("setBranchs");
      this.getList();
    },
    addVisit(row) {
      this.visitVisible = true;
      this.detail = row;
    },
    // 导出
    async onExport() {
      const { dates, ...rest } = this.searchForm;
      let obj = {
        ...rest,
        page: this.rules.page,
        rows: this.rules.limit,
        ids: this.$route.query.ids,
        ...getTimes(dates, ["classStartDate", "classEndDate"], "YYYY-MM-DD"),
      };
      await Export(
        this,
        {
          url: "/api-web/export/exportStudentAttendances",
          fileName: "考勤列表.xls",
          method: "post",
          params: qs.stringify(cleanDeep(obj)),
        },
        "您确定考勤列表?"
      );
    },
    getList() {
      const { dates, ...rest } = this.searchForm;
      let obj = {
        ...rest,
        page: this.rules.page,
        rows: this.rules.limit,
        ...getTimes(
          dates,
          ["startDateOfCourse", "endDateOfCourse"],
          "YYYY-MM-DD"
        ),
      };

      // let obj = {
      //   page: this.rules.page,
      //   rows: this.rules.limit,
      //   ...this.searchForm,
      //   ...getTimes(
      //     this.dates,
      //     ["startDateOfCourse", "endDateOfCourse"],
      //     "YYYY-MM-DD"
      //   ),
      // };
      findStudentAttendance(obj, {
        ids: this.$route.query.ids,
      }).then((res) => {
        if (res.code == 200) {
          this.tableList = res.data.rows;
          this.rules.total = res.data.total;
        }
      });
    },
    clearSearchUrl() {
      const { query } = this.$route;
      if (
        query.status ||
        query.visitFlag == 0 ||
        query.visitFlag == 1 ||
        query.start ||
        query.end
      ) {
        this.$router.replace({
          status: undefined,
          visitFlag: undefined,
          start: undefined,
          end: undefined,
        });
      }
    },
    search() {
      this.rules.page = 1;
      this.clearSearchUrl();
      this.getList();
    },
    onReSet() {
      this.$refs["searchForm"].resetFields();
      this.clearSearchUrl();
      this.search();
    },
    lookVisit(row) {
      this.$router.push({
        path: "/studentManager/returnVisitList",
        query: { search: row.id },
      });
    },
  },
};
</script>
<style lang='scss' scoped>
</style>
