<template>
  <!-- 老师端显示 -->
  <el-card class="account-container coach" v-if="isTeacher">
    <div class="control">
      <p class="control_name">我的预约</p>
      <div class="select_box">
        <p class="select_tag">预约日期：</p>
        <el-date-picker v-model="dayName" value-format="YYYY-MM-DD" type="date" placeholder="全部" :clearable="false" @change="dayNameChange">
        </el-date-picker>
      </div>
    </div>
    <el-table v-loading="loading" :data="tableData" style="width: 100%" stripe v-if="tableData.length>0">
      <el-table-column label="时间" align="center">
        <template #default="props">
          <p class="time">{{Moment(props.row.dayName).format('YYYY-MM-DD')}} {{props.row.startTime}}-{{props.row.endTime}}</p>
        </template>
      </el-table-column>
      <el-table-column prop="studentName" label="学生姓名" align="center">
      </el-table-column>
      <el-table-column prop="class" label="班级" align="center">
      </el-table-column>
      <el-table-column prop="bookTypeName" label="咨询问题" align="center">
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template #default="props">
          <p>{{props.row.statusName}}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <div v-if="scope.row.statusName=='已预约'">
            <a @click="handleAssessment(scope.row)" style="text-decoration: underline;cursor: pointer; margin-right: 10px">评估</a>
            <el-popconfirm title="确定取消？" confirmButtonText="确认" cancelButtonText="取消" @confirm="handleCancelBooking(scope.row)">
              <template #reference>
                <a style="color:#FF3A3A;text-decoration: underline;cursor:pointer;">取消</a>
              </template>
            </el-popconfirm>
          </div>
          <div v-if="scope.row.statusName=='已有评估记录'">
            <a @click="handleAssessment(scope.row)" style="text-decoration: underline;cursor: pointer; margin-right: 10px">评估</a>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-empty description="暂无内容" v-if="tableData.length==0"></el-empty>
    <el-pagination :hide-on-single-page="total<=10" background layout="prev, pager, next" v-model="currentPage" :total="total" @current-change="handleCurrentChange">
    </el-pagination>
  </el-card>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import axios from '@/utils/axios'
import Moment from 'moment'

export default {
  name: 'Term',
  setup () {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    const state = reactive({
      dayName: '',
      search: '',
      tableData: [],
      total: 0,
      currentPage: 1,
      loading: false,
      isTeacher: true,
      UserId:store.state.userInfo?.userId
    })
    onMounted(() => {
      if (state.isTeacher) {
        // state.dayName = Moment().format('YYYY-MM-DD');
        getBookingRecordTeacher()
      }
    })
    const dayNameChange = () => {
      getBookingRecordTeacher()
    }
    // 教师收到的预约
    const getBookingRecordTeacher = () => {
      state.loading = true
      if (!state.UserId) return;
      axios.post('/SchedualBooking/BookingRecord/Teacher/List', {
        "DetailId": 0,
        // "StudentId": "string",
        "TeacherId": state.UserId,
        "Type": 1,
        "DayName": state.dayName,
        // "StudentName": "string",
        // "ChooseType": 1,
        "PageIndex": state.currentPage,
        "PageSize": 10
      }).then(res => {
        // console.log('教师收到的预约:', res)

        let tableData = res.data;
        for (let i in tableData) {
          tableData[i].time = [tableData[i].startTime, tableData[i].endTime]
        }
        state.tableData = tableData
        state.total = res.count
        state.loading = false
      }).catch(err => {
        console.log('err', err)

      })
    }
    // 取消预约
    const handleCancelBooking = (item) => {
      axios.post('/SchedualBooking/StudentBooking', {
        "DetailId": item.detailId,
        "StudentId": item.studentId,
        "IsBooking": false,
        "BookTypeName": item.bookTypeName,
        "BookDescription": item.bookDescription
      }).then(() => {
        ElMessage.success('取消预约成功')
        state.currentPage = 1;
        getBookingRecordTeacher()
      })
    }
    // 页码改变时
    const handleCurrentChange = (e) => {
      state.currentPage = e;
      getBookingRecordTeacher()
    }
    // 点击进入排班
    const handleAssessment = (item) => {
      router.push({ path: '/assessment', query: { detailId: item.detailId,UserId:item.studentId } })
    }

    return {
      ...toRefs(state),
      handleCancelBooking,
      handleAssessment,
      handleCurrentChange,
      Moment,
      dayNameChange
    }
  }
}
</script>
<style lang="scss" scoped>
.control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .control_name {
    font-size: 23px;
    font-weight: bold;
  }
  .el-input {
    width: 300px;
  }
  .booking_time {
    font-size: 15px;
    span {
      margin-left: 13px;
      font-size: 14px;
      line-height: 27px;
      padding: 0 10px;
      color: #3a84ff;
      background: #f8f9fb;
      border-radius: 4px;
    }
  }
}
.coach {
  .select_box {
    display: flex;
    align-items: center;
  }
}
</style>