<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="formData.orderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="formData.userName" placeholder="请输入用户姓名"/>
        </el-form-item>
        <el-form-item label="用户手机号" prop="userPhone">
          <el-input v-model="formData.userPhone" placeholder="请输入用户手机号"/>
        </el-form-item>
        <el-form-item label="用户身份证号" prop="userNo">
          <el-input v-model="formData.userNo" placeholder="请输入用户身份证号"/>
        </el-form-item>
        <el-form-item label="性别" prop="userSex">
          <el-radio-group v-model="formData.sex">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
                      :key="dict.value" :label="dict.value">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="课程名字" prop="courseName">
          <el-input v-model="formData.courseName" placeholder="请输入课程名字"/>
        </el-form-item>

        <el-form-item label="负责教练" prop="coachUserName">
          <el-input v-model="formData.coachUserName" placeholder="请输入主负责教练姓名"/>
        </el-form-item>
        <el-form-item label="订金金额" prop="reserveMoney">
          <el-input v-model="formData.reserveMoney" placeholder="请输入订金金额"/>
        </el-form-item>

        <el-form-item label="是否终止" prop="ifEnd">
          <el-radio-group v-model="formData.ifEnd">
            <el-radio v-for="item in ifEndRadio"
                      :key="item.value" :label="item.value">{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="终止原因" prop="endReason">
          <el-input v-model="formData.endReason" placeholder="请输入终止原因"/>
        </el-form-item>
        <el-form-item label="终止时间" prop="endTime">
          <el-date-picker clearable v-model="formData.endTime" type="date" value-format="timestamp"
                          placeholder="选择终止时间"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="paymentStatus">
          <el-select v-model="formData.paymentStatus" placeholder="请选择订单状态">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DRIVE_SCHOOL_ORDER_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否已面签" prop="isSign">
          <el-select v-model="formData.isSign" placeholder="请选择是否已面签">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="尾款" prop="restMoney">
          <el-input v-model="formData.restMoney" placeholder="请输入尾款"/>
        </el-form-item>
        <el-form-item label="支付类型" prop="payType">
          <el-select v-model="formData.paymentStatus" placeholder="请选择支付类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DRIVE_SCHOOL_PAY_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as SchoolCourseOrderApi from '@/views/drivingSchool/DriveSchoolPay/api';

export default {
  name: "SchoolCourseOrderForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        orderNo: undefined,
        userName: undefined,
        userPhone: undefined,
        userNo: undefined,
        userSex: undefined,
        courseId: undefined,
        courseName: undefined,
        coachUserId: undefined,
        coachUserName: undefined,
        reserveMoney: undefined,
        courseType: undefined,
        ifEnd: undefined,
        endReason: undefined,
        endTime: undefined,
        paymentStatus: undefined,
        isSign: undefined,
        restMoney: undefined,
        payType: undefined,
      },
      ifEndRadio:[
        {
          label:'是',
          value:'1'
        },
        {
          label:'否',
          value:'0'
        },
      ],
      // 表单校验
      formRules: {},
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await SchoolCourseOrderApi.getSchoolCourseOrder(id);
          this.formData = res.data;
          this.title = "修改驾照报名订单";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增驾照报名订单";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await SchoolCourseOrderApi.updateSchoolCourseOrder(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await SchoolCourseOrderApi.createSchoolCourseOrder(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        orderNo: undefined,
        userName: undefined,
        userPhone: undefined,
        userNo: undefined,
        userSex: undefined,
        courseId: undefined,
        courseName: undefined,
        coachUserId: undefined,
        coachUserName: undefined,
        reserveMoney: undefined,
        courseType: undefined,
        ifEnd: undefined,
        endReason: undefined,
        endTime: undefined,
        paymentStatus: undefined,
        isSign: undefined,
        restMoney: undefined,
        payType: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
