<template>
  <a-drawer width="45%" :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="masterCardCode">
      <a-input type="number" v-model="form.masterCardCode" placeholder="请输入住卡号" :disabled="formType == 3" />
    </a-form-model-item>
      <a-form-model-item label="加油卡类型" prop="gasCardType">
        <a-select
          placeholder="请选择加油卡类型"
          v-model="form.gasCardType"
          :disabled="formType == 3"
          @change="shangeCardType"
        >
          <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" v-if="showInput == 2 || form.gasCardType == '2'">
        <a-select
          placeholder="请选择车牌号"
          v-model="form.carId"
          @change="changeCarNum"
          :disabled="formType == 3"
          showSearch
          :filterOption="filterOption"
        >
          <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" v-if="showInput == 2 || form.gasCardType == '2'">
        <a-input v-model="form.deptName" placeholder="请输入机构名称" disabled />
      </a-form-model-item>
      <a-form-item label="机构名称" prop="deptId" v-if="showInput == 1 || form.gasCardType == '1'">
        <a-select placeholder="请选择" v-model="form.deptId" style="width: 100%" allow-clear @change="changeDept">
          <a-select-option v-for="(d, index) in deptIdList" :key="index" :value="d.id">{{ d.lable }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-model-item label="车辆分类" prop="carCategoryName" v-if="showInput == 2 || form.gasCardType == '2'">
        <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-select placeholder="请选择加油卡类型" v-model="form.gasType" :disabled="formType == 3">
          <a-select-option v-for="(d, index) in dict.type['gas_type']" :key="index" :value="d.value">{{
            d.label
          }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="油卡号" prop="gasCode">
        <a-input type="number" v-model="form.gasCode" placeholder="请输入油卡号" :disabled="formType == 3" />
      </a-form-model-item>
      <a-form-model-item label="初始金额（元）" prop="initialAmount">
        <a-input type="number" 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',
  dicts: ['gas_type'],
  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,
        masterCardCode: null,
      },
      deptIdList: [
        {
          id: 100,
          lable: '两江新区消防救援支队',
        },
        {
          id: 173,
          lable: '两江新区支队鸳鸯消防救援站',
        },
        {
          id: 141,
          lable: '两江新区支队翠云消防救援站',
        },
        {
          id: 142,
          lable: '两江新区支队龙头寺消防救援站',
        },
        {
          id: 140,
          lable: '两江新区支队大竹林特勤站',
        },
        {
          id: 139,
          lable: '两江新区支队礼嘉消防救援站',
        },
        {
          id: 129,
          lable: '两江新区支队奥园消防救援站',
        },
      ],
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
        masterCardCode:[{required:true, message: '主卡号不能为空', trigger: 'blur' }],
        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: 'change' }],
        gasCode: [{ required: true, message: '油卡号不能为空', trigger: 'blur' }],
        initialAmount: [{ required: true, message: '初始金额不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
      },
      cacheCarPlateNumber: {},
      disabled: true,
      showInput: '',
    }
  },
  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,
        masterCardCode: 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) => {
        if (response.data.gasCardType == '2') {
          this.disabled = true
        } else if (response.data.gasCardType == '1') {
          this.disabled = false
        }
        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
    },
    filterOption(value, option) {
      return option.componentOptions.children[0].text.indexOf(value) >= 0
    },
    shangeCardType(val) {
      this.showInput = val
      if (val == 1) {
        this.disabled = false
      } else if (val == 2) {
        this.disabled = false
      }
    },
    changeDept(ev) {
      let name = this.deptIdList[ev]
      console.log(name)
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-select {
  color: #000;
}
/deep/.ant-input {
  color: #000;
}
</style>
