<template>
  <div>
    <div class="con-title">重修申请</div>
    <div class="con-box">
      <el-button type="text">
        <router-link :to="{ path: '/app/affairs-hall/hall', query: { index: 2 } }">返回列表</router-link>
      </el-button>
      <el-row type="flex" justify="center">
        <el-col :lg="12">
          <el-form ref="form" :model="ruleForm" :rules="rules" :disabled="isView" label-width="110px">
            <el-form-item label="姓名" prop="personal_name" required>
              <el-input
                v-model="ruleForm.personal_name"
                readonly
                :disabled="disabledInfo"
                placeholder="请输入您的姓名"
              />
            </el-form-item>
            <el-form-item label="班级" prop="class_name" required>
              <el-input v-model="ruleForm.class_name" readonly :disabled="disabledInfo" placeholder="请输入您的班级" />
            </el-form-item>
            <el-form-item label="Marywood ID" prop="sofia_id" required>
              <el-input
                v-model="ruleForm.sofia_id"
                readonly
                :disabled="disabledInfo"
                placeholder="请输入您的Marywood ID"
              />
            </el-form-item>
            <el-form-item label="重修学期" prop="semester_name">
              <el-select
                v-model="ruleForm.semester_name"
                placeholder="请选择"
                @change="onSemesterChange"
                :disabled="disabledInfo"
              >
                <el-option v-for="item in semesterList" :key="item.id" :value="item.semester_name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="重修课程" prop="course_id">
              <el-checkbox-group v-model="ruleForm.course_id">
                <el-checkbox v-for="item in courseList" :label="item.id" :key="item.id" :disabled="disabledInfo">{{
                  item.course_name
                }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <div style="padding-left:110px" v-if="hasPay">
              <div class="score" v-if="detail.form">{{ detail.form.score_sum_str }}</div>
              <pay :datalist="ruleForm.payment_instrument" :disabled="disabledPay || isView"></pay>
            </div>
            <template v-if="isView && detail.status !== -1">
              <el-form-item label="审核时间">
                <el-input :value="detail.approve_time1" readonly />
              </el-form-item>
              <el-form-item label="备注" prop="content">
                <el-input type="textarea" :autosize="{ minRows: 5 }" :value="detail.remark" readonly />
              </el-form-item>
            </template>

            <el-form-item>
              <el-button type="primary" @click="onSubmit" v-if="!isView">保存并提交</el-button>
              <el-button type="info" @click="onAbort" v-if="hasAbort">撤回申请</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import * as api from '../api'
import Pay from '../components/pay'

export default {
  props: {
    isAdd: { type: Boolean, default: false },
    isEdit: { type: Boolean, default: false },
    isView: { type: Boolean, default: false }
  },
  components: { Pay },
  data() {
    return {
      detail: {},
      ruleForm: {
        personal_name: '',
        class_name: '',
        sofia_id: '123',
        semester_name: '',
        course_id: [],
        payment_instrument: []
      },
      rules: {
        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' }],
        course_id: [{ required: true, message: '请选择您的重修课程名称', trigger: 'blur' }]
      },
      semesterList: [], // 学期列表
      courseList: [] // 课程列表
    }
  },
  watch: {
    semesterId(value) {
      value && this.getCourses()
    }
  },
  computed: {
    pid() {
      return this.$route.params.id
    },
    // 事务ID
    affairId() {
      return this.$route.query.id
    },
    // 学期ID
    semesterId() {
      const found = this.semesterList.find(item => item.semester_name === this.ruleForm.semester_name)
      return found ? found.id : ''
    },
    // 撤回
    hasAbort() {
      return this.isEdit && this.detail.status === -1
    },
    // 支付凭证
    hasPay() {
      return !this.isAdd && this.detail.file_put !== 0
    },
    // 禁用信息部分
    disabledInfo() {
      return this.isView || (this.isEdit && this.detail.status !== -1)
    },
    // 禁用支付凭证模块
    disabledPay() {
      return this.isView || (this.isEdit && this.detail.file_put === 2)
    }
  },
  methods: {
    init() {
      // 获取学期
      this.getSemesters()

      if (this.isEdit || this.isView) {
        this.getDetail()
      } else {
        // 获取学生信息
        this.getStudent()
      }
    },
    // 获取学生信息
    getStudent() {
      api.getStudent().then(response => {
        const data = this.$_.pick(response, ['personal_name', 'class_name', 'sofia_id'])
        Object.assign(this.ruleForm, data)
      })
    },
    // 获取学期
    getSemesters() {
      api.getSemesters().then(response => {
        this.semesterList = response.data
        const [first] = this.semesterList
        // 设置默认值
        if (first && !this.isEdit) {
          this.ruleForm.semester_name = first.semester_name
        }
      })
    },
    onSemesterChange() {
      this.ruleForm.course_id = []
    },
    // 获取课程
    getCourses() {
      api.getCourses(this.semesterId).then(response => {
        this.courseList = response.data
      })
    },
    // 获取详情
    getDetail() {
      api.getAffair(this.pid).then(response => {
        const form = response.form
        if (form.course_name_arr) {
          form.course_id = form.course_name_arr.map(item => item.course_id)
        }
        // 设置表单数据
        const ruleForm = this.$_.pick(response.form, [
          'personal_name',
          'class_name',
          'sofia_id',
          'semester_name',
          'course_id',
          'payment_instrument'
        ])
        Object.assign(this.ruleForm, ruleForm)
        // 设置接口返回的数据
        this.detail = response
      })
    },
    // 添加
    addRequest(data) {
      api.createAffair(data).then(response => {
        this.$message.success(response.message)
        this.$router.push({ path: '/app/affairs-hall/hall', query: { index: 2 } })
      })
    },
    // 修改
    updateRequest(data) {
      api.updateAffair(this.pid, data).then(response => {
        this.$message.success(response.message)
        this.$router.push({ path: '/app/affairs-hall/hall', query: { index: 2 } })
      })
    },
    // 提交
    onSubmit() {
      this.$refs.form
        .validate()
        .then(response => {
          const data = Object.assign({ affair_id: this.affairId }, this.ruleForm)
          data.course_id = this.ruleForm.course_id.join(',')
          data.payment_instrument = JSON.stringify(this.ruleForm.payment_instrument)
          this.isEdit ? this.updateRequest(data) : this.addRequest(data)
        })
        .catch(() => {
          this.$message.error('请完善表单信息')
        })
    },
    // 撤回
    onAbort() {
      this.$confirm('你确定要撤回重修申请吗？撤回后，需要重新提交申请并由教务审批。', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {})
        .catch(() => {})
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form--label-top .el-form-item__label {
  line-height: 1;
}
.score {
  margin-bottom: 20px;
  color: #b80037;
}
</style>
