<template>
  <a-drawer width="55%" :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="gasCardType">
        <a-select placeholder="请选择加油卡类型" v-model="form.gasCardType" :disabled="formType==3">
          <a-select-option v-for="(d, index) in gasCardTypeOptions" :key="index" :value="d.value">{{
            d.label
          }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="车牌号" prop="carId">
        <a-select placeholder="请选择加油卡类型" v-model="form.carId" @change="changeCarNum" :disabled="formType==3">
          <a-select-option v-for="(d, index) in Object.values(cacheCarPlateNumber)" :key="index" :value="d.id" >{{
            d.plateNumber
          }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="机构名称" prop="deptName">
        <a-input v-model="form.deptName" placeholder="请输入机构名称" disabled />
      </a-form-model-item>
      <a-form-model-item label="车辆分类" prop="carCategoryName">
        <a-input v-model="form.carCategoryName" placeholder="请输入车辆分类" disabled />
      </a-form-model-item>
      <a-form-model-item label="油品" prop="gasType">
        <a-input v-model="form.gasType" placeholder="请输入油品" :disabled="formType==3" />
      </a-form-model-item>
      <a-form-model-item label="油卡号" prop="gasCode">
        <a-input v-model="form.gasCode" placeholder="请输入油卡号" :disabled="formType==3" />
      </a-form-model-item>
      <a-form-model-item label="初始金额" prop="initialAmount">
        <a-input v-model="form.initialAmount" placeholder="请输入初始金额" :disabled="formType==3" />
      </a-form-model-item>
      <a-form-model-item label="状态" prop="status">
        <a-select placeholder="请选择状态" v-model="form.status" :disabled="formType==3">
          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.value">{{
            d.label
          }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear :disabled="formType==3" />
      </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 { getGas, addGas, updateGas, plateNumberList } from '@/api/clw/gas'
export default {
  name: 'CreateForm',
  props: {
    gasCardTypeOptions: {
      type: Array,
      required: true,
    },
    statusOptions: {
      type: Array,
      required: true,
    },
  },
  components: {},
  data() {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        gasCardType: null,

        carId: null,

        deptName: null,

        carCategoryName: null,

        gasType: null,

        gasCode: null,

        initialAmount: null,

        status: '1',

        remark: null,
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        gasCardType: [{ required: true, message: '加油卡类型不能为空', trigger: 'change' }],

        carId: [{ required: true, message: '车辆ID不能为空', trigger: 'change' }],

        deptName: [{ required: true, message: '机构名称不能为空', trigger: 'blur' }],

        carCategoryName: [{ required: true, message: '车辆分类名称不能为空', trigger: 'blur' }],

        gasType: [{ required: true, message: '油品不能为空', trigger: 'blur' }],

        gasCode: [{ required: true, message: '油卡号不能为空', trigger: 'blur' }],

        initialAmount: [{ required: true, message: '初始金额不能为空', trigger: 'blur' }],

        status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
      },
      cacheCarPlateNumber: {},
    }
  },
  filters: {},
  created() {
    this.getCarNum()
  },
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.formType = 1
      this.form = {
        gasCardType: null,

        carId: null,

        deptName: null,

        carCategoryName: null,

        gasType: null,

        gasCode: null,

        initialAmount: null,

        status: '1',

        remark: null,
      }
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row, ids) {
      this.reset()
      this.formType = 2
      const gasId = row ? row.gasId : ids
      getGas(gasId).then((response) => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    handleInfo(row, ids){
      this.formType = 3
      const gasId = row ? row.gasId : ids
      getGas(gasId).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.gasId !== undefined && this.form.gasId !== null) {
            updateGas(this.form)
              .then((response) => {
                this.$message.success('修改成功', 3)
                this.open = false
                this.$emit('ok')
              })
              .finally(() => {
                this.submitLoading = false
              })
          } else {
            addGas(this.form)
              .then((response) => {
                this.$message.success('新增成功', 3)
                this.open = false
                this.$emit('ok')
              })
              .finally(() => {
                this.submitLoading = false
              })
          }
        } else {
          return false
        }
      })
    },
    getCarNum() {
      plateNumberList().then((res) => {
        this.cacheCarPlateNumber = res.data.reduce((config, item) => {
          config[item.id] = item
          return config
        }, {})
      })
    },
    changeCarNum(id) {
      const node = this.cacheCarPlateNumber[id]
      this.form.carId = node.id
      this.form.deptId = node.deptId
      this.form.deptName = node.deptName
      this.form.carCategoryName = node.categoryName
      this.form.plateNumber = node.plateNumber
    },
   
  },
}
</script>
