<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="车辆分类" prop="categoryId" >
        <a-input v-model="form.categoryId" placeholder="请输入车辆分类" />
      </a-form-model-item>
      <a-form-model-item label="车辆分类名称" prop="categoryName" >
        <a-input v-model="form.categoryName" placeholder="请输入车辆分类名称" />
      </a-form-model-item>
      <a-form-model-item label="部门ID" prop="deptId" >
        <a-input v-model="form.deptId" placeholder="请输入部门ID" />
      </a-form-model-item>
      <a-form-model-item label="部门名称" prop="deptName" >
        <a-input v-model="form.deptName" placeholder="请输入部门名称" />
      </a-form-model-item>
      <a-form-model-item label="车牌号" prop="plateNumber" >
        <a-input v-model="form.plateNumber" placeholder="请输入车牌号" />
      </a-form-model-item>
      <a-form-model-item label="加油卡号" prop="gasCode" >
        <a-input v-model="form.gasCode" placeholder="请输入加油卡号" />
      </a-form-model-item>
      <a-form-model-item label="上次加油量" prop="prevFuelCharge" >
        <a-input v-model="form.prevFuelCharge" placeholder="请输入上次加油量" />
      </a-form-model-item>
      <a-form-model-item label="上次消耗金额" prop="prevLoseAmount" >
        <a-input v-model="form.prevLoseAmount" placeholder="请输入上次消耗金额" />
      </a-form-model-item>
      <a-form-model-item label="本次划拨金额" prop="currentAmount" >
        <a-input v-model="form.currentAmount" placeholder="请输入本次划拨金额" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入备注" />
      </a-form-model-item>
      <a-form-model-item label="审核状态" prop="applyStatus" >
      </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 { getOilSkimmingLog, addOilSkimmingLog, updateOilSkimmingLog } from '@/api/clw/oilSkimmingLog'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        createTime: null,

        createBy: null,

        categoryId: null,

        categoryName: null,

        deptId: null,

        deptName: null,

        plateNumber: null,

        gasCode: null,

        prevFuelCharge: null,

        prevLoseAmount: null,

        currentAmount: null,

        remark: null,

        applyStatus: 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 = {
        createTime: null,

        createBy: null,

        categoryId: null,

        categoryName: null,

        deptId: null,

        deptName: null,

        plateNumber: null,

        gasCode: null,

        prevFuelCharge: null,

        prevLoseAmount: null,

        currentAmount: null,

        remark: null,

        applyStatus: 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
      getOilSkimmingLog(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) {
            updateOilSkimmingLog(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addOilSkimmingLog(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
