<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="日期筛选">
        <dict-selector defaultable :type="DICT_TYPE.ECW_BEGINTIME_TYPE_ENDTIME" v-model="dateFilterType" class="w-100 mr-10" />
        <el-date-picker v-model="dateFilter" type="daterange" range-separator="-"  start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="始发地" prop="startWarehouseId">
        <el-select v-model="queryParams.startWarehouseId" placeholder="请选择始发地">
          <el-option v-for="item in expoerCityList" :label="item.titleZh" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目的地" prop="destWarehouseId">
        <el-select v-model="queryParams.destWarehouseId" placeholder="请选择目的地">
          <el-option v-for="item in importCityList" :label="item.titleZh" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运输方式" prop="transportId">
        <dict-selector :type="DICT_TYPE.ECW_TRANSPORT_TYPE" v-model="queryParams.transportId" formatter="number" />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品名" prop="prodId">
        <product-selector v-model="queryParams.prodId" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="控货" prop="isCargoControl">
        <dict-selector v-model="queryParams.isCargoControl" :type="DICT_TYPE.INFRA_BOOLEAN_STRING" fomatter="bool" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <dict-selector :type="DICT_TYPE.PAY_ORDER_STATUS" v-model="queryParams.status"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
       <el-form-item label="提单号" prop="tidanNo">
        <el-input v-model="queryParams.tidanNo" placeholder="提单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发货人" prop="consignorId">
        <el-input v-model="queryParams.consignorId" placeholder="发货人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="收货人" prop="consigneeId">
        <el-input v-model="queryParams.consigneeId" placeholder="收货人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="报关方式" prop="customsType">
        <dict-selector :type="DICT_TYPE.ECW_CUSTOMS_TYPE" v-model="queryParams.customsType" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <el-form-item label="唛头" prop="marks">
        <el-input v-model="queryParams.marks" placeholder="唛头" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['ecw:order:export']">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="2">
        <el-button type="primary" plain icon="el-icon-setting" size="mini" :disabled="multiple">批量复核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    
    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="订单编号" width="120px" align="center" prop="orderNo" >
        <template slot-scope="scope">
          <span>{{ scope.row.orderNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="唛头" align="center" prop="marks" />
      <el-table-column label="箱数" align="center" prop="sumNum">
        <template slot-scope="{row}">{{row.totalNum}}</template>
      </el-table-column>
      <el-table-column label="已放箱数" align="center" prop="sumWeight">
        <template slot-scope="{row}">{{row.releaseNum}}</template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="提单号" align="center" prop="status">
        <template slot-scope="scope">
          {{scope.row.tidanNo}}
        </template>
      </el-table-column>
      <el-table-column label="放货人" align="center" prop="sumWeight">
        <template slot-scope="{row}">{{row.cargoControlName}}</template>
      </el-table-column>
      <el-table-column label="控货状态" align="center" prop="transportId">
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.CONTROL_GOODS_STATUS" :value="row.cargoControlStatus" />
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button type="text"  size="mini" @click="showReleaseOrderId=scope.row.orderId">放货</el-button>
          
          <el-button type="text"  size="mini" @click="showEditOrderId=scope.row.orderId" >修改</el-button>
          <el-button type="text"  size="mini" >取消</el-button>
          <el-button type="text"  size="mini" @click="review(scope.row)" >放货复核</el-button>
          <el-button type="text"  size="mini" @click="$router.push('cargo_control/detail?id=' + scope.row.orderId)" >查看</el-button>
          <el-button type="text"  size="mini" @click="showTransferCargoOrderId=scope.row.orderId" >调货</el-button>
          <el-button type="text"  size="mini" >反复核</el-button>
          <el-button type="text"  size="mini" @click="showTransferOrder = scope.row" >控货权转移</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows"
      @pagination="getList" />

    <transfer v-if="showTransferOrder" v-bind="showTransferOrder" @close="showTransferOrder=null" />
    <release  v-if="showReleaseOrderId"  :order-id="showReleaseOrderId"  @close="showReleaseOrderId=null" />
    <release  v-if="showEditOrderId"     :order-id="showEditOrderId"     @close="showEditOrderId=null" />
    <transfer-cargo v-if="showTransferCargoOrderId"     :order-id="showTransferCargoOrderId"     @close="showTransferCargoOrderId=null" />
  </div>
</template>

<script>
import Selector from '@/components/Selector'
import ProductSelector from '@/components/ProductSelector'
import { getProductAttrList } from '@/api/ecw/productAttr'
import CustomerSelector from '@/components/CustomerSelector'
import { getTradeCityList } from '@/api/ecw/region'
import Transfer from '@/views/ecw/order/components/Transfer'
import Release from './components/Release'
import CargoControlEdit from './components/CargoControlEdit'
import { getCargoControlOrderPage } from "@/api/ecw/orderCargoControl"
import TransferCargo from '@/views/ecw/order/components/TransferCargo'


export default {
  name: "OrderCargonControl",
  components: {
    CustomerSelector, ProductSelector, Selector, Transfer, Release,CargoControlEdit, TransferCargo
  },
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表
      list: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        page: 1,
        rows: 10
      },
      tradeCityList: [],
      productAttrList: [],  // 商品属性
      molecule: '', //重货比分子
      denominator: '', //重货比分母
      dateFilterType: '', //日期筛选类别
      dateFilter: [], //筛选日期
      showTransferOrder: null, // 控制权转移操作订单
      showReleaseOrderId: null, // 放货ID
      showEditOrderId: null, // 修改ID
      showTransferCargoOrderId: null, // 调货ID
    };
  },
  computed: {
    expoerCityList() {
      return this.tradeCityList.filter(item => item.type == 2)
    },
    importCityList() {
      return this.tradeCityList.filter(item => item.type == 1)
    },
    combinedQueryParams(){
        let timeParams = {}
        if(this.dateFilterType && this.dateFilter){
          timeParams['begin' + this.dateFilterType] = this.dateFilter[0]
          timeParams['end' + this.dateFilterType] = this.dateFilter[1]
        }
        return Object.assign({}, this.queryParams, timeParams)
    }
  },
  created() {
    this.getList();
    getProductAttrList().then(res => this.productAttrList = res.data)
    getTradeCityList().then(res => this.tradeCityList = res.data)
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getCargoControlOrderPage(this.combinedQueryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      // 执行导出
      this.$modal.confirm('是否确认导出所有订单数据项?').then(() => {
        this.exportLoading = true;
        return exportOrderExcel(this.combinedQueryParams);
      }).then(response => {
        this.$download.excel(response, '${table.classComment}.xls');
        this.exportLoading = false;
      }).catch(() => { });
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 复核
    review(row){
        this.$confirm('已核实放货信息，确认放货？')
            .then(res => {
                this.$alert('// TODO ')
            })
    }
  }
};
</script>
