<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" readonly />
        </el-form-item>
        <el-form-item label="班级" prop="class_name">
          <el-input v-model="ruleForm.class_name" :disabled="disabledInfo" readonly />
        </el-form-item>
        <el-form-item label="Sofia ID" prop="sofia_id">
          <el-input v-model="ruleForm.sofia_id" :disabled="disabledInfo" readonly />
        </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">
            <div v-for="item in currentCourseList" :key="item.id">
              <el-checkbox :label="item.id">{{ item.course_name }}</el-checkbox>
            </div>
          </el-checkbox-group>
          <div class="score" v-if="(hasPay || isView) && detail.form">{{ detail.form.credit_sum_str }}</div>
        </el-form-item>
        <template v-if="isView">
          <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 && ruleForm.payment !== '3'">
            {{ 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.remark1 || detail.remark2">
            {{ detail.remark1 || detail.remark2 }}
          </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" :loading="submitLoading" @click="handleSubmit" v-if="!isView">保存并提交</el-button>
          <el-button type="info" @click="onRemove" v-if="hasAbort">撤回申请</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog title="支付" width="400px" :visible.sync="payVisible">
      <pay :payment="ruleForm.payment" :product="product" :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: '2'
      },
      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: [], // 提交的课程列表
      product: { shop_id: '6816247184455892992', spu_id: '6869463821962969088', sku_id: '6869463822004912128' }, // 商品
      payVisible: false
    }
  },
  computed: {
    currentCourseList() {
      if (this.isView) {
        return this.submitedCourseList
      }
      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 || []
          if (!this.isEdit && !this.isView && !this.courseList.length) {
            this.$alert('您没有需要重修的课程', {
              type: 'warning',
              confirmButtonText: '确定',
              callback: action => {
                this.goBack()
              }
            })
          }
        })
        .catch(() => {
          if (!this.isEdit && !this.isView) {
            this.$alert('您没有需要重修的课程', {
              type: 'warning',
              confirmButtonText: '确定',
              callback: action => {
                this.goBack()
              }
            })
          }
        })
    },
    // 获取详情
    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(callback) {
      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, callback) : this.handleAdd(params, callback)
      })
    },
    // 去支付
    handlePay() {
      this.handleSubmit(() => {
        this.payVisible = true
      })
    },
    // 支付成功
    handlePaySuccess() {
      api.paySuccess(this.pid).then(response => {
        this.$router.replace({ name: 'retakeView', params: { id: this.pid }, query: { id: this.affairId } })
      })
    }
  },
  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>
