<template>
  <div>
    <div class="con-title">重修申请</div>
    <div class="con-box">
      <el-button type="text" @click="goBack">返回列表</el-button>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        :disabled="isView"
        label-width="100px"
        class="main-form"
        v-loading="loading"
      >
        <el-form-item label="姓名" prop="personal_name">
          <el-input v-model="ruleForm.personal_name" :disabled="disabledInfo" placeholder="请输入您的姓名" />
        </el-form-item>
        <el-form-item label="班级" prop="class_name">
          <el-input v-model="ruleForm.class_name" :disabled="disabledInfo" placeholder="请输入您的班级" />
        </el-form-item>
        <el-form-item label="Sofia ID" prop="sofia_id">
          <el-input v-model="ruleForm.sofia_id" :disabled="disabledInfo" placeholder="请输入您的Sofia ID" />
        </el-form-item>
        <el-form-item label="重修时间" prop="semester_name">
          <el-radio-group
            v-model="ruleForm.semester_name"
            :disabled="disabledInfo"
            style="line-height: 30px; margin-top: 10px"
          >
            <div v-for="item in semesterList" :key="item.value">
              <el-radio :label="item.value"></el-radio>
            </div>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="重修课程" prop="course_id">
          <el-checkbox-group
            v-model="ruleForm.course_id"
            :disabled="disabledInfo"
            style="line-height: 24px; margin-top: 10px"
          >
            <el-checkbox
              v-for="item in currentCourseList"
              :label="item.id"
              :key="item.id"
              style="width: 50%; margin: 0"
              >{{ item.course_name }}</el-checkbox
            >
          </el-checkbox-group>
          <div class="score" v-if="hasPay && detail.form">{{ detail.form.credit_sum_str }}</div>
        </el-form-item>
        <template v-if="isView && detail.status !== -1">
          <el-form-item label="支付方式" style="margin: 0" v-if="detail.pay !== -1">
            {{ payText }}
          </el-form-item>
          <el-form-item label="支付状态" style="margin: 0" v-if="detail.pay !== -1">
            {{ payStatusText }}
          </el-form-item>
          <el-form-item label="审核状态" style="margin: 0">
            {{ statusText }}
          </el-form-item>
          <el-form-item label="审核时间" style="margin: 0" v-if="detail.approve_time1">
            {{ detail.approve_time1 }}
          </el-form-item>
          <el-form-item label="备注" style="margin: 0" v-if="detail.remark">
            {{ detail.remark }}
          </el-form-item>
        </template>
        <template v-if="hasPay">
          <el-form-item label="支付方式" prop="payment">
            <el-radio-group v-model="ruleForm.payment">
              <div v-for="item in payList" :key="item.value" style="margin: 10px 0">
                <el-radio :label="item.value">{{ item.label }}</el-radio>
              </div>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handlePay">去支付</el-button>
          </el-form-item>
        </template>
        <el-form-item v-else>
          <el-button type="primary" @click="handleSubmit" v-if="!isView">保存并提交</el-button>
          <!-- <el-button type="info" @click="onAbort" v-if="hasAbort">撤回申请</el-button> -->
        </el-form-item>
      </el-form>
    </div>

    <el-dialog title="支付" width="400px" :visible.sync="payVisible">
      <pay
        :payment="ruleForm.payment"
        :productId="productId"
        :num="detail.form.credit"
        @success="handlePaySuccess"
        v-if="payVisible"
      ></pay>
    </el-dialog>
  </div>
</template>
<script>
import * as api from '../../api'
import editMixins from '../../mixins/edit'
import Pay from '../../components/pay'
export default {
  mixins: [editMixins],
  components: { Pay },
  data() {
    return {
      loading: false,
      ruleForm: {
        personal_name: '',
        class_name: '',
        sofia_id: '123',
        semester_name: '',
        course_id: [],
        payment: '1'
      },
      rules: {
        personal_name: [{ required: true, message: '请输入您的姓名', trigger: 'blur' }],
        class_name: [{ required: true, message: '请输入您的班级', trigger: 'blur' }],
        sofia_id: [{ required: true, message: '请输入Sofia ID', trigger: 'blur' }],
        semester_name: [{ required: true, message: '请选择您的重修时间', trigger: 'blur' }],
        course_id: [{ required: true, message: '请选择您的重修课程', trigger: 'blur' }]
      },
      courseList: [], // 课程列表
      submitedCourseList: [], // 提交的课程列表
      productId: '6745273257873637376', // 商品ID
      payVisible: false
    }
  },
  computed: {
    currentCourseList() {
      return this.courseList.concat(this.submitedCourseList)
    }
  },
  methods: {
    init() {
      if (this.isEdit || this.isView) {
        this.getDetail()
      } else {
        // 获取学生信息
        this.getStudent()
      }
      // 获取重修课程
      this.getCourses()
    },
    // 获取课程
    getCourses() {
      api.getRetakeCourses().then(response => {
        this.courseList = response || []
      })
    },
    // 获取详情
    getDetail() {
      this.loading = true
      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)
            this.submitedCourseList = form.course_name_arr.map(item => {
              item.id = item.course_id
              return item
            })
          }
          // 设置表单数据
          const ruleForm = this.$_.pick(response.form, [
            'personal_name',
            'class_name',
            'sofia_id',
            'semester_name',
            'course_id',
            'payment'
          ])
          Object.assign(this.ruleForm, ruleForm)
          // 设置接口返回的数据
          this.detail = response
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 提交
    handleSubmit() {
      this.$refs.ruleForm.validate().then(() => {
        const params = Object.assign({}, this.ruleForm, { affair_id: this.affairId })
        params.course_id = this.ruleForm.course_id.join(',')
        this.isEdit ? this.handleUpdate(params) : this.handleAdd(params)
      })
    },
    // 撤回
    onAbort() {
      this.$confirm('你确定要撤回重修申请吗？撤回后，需要重新提交申请并由教务审批。', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {})
        .catch(() => {})
    },
    // 去支付
    handlePay() {
      this.payVisible = true
    },
    // 支付成功
    handlePaySuccess() {
      api.paySuccess(this.pid).then(response => {
        this.getDetail()
        this.payVisible = false
      })
    }
  },
  beforeMount() {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.main-form {
  max-width: 600px;
  margin: 0 auto;
}

::v-deep .el-form--label-top .el-form-item__label {
  line-height: 2;
}
.score {
  color: #b80037;
}
</style>
