<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'number', validatorRules.number]" placeholder="请输入车牌号"></a-input>
        </a-form-item>
        <a-form-item label="车辆类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请选择车辆类型" v-decorator="[ 'carType', validatorRules.carType]" >
            <a-select-option value="中巴">中巴</a-select-option>
            <a-select-option value="轿车">轿车</a-select-option>
            <a-select-option value="大巴">大巴</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="能源类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请选择能源类型" v-decorator="[ 'energyType', validatorRules.energyType]" >
            <a-select-option value="燃油">燃油</a-select-option>
            <a-select-option value="电能源">电能源</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="座位数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'seat', validatorRules.seat]" placeholder="请输入座位数量"></a-input>
        </a-form-item>
        <a-form-item label="购买价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'price', validatorRules.price]" placeholder="请输入购买价格" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="购置日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择购置日期" v-decorator="[ 'purchaseDate', validatorRules.purchaseDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="发动机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'engineNum', validatorRules.engineNum]" placeholder="请输入发动机号码"></a-input>
        </a-form-item>
        <a-form-item label="车架号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'vin', validatorRules.vin]" placeholder="请输入车架号"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: "CarModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          number: {rules: [
          ]},
          carType: {rules: [
          ]},
          energyType: {rules: [
          ]},
          seat: {rules: [
          ]},
          price: {rules: [
          ]},
          purchaseDate: {rules: [
          ]},
          engineNum: {rules: [
          ]},
          vin: {rules: [
          ]},
          accesory: {rules: [
          ]},
        },
        url: {
          add: "/oa/car/add",
          edit: "/oa/car/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'number','carType','energyType','seat','price','purchaseDate','engineNum','vin','accesory'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'number','carType','energyType','seat','price','purchaseDate','engineNum','vin','accesory'))
      },

      
    }
  }
</script>