<template>
  <div>
    <div class="con-title">重修申请</div>
    <div class="con-box">
      <el-button type="text" @click='gobackActiveList'>返回列表</el-button>
      <el-row type="flex" justify="center">
        <el-col :xs="24" :sm="24" :md="16" :lg="12" :xl="8">
          <el-form ref="setApplyForm" :label-width="labelWidth" :model="setApply" :rules="applyRules">
            <el-form-item label="姓名" prop="personal_name">
              <el-input v-model="setApply.personal_name" placeholder="请输入您的姓名" type="text" />
            </el-form-item>
            <el-form-item label="班级" prop="class_name">
              <el-input v-model="setApply.class_name" placeholder="请输入您的班级" type="text" />
            </el-form-item>
            <el-form-item label="挂科学期" prop="semester_name">
              <el-input v-model="setApply.semester_name" placeholder="请输入您的挂科学期" type="text" />
            </el-form-item>
            <el-form-item label="重修课程" prop="course_name">
              <el-input v-model="setApply.course_name" placeholder="请输入您的重修课程名称" type="text" />
            </el-form-item>
            <el-form-item label="上传缴费凭证" prop="file">
              <el-upload
                ref="upFile"
                class="upload-demo"
                list-type="picture-card"
                action=""
                :multiple="false"
                :limit="1"
                :on-change="handleChange"
                :http-request="uploadFile"
                :file-list="filesArr">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">长传图片.jpg,.jpeg,.png</div>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onApplyFrom">保存并提交</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import cAction from '@actions'

export default {
  props: {
    rid: { type: String, required: false }
  },
  components: {},
  data () {
    return {
      labelWidth: '110px',
      setApply: {
        personal_name: '',
        class_name: '',
        semester_name: '',
        course_name: ''
      },
      applyRules: {
        'personal_name': [
          { required: true, message: '请输入您的姓名', trigger: 'blur' }
        ],
        'class_name': [
          { required: true, message: '请输入您的班级', trigger: 'blur' }
        ],
        'semester_name': [
          { required: true, message: '请输入您的挂科学期', trigger: 'blur' }
        ],
        'course_name': [
          { required: true, message: '请输入您的重修课程名称', trigger: 'blur' }
        ]
      },
      successFileUrl: '',
      filesArr: [],
      file: {
        id: 'WU_FILE_0',
        name: '',
        type: '',
        lastModifiedDate: '',
        size: '',
        file: ''
      },
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  created () {
    this.affair_id = this.$route.query.id
  },
  mounted () {
    if (this.rid !== '-1') {
      const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
      cAction.reportAction.getReportActive(this.rid).then(data => {
        if (data.symposium_name) {
          this.setApply.name = data.symposium_name
          this.setApply.time = data.symposium_time
          this.setApply.author = data.symposium_lecturer
          this.setApply.address = data.symposium_address
          this.setApply.content = data.symposium_contents
          this.successFileUrl = data.symposium_attachments
        }
      }).catch(e => { this.$message.error(e.message) }).finally(() => { loading.close() })
    }
    window.addEventListener('resize', () => {
      let width = document.documentElement.clientWidth
      if (width < 790) {
        this.labelWidth = 'auto'
      } else {
        this.labelWidth = '110px'
      }
    })
  },
  methods: {
    handleChange (file, filelist) {
      this.file.name = file.raw.name
      this.file.type = file.raw.type
      this.file.lastModifiedDate = file.raw.lastModifiedDate
      this.file.size = file.raw.size
      this.file.file = file.raw
    },
    uploadFile () {
      const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
      cAction.reportAction.uploadFile(this.file).then(data => {
        this.successFileUrl = data.url
        // this.filesArr.pop()
      }).catch(e => { this.filesArr.pop(); this.$message.error(e.message) }).finally(() => { loading.close() })
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    gobackActiveList () {
      this.$router.push({ path: `/app/service-hall/hall` })
    },
    onApplyFrom () {
      this.$refs['setApplyForm'].validate((valid) => {
        if (valid) {
          if (!this.successFileUrl) {
            this.$message.error('请上传缴费凭证')
            return
            // this.$refs['setApplyForm'].resetFields()
          }
          const loading = this.$loading({ lock: true, text: '', spinner: '', background: 'rgba(255, 255, 255, 0.9)' })
          let reportActive = null
          if (this.rid !== '-1') {
            /* 修改 */
            reportActive = cAction.reportAction.submitLearning(this.rid, {
              symposium_name: this.setApply.name,
              symposium_time: this.setApply.time,
              symposium_lecturer: this.setApply.author,
              symposium_address: this.setApply.address,
              symposium_contents: this.setApply.content,
              symposium_attachments: this.successFileUrl
            })
          } else {
            /* 新建 */
            reportActive = cAction.reportAction.submitLearning({
              affair_id: this.affair_id,
              personal_name: this.setApply.personal_name,
              class_name: this.setApply.class_name,
              semester_name: this.setApply.semester_name,
              course_name: this.setApply.course_name,
              payment_instrument: this.successFileUrl
            })
          }
          reportActive.then(data => {
            if (data.success) {
              this.$message({ type: 'success', message: '提交成功' })
              setTimeout(() => {
                this.$router.push({ path: `/app/service-hall/hall` })
              }, 500)
            }
          }).catch(e => { this.filesArr.pop(); this.$message.error(e.message) }).finally(() => { loading.close() })
        } else {
          this.$message.error('请检查输入项，确认无误后，重新提交')
          return false
        }
      })
    }
  }
}
</script>


<style lang="scss" scoped>
/* 申请 */
.el-date-editor.el-input {
  width: 100%;
}
.el-upload__tip {
  line-height: 1.5;
}
</style>
