<template>
    <el-dialog :title="$t('批量提货')" visible :before-close="closeDialog" :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules">                
            <el-tabs v-model="current" type="card">
                <el-tab-pane :label="$t('全部提货')" name="createBatch">
                    <el-form-item :label="$t('订单号') + '/' + this.$t('提单号')" prop="orderId">
                        <el-input v-model="form.orderId" :placeholder="$t('例：NA2200001L')"></el-input>
                        <div>{{$t('注：单号之间使用逗号或空格隔开')}}</div>
                    </el-form-item>
                    <el-form-item :label="$t('提货时间')" prop="pickTime">
                        <el-date-picker v-model="form.pickTime" placeholder="" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-form-item>
                </el-tab-pane>
                <el-tab-pane :label="$t('分批提货')" name="createSplitBatch">
                    <el-form-item :label="$t('订单号') + '/' + this.$t('提单号')" prop="orderId">
                        <el-input v-model="form.orderId" :placeholder="$t('例：NA2200001L')" class="w-200 mr-10"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="getPickUpListByOrderNo"></el-button>
                        <div>{{$t('注：单号之间使用逗号或空格隔开')}}</div>
                    </el-form-item>
                    <el-form-item :label="$t('提货时间')" prop="pickTime">
                        <el-date-picker v-model="form.pickTime" placeholder="" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-form-item>
                    <el-form-item label="">
                        <el-table :data="orderList">
                          <el-table-column :label="$t('提单号')" prop="tidanNo"></el-table-column>
                          <el-table-column :label="$t('提货数量')" prop="tidanNo">
                                <template slot-scope="{row}">
                                    <el-input v-model.number="row.pickNum" placeholder=""></el-input>
                                </template>
                          </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-tab-pane>
            </el-tabs>
            <el-form-item label="">
                <el-button type="primary" @click="submit">{{$t('提交')}}</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import {createBatch, createSplitBatch, getPickUpListByOrderNo} from '@/api/ecw/orderPickup'

export default {
    props:{
        
    },
    data(){
        return {
            current: 'createBatch',
            show: false,
            form:{
                orderId: '',
                pickTime: '',
                pickType: 1
            },
            rules:{
                orderId: [{required: true, message: this.$t('请填写单号')}],
                pickTime: [{required: true, message: this.$t('请选择时间')}]
            },
            orderList:[]
        }
    },
    computed:{
        
    },
    created(){
        
    },
    methods:{
        submit(){
            this.$refs.form.validate().then(() => {
                if(this.form.orderId == '' || this.form.pickTime==''){
                    return this.$message.error(this.$t('请填写订单号和提货时间'))
                }
                let orderIdArr = this.form.orderId.replace(/,/g, ' ').split(' ').filter(item => item !='')
                let data = []
                
                // 分批提货
                if(this.current == 'createSplitBatch'){
                    data = this.orderList.map(item => {
                        item.pickType = 1
                        item.pickTime = this.form.pickTime
                        return item
                    })
                }
                // 批量提货
                else{
                    for(let orderId of orderIdArr){
                        data.push({
                            pickType: 1, // 1字体，2送货上门
                            orderId,
                            pickTime: this.form.pickTime
                        })
                    }
                }

                ({createBatch, createSplitBatch}[this.current])(data).then(res => {
                    this.$message.success(this.$t("操作成功"));
                    this.$emit('success')
                })
            })
        },
        getPickUpListByOrderNo(){
            getPickUpListByOrderNo({orderIds: this.form.orderId}).then(res => {
                this.orderList = res.data
            })
        },
        closeDialog(){
            this.show = false
            this.$emit('close');
        }
    }
}
</script>