<template>
  <p class="sub_back" @click="handleBack">&#60;返回</p>
  <p class="sub_title">{{readonly?'心理咨询':'个案辅导'}}</p>
  <el-card class="account-container interview">
    <div class="control">
      <p class="control_name">访谈记录</p>
      <el-button type="primary" @click="addInterviewRecord" v-if="!isEdit&&!readonly">保存</el-button>
    </div>
    <el-form :model="form" :rules="rules" ref="formRef" v-if="!readonly">
      <el-form-item label="学生记录：" prop="studentInterviewRecord">
        <el-input type="textarea" :autosize="{ minRows: 4}" v-model="form.studentInterviewRecord"></el-input>
      </el-form-item>
      <div class="btn_box">
        <el-button type="primary" @click="editStudent" v-if="isEdit">保存</el-button>
      </div>

      <el-form-item label="家长记录：" prop="parentInterviewRecord">
        <el-input type="textarea" :autosize="{ minRows: 4}" v-model="form.parentInterviewRecord"></el-input>
      </el-form-item>
      <div class="btn_box">
        <el-button type="primary" @click="editParent" v-if="isEdit">保存</el-button>
      </div>
      <el-form-item label="班主任记录：" prop="teacherInterviewRecord">
        <el-input type="textarea" :autosize="{ minRows: 4}" v-model="form.teacherInterviewRecord"></el-input>
      </el-form-item>
      <div class="btn_box">
        <el-button type="primary" @click="editTeacher" v-if="isEdit">保存</el-button>
      </div>
    </el-form>
    <div v-else>
      <p class="title">学生记录：</p>
      <p>{{form.studentInterviewRecord?form.studentInterviewRecord:'暂无'}}</p>
      <p class="title">家长记录：</p>
      <p>{{form.parentInterviewRecord?form.parentInterviewRecord:'暂无'}}</p>
      <p class="title">老师记录：</p>
      <p>{{form.teacherInterviewRecord?form.teacherInterviewRecord:'暂无'}}</p>
    </div>
  </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'
export default {
  name: 'Interview',
  setup () {
    const formRef = ref(null)
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    const { detailId, readonly } = route.query

    const state = reactive({
      isEdit: false,//新增或者修改的状态
      form: {
        id: '0',
        studentInterviewRecord: '',
        parentInterviewRecord: '',
        teacherInterviewRecord: '',
      },
      UserId: store.state.userInfo?.userId,
      rules: {
      },
    })
    onMounted(() => {
      getInterviewRecord()
    })
    // 访谈记录
    const getInterviewRecord = () => {
      state.loading = true
      axios.get(`/Counse/InterviewRecord?userId=${state.UserId}&detailId=${detailId}`).then(res => {
        // console.log('访谈记录:', res)
        if (res) {
          state.isEdit = true
          state.form = res
        }

      }).catch(err => {
        console.log('err', err)

      })
    }
    // 新增
    const addInterviewRecord = () => {
      axios.post('/Counse/InterviewRecord', {
        "Id": 0,
        "DetailId": detailId,
        "UserId": state.UserId,
        "StudentInterviewRecord": state.form.studentInterviewRecord,
        "ParentInterviewRecord": state.form.parentInterviewRecord,
        "TeacherInterviewRecord": state.form.teacherInterviewRecord
      }).then(() => {
        ElMessage.success('保存成功')
        getInterviewRecord()
      })
    }
    // 修改学生
    const editStudent = () => {
      axios.put('/Counse/StudentInterviewRecord', {
        "Id": state.form.id,
        "DetailId": detailId,
        "UserId": state.UserId,
        "StudentInterviewRecord": state.form.studentInterviewRecord,
        "ParentInterviewRecord": state.form.parentInterviewRecord,
        "TeacherInterviewRecord": state.form.teacherInterviewRecord
      }).then(() => {
        ElMessage.success('保存成功')
      })
    }
    // 修改家长
    const editParent = () => {
      axios.put('/Counse/ParentInterviewRecord', {
        "Id": state.form.id,
        "DetailId": detailId,
        "UserId": state.UserId,
        "StudentInterviewRecord": state.form.studentInterviewRecord,
        "ParentInterviewRecord": state.form.parentInterviewRecord,
        "TeacherInterviewRecord": state.form.teacherInterviewRecord
      }).then(() => {
        ElMessage.success('保存成功')
      })
    }
    // 修改老师
    const editTeacher = () => {
      axios.put('/Counse/TeacherInterviewRecord', {
        "Id": state.form.id,
        "DetailId": detailId,
        "UserId": state.UserId,
        "StudentInterviewRecord": state.form.studentInterviewRecord,
        "ParentInterviewRecord": state.form.parentInterviewRecord,
        "TeacherInterviewRecord": state.form.teacherInterviewRecord
      }).then(() => {
        ElMessage.success('保存成功')
      })
    }

    const handleBack = () => {
      router.back()
    }
    return {
      ...toRefs(state),
      formRef,
      handleBack,
      addInterviewRecord,
      editStudent,
      editParent,
      editTeacher,
      readonly
    }
  }
}
</script>
<style lang="scss" scoped>
.control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .control_name {
    font-size: 23px;
    font-weight: bold;
  }
}
.interview {
  background: #fff;
  padding: 0 20px;
  padding-bottom: 40px;
}
.btn_box {
  width: 100%;
  margin-bottom: 40px;
  display: flex;
  flex-direction: row-reverse;
}
.title {
  font-weight: bold;
  margin-top: 40px;
}
</style>