<template>
  <div>
    <el-form :model="form" ref="form" label-width="80px">
      <el-form-item
        label="申诉图片"
        prop="url"
      >
        <div style="width: 100%;">
          <el-upload
            action="/api-web/uploadFile"
            class="avatar-uploader"
            :headers="headers"
            :on-success="handleSuccess"
            accept=".jpg, .jpeg, .png"
            :show-file-list="false"
          >
            <img v-if="form.url" :src="form.url" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item
        label="申诉内容"
        prop="content"
        :rules="[{required: true, message: '请输入申诉内容'}]"
      >
        <el-input
          type="textarea"
          v-model="form.content"
          placeholder="请输入申诉内容"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: right;margin-top: 20px;">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </div>
  </div>
</template>
<script>
import { addComplaints } from '../api'
import {
  getToken
} from '@/utils/auth'
export default {
  props: ['detail'],
  data() {
    return{
      headers: {
        Authorization: getToken()
      },
      form: {
        content: '',
        url: '',
      }
    }
  },
  methods: {
    handleSuccess(res) {
      this.$set(this.form, 'url', res.data.url)
    },
    submit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            await addComplaints({
              ...this.form,
              courseScheduleId: this.detail.courseScheduleId,
              userId: this.detail.teacherId,
            })
            this.$message.success('提交成功')
            this.$emit('submited')
            this.$emit('close')
          } catch (error) {}
        }
      })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 78px;
    height: 78px;
    line-height: 78px;
    text-align: center;
  }
  .avatar {
    width: 78px;
    height: 78px;
    display: block;
  }
</style>
