<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="申请人ID" prop="userId" >
      </a-form-model-item>
      <a-form-model-item label="申请用车时间" prop="applicationTime" >
      </a-form-model-item>
      <a-form-model-item label="预计归还时间" prop="returnTime" >
      </a-form-model-item>
      <a-form-model-item label="部门ID" prop="deptId" >
      </a-form-model-item>
      <a-form-model-item label="部门名称" prop="deptName" >
      </a-form-model-item>
      <a-form-model-item label="车牌号码" prop="plateNumber" >
      </a-form-model-item>
      <a-form-model-item label="车辆ID" prop="carId" >
      </a-form-model-item>
      <a-form-model-item label="驾驶人ID" prop="driverId" >
      </a-form-model-item>
      <a-form-model-item label="驾驶人名称" prop="driverName" >
      </a-form-model-item>
      <a-form-model-item label="是否涉及非定点加油" prop="isNonfixedPoint" >
        <a-select placeholder="请选择是否涉及非定点加油" v-model="form.isNonfixedPoint">
          <a-select-option v-for="(d, index) in isNonfixedPointOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="出发地" prop="startPlace" >
      </a-form-model-item>
      <a-form-model-item label="目的地" prop="endPlace" >
      </a-form-model-item>
      <a-form-model-item label="审核状态" prop="applyStatus" >
      </a-form-model-item>
      <a-form-model-item label="任务ID" prop="taskId" >
      </a-form-model-item>
      <a-form-model-item label="实际归还时间" prop="realReturnTime" >
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getApplyCarLog, addApplyCarLog, updateApplyCarLog } from '@/api/clw/applyCarLog'

export default {
  name: 'CreateForm',
  props: {
    isNonfixedPointOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        userId: null,

        applicationTime: null,

        returnTime: null,

        deptId: null,

        deptName: null,

        plateNumber: null,

        carId: null,

        driverId: null,

        driverName: null,

        isNonfixedPoint: null,

        startPlace: null,

        endPlace: null,

        applyStatus: 0,

        taskId: null,

        realReturnTime: null,

        remark: null,

        createTime: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        userId: null,

        applicationTime: null,

        returnTime: null,

        deptId: null,

        deptName: null,

        plateNumber: null,

        carId: null,

        driverId: null,

        driverName: null,

        isNonfixedPoint: null,

        startPlace: null,

        endPlace: null,

        applyStatus: 0,

        taskId: null,

        realReturnTime: null,

        remark: null,

        createTime: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getApplyCarLog(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateApplyCarLog(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addApplyCarLog(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
