<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="gasId" >
        <a-input v-model="form.gasId" placeholder="请输入油卡ID" />
      </a-form-model-item>
      <a-form-model-item label="油卡类型" prop="gasCardType" >
      </a-form-model-item>
      <a-form-model-item label="车辆分类ID" prop="categoryId" >
      </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="车牌号码" prop="plateNumber" >
        <a-input v-model="form.plateNumber" placeholder="请输入车牌号码" />
      </a-form-model-item>
      <a-form-model-item label="交易时间" prop="tradingHour" >
        <a-input v-model="form.tradingHour" placeholder="请输入交易时间" />
      </a-form-model-item>
      <a-form-model-item label="交易ID" prop="tradingId" >
        <a-input v-model="form.tradingId" placeholder="请输入交易ID" />
      </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="gasType" >
      </a-form-model-item>
      <a-form-model-item label="加油升数" prop="litres" >
        <a-input v-model="form.litres" placeholder="请输入加油升数" />
      </a-form-model-item>
      <a-form-model-item label="加油金额" prop="amount" >
        <a-input v-model="form.amount" placeholder="请输入加油金额" />
      </a-form-model-item>
      <a-form-model-item label="卡内余额" prop="balance" >
        <a-input v-model="form.balance" placeholder="请输入卡内余额" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark" >
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
      </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 { getFuelConsumptionLog, addFuelConsumptionLog, updateFuelConsumptionLog } from '@/api/clw/fuelConsumptionLog'

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

        createBy: null,

        gasId: null,

        gasCardType: null,

        categoryId: null,

        categoryName: null,

        plateNumber: null,

        tradingHour: null,

        tradingId: null,

        gasCode: null,

        gasType: null,

        litres: null,

        amount: null,

        balance: null,

        remark: 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,

        gasId: null,

        gasCardType: null,

        categoryId: null,

        categoryName: null,

        plateNumber: null,

        tradingHour: null,

        tradingId: null,

        gasCode: null,

        gasType: null,

        litres: null,

        amount: null,

        balance: null,

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