<template>
  <div class="add-associated-order">
    <h1 class="title">{{$t('订单号')}}：{{ orderDetails.orderNo }} {{$t('选择订单')}}</h1>
    <el-divider></el-divider>
    <el-form inline label-width="85px" label-position="left">
      <el-form-item :label="$t('订单号')">
        <div class="content">
            <el-input v-model.trim="params.relateOrderNo"></el-input>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button @click="getUnassociatedOrder">{{$t('查询')}}</el-button>
      </el-form-item>
    </el-form>
    <h1 class="title">
      {{$t('未加入关联订单列表')}}
    </h1>
    <el-divider></el-divider>
    <el-table :data="list" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
      ></el-table-column>
      <el-table-column :label="$t('序号')" type="index"></el-table-column>
      <el-table-column :label="$t('订单号')" prop="orderNo"></el-table-column>
      <el-table-column :label="$t('唛头')" prop="marks"></el-table-column>
      <el-table-column :label="$t('货物数据')">
        <template v-slot="{row}">
          <div v-if="row.costVO && row.status !== 5">
            箱数：{{row.costVO.totalNum}}
            {{row.costVO.totalVolume}}m³
            {{row.costVO.totalWeight}}kg
          </div> <div  v-else>
            箱数：{{row.sumNum}}
            {{row.sumVolume}}m³
            {{row.sumWeight}}kg
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('入仓时间')" prop="rucangTime">
        <template v-slot="{row}">
         {{parseTime(row.rucangTime)}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('状态')" prop="statusMsg">
      </el-table-column>
      <el-table-column :label="$t('操作')">
        <template v-slot:default="scope">
          <el-button type="text" size="mini" @click="joinAssociation(scope.row.orderId)">{{$t('加入关联')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="params.page" :limit.sync="params.rows"
                @pagination="getUnassociatedOrder" />
    <el-row style="margin-top: 40px">
      <el-col>
        <el-button type="primary" @click="batchJoin(multipleSelection.map(e => e.orderId))"
                   :disabled="multipleSelection.length === 0">
          {{$t('批量加入关联')}}
        </el-button>
        <el-button @click="$router.push('/order/associated-order/'+ orderId);">
          {{$t('取消')}}
        </el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
//添加关联订单
import {getOrder} from "@/api/ecw/order";
import Template from "@/views/cms/template";
import {getUnGuanlianOrderByOrderId, guanlianCreate, ordeRcreateBatch} from "@/api/ecw/associatedOrder";

export default {
  name: "addAssociatedOrder",
  components: {Template},
  props: {
    orderId: [String, Number]
  },
  data() {
    return {
      orderDetails: {},
      list: [],
      multipleSelection: [],
      params:{
        page:1,
        rows:10
      },
      total:0
    }
  },
  created() {
    getOrder(this.orderId).then(r => {
      if (r.code === 0) {
        this.orderDetails = r.data;
      }
    })
    this.getUnassociatedOrder();
  },
  mounted() {
  },
  methods: {
    getUnassociatedOrder() {
      getUnGuanlianOrderByOrderId({
          orderId: this.orderId,
         ...this.params
      }).then(r => {
        if (r.code === 0) {
          this.list = r.data.list;
          this.total = r.data.total;
        }
      })
    },
    joinAssociation(val) {
      guanlianCreate({orderId: this.orderId, relateOrderId: val}).then(this.operation)
    },
    batchJoin(val) {
      let p = val.map(e => ({orderId:this.orderId,relateOrderId:e}))
      ordeRcreateBatch(p).then(this.operation)
    },
    operation(r) {
      if (r.code === 0) {
        this.$message.success(this.$t('操作成功'))
        this.getUnassociatedOrder();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
}
</script>

<style scoped lang="scss">
.add-associated-order {
  padding: 20px;
  box-sizing: border-box;

  .title {
    font-size: 30px;
    font-weight: 600;
    text-align: left;
  }

  .el-form {
    .content {
      width: 300px;
    }
  }
}
</style>
