<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="12">
            <a-form-model-item label="发货类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shipType">
              <j-dict-select-tag v-model="model.shipType" dictCode="ship_type" placeholder="请选择发货类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pn">
              <!-- <a-input v-model.trim="model.pn" placeholder="请输入料号" @change="changeGetpalletsNum" ></a-input> -->
              <a-auto-complete v-model="model.pn" placeholder="请输入料号"  @select="pnSelect" @change="pnChange" :dataSource="pnList"></a-auto-complete>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spec">
              <a-input v-model="model.spec" placeholder="请输入规格" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发货数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shipNumber">
              <a-input-number v-model="model.shipNumber" :min="0" placeholder="请输入发货数量" style="width: 100%" @change="changeGetpalletsNum"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag placeholder="请选择单位" v-model="model.unit" dictCode="unit"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预估托盘数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="palletsNum">
              <a-input-number v-model="model.palletsNum" placeholder="请输入预估托盘数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发货日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shipDate">
              <j-date placeholder="请选择发货日期"  v-model="model.shipDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="取货仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pickUpHub">
              <!-- <j-dict-select-tag placeholder="请选择取货仓库" v-model="model.pickUpHub" dictCode="tms_logistics_route,start_city,start_city"></j-dict-select-tag> -->
              <a-auto-complete v-model="model.pickUpHub" placeholder="请输入取货仓库"  @select="pickUpHubSelect" @change="pickUpHubChange" :dataSource="pickUpHubList"></a-auto-complete>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="送货地点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveryAddress">
              <!-- <j-dict-select-tag placeholder="请选择送货地点" v-model="model.deliveryAddress" dictCode="tms_logistics_route,target_city,target_city"></j-dict-select-tag> -->
              <a-input v-model="model.deliveryAddress" placeholder="请输入送货地点" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-model-item label="发货单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoice">
              <a-input v-model="model.invoice" placeholder="请输入发货单"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="销售人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salesperson">
              <a-input v-model="model.salesperson" placeholder="请输入销售人员"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="作业备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workNotes">
              <a-input v-model="model.workNotes" placeholder="请输入作业备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="当前状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentStatus">
              <j-dict-select-tag placeholder="请选择当前状态" v-model="model.currentStatus" dictCode="current_status" disabled></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction,postAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
import { off } from 'codemirror'
import { min } from 'xe-utils/methods'

  export default {
    name: 'DeliveryDemandForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        cityList:[],
        pickUpHubList:[],
        pnSpecList:[],
        pnList:[],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          shipType :[{required: true, message: '请选择发货类型!'}],
          pn :[{required: true, message: '请输入料号!'}],
          shipNumber :[{required: true, message: '请输入发货数量!'}],
          shipDate :[{required: true, message: '请选择发货日期!'}],
          deliveryAddress :[{required: true, message: '请选择送货地点!'}],
          pickUpHub :[{required: true, message: '请选择取货仓库!'}],
          spec: [{required: true, message: '请输入规格!'}],
          palletsNum: [{required: true, message: '请输入预估托盘数!'}],
        },
        url: {
          add: "/deliverydemand/deliveryDemand/add",
          edit: "/deliverydemand/deliveryDemand/edit",
          queryById: "/deliverydemand/deliveryDemand/queryById",
          getPalletsNum: "/deliverydemand/deliveryDemand/getPalletsNum",
          getCity: "/logisticsroute/logisticsRoute/getCity",
          getPnSpecList: "/vehicleproduct/tmsVehicleProduct/getPnSpecList"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getCity();
      this.getPnSpecList();
    },
    methods: {
      changeGetpalletsNum() {
        if (!this.model.pn || !this.model.spec || !this.model.shipNumber) {
          return
        }
        console.log("22222");
        postAction(this.url.getPalletsNum, this.model).then(res => {
          if (res.success) {
            this.model.palletsNum = res.result
          }
        })
      },
      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) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },

      getCity(){
        getAction(this.url.getCity).then(res => {
          if (res.success) {
            this.cityList = res.result;
            this.pickUpHubList = res.result;
          }
        })
      },
      pickUpHubSelect(){
        let pickUpHub = this.pickUpHubList.filter(x=>x.includes(this.model.pickUpHub))[0].split('&-')[0]
        let deliveryAddress = this.pickUpHubList.filter(x=>x.includes(this.model.pickUpHub))[0].split('&-')[1]
        this.model.pickUpHub = pickUpHub
        this.model.deliveryAddress = deliveryAddress
      },
      pickUpHubChange(val){
        this.pickUpHubList = this.cityList.filter(x => x.includes(val));
      },

      getPnSpecList(){
        getAction(this.url.getPnSpecList).then(res => {
          if (res.success) {
            this.pnSpecList = res.result;
            this.pnList = res.result;
          }
        })
      },
      pnSelect(){
        let pn = this.pnList.filter(x=>x.includes(this.model.pn))[0].split('&-')[0]
        let spec = this.pnList.filter(x=>x.includes(this.model.pn))[0].split('&-')[1]
        this.model.pn = pn
        this.model.spec = spec
      },
      pnChange(val){
        this.pnList = this.pnSpecList.filter(x => x.includes(val));
      },
    }
  }
</script>