<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="用车需求编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vdNo">
              <a-input v-model="model.vdNo" placeholder="请输入用车需求编号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="发货类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shipType">
              <j-dict-select-tag type="list" v-model="model.shipType" dictCode="ship_type" placeholder="请选择发货类型" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="车型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="carType">
              <a-input v-model="model.carType" placeholder="请输入车型" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="车长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="carLong">
              <a-input-number v-model="model.carLong" placeholder="请输入车长" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="取货仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pickUpHub">
              <a-input v-model="model.pickUpHub" placeholder="请输入取货仓库" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="送货地点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveryAddress">
              <a-input v-model="model.deliveryAddress" placeholder="请输入送货地点" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="供应商编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierCode">
              <a-input v-model.trim="model.supplierCode" placeholder="请输入供应商编码" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierName">
              <a-input v-model.trim="model.supplierName" placeholder="请输入供应商名称" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="carNum">
              <a-input v-model.trim="model.carNum" placeholder="请输入车牌号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物流公司" :labelCol="{ span: 2 }" :wrapperCol="{ span: 22 }" prop="logistics">
              <a-auto-complete v-model="model.logistics" placeholder="请输入物流公司"  @select="logisticsSelect" @change="logisticsChange" :dataSource="logisticsList"></a-auto-complete>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="driverName">
              <a-input v-model.trim="model.driverName" placeholder="请输入姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="身份证" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="driverIdCard">
              <a-input v-model.trim="model.driverIdCard" placeholder="请输入身份证"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="driverTel">
              <a-input v-model.trim="model.driverTel" placeholder="请输入电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="预计到达" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="etaTime">
              <j-date v-model="model.etaTime" placeholder="请选择时间" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;"></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="进厂门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enterFactoryDoor">
              <a-input v-model.trim="model.enterFactoryDoor" placeholder="请输入进厂门"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="出厂门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="exitFactoryDoor">
              <a-input v-model.trim="model.exitFactoryDoor" placeholder="请输入出厂门"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'TakeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        driverInfoList:[],
        logisticsList:[],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 },
        },
        confirmLoading: false,
        validatorRules: {
          vdNo :[{required: true, message: '请选择用车需求编号!'}],
          shipType :[{required: true, message: '请选择发货类型!'}],
          carType :[{required: true, message: '请选择车型!'}],
          carLong :[{required: true, message: '请选择车长!'}],
          pickUpHub :[{required: true, message: '请选择取货仓库!'}],
          deliveryAddress :[{required: true, message: '请选择送货地点!'}],
          supplierCode :[{required: true, message: '请选择供应商编码!'}],
          carNum :[{required: true, message: '请输入车牌号!'}],
          logistics :[{required: true, message: '请输入物流公司!'}],
          driverName :[{required: true, message: '请输入司机姓名!'}],
          driverTel :[{required: true, message: '请输入电话!'}],
          etaTime :[{required: true, message: '请选择时间!'}],
          
        },
        url: {
          add: "/vehicledemandcount/vehicleDemandCount/add",
          take: "/vehicleinout/vehicleInOut/take",
          queryById: "/vehicledemandcount/vehicleDemandCount/queryById",
          getDriverInfo: "/logisticsdriver/logisticsDriver/getDriverInfo"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDriverInfo();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            // 入厂类型: 提货
            this.model.efType = '1';
            httpAction(this.url.take,this.model,'post').then((res)=>{
              if(res.success){
                // that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },

      getDriverInfo(){
        getAction(this.url.getDriverInfo).then(res => {
          if (res.success) {
            this.driverInfoList = res.result;
            this.logisticsList = res.result;
          }
        })
      },
      logisticsSelect(){
        let logistics = this.logisticsList.filter(x=>x.includes(this.model.logistics))[0].split('&-')[0];
        let carNum = this.logisticsList.filter(x=>x.includes(this.model.logistics))[0].split('&-')[1];
        let driverName = this.logisticsList.filter(x=>x.includes(this.model.logistics))[0].split('&-')[2];
        let driverTel = this.logisticsList.filter(x=>x.includes(this.model.logistics))[0].split('&-')[3];
        let driverIdCard = this.logisticsList.filter(x=>x.includes(this.model.logistics))[0].split('&-')[4];
        this.model.logistics = logistics;
        this.model.carNum = carNum;
        this.model.driverName = driverName;
        this.model.driverTel = driverTel;
        this.model.driverIdCard = driverIdCard;
      },
      logisticsChange(val){
        this.logisticsList = this.driverInfoList.filter(x => x.includes(val));
      },
    }
  }
</script>