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

    <div v-if="show == 0" class="left-container">
      <BillList ref="billList" billType="3" @rowClick="billClick"></BillList>
      <div class="right-content">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="mini" label-width="68px">
          <el-form-item label="货物名称" prop="cargoName" class="search-item">
            <el-input v-model="queryParams.cargoName" placeholder="请输入货物名称" class="search-input" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="货主名称" prop="custName" class="search-item">
            <el-input v-model="queryParams.custName" placeholder="请输入货主名称" class="search-input" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="仓库名称" prop="stockCode" class="search-item">
            <el-select v-model="queryParams.stockCode" filterable placeholder="请选择">
              <el-option v-for="item in warehouses" :key="item.stockCode" :label="item.stockName" :value="item.stockCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="仓位名称" prop="stockName" class="search-item">
            <el-input v-model="queryParams.stockName" placeholder="请输入库场名称" class="search-input" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['jxc:ckcargo:add']">新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="showView"
              v-hasPermi="['jxc:ckcargo:edit']">查看
            </el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="tableData" height="50vh" @sort-change="handleSortChange"
          @selection-change="handleSelectionChange" row-key="id" default-expand-all highlight-current-row
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
          <el-table-column type="selection" align="center" />
          <!--        <el-table-column label="出库单据" align="center" prop="outBill" sortable='custom' width="100"/>-->
          <el-table-column label="仓库名称" min-width="150" align="center" prop="stockCode" sortable='custom'>
            <template slot-scope="scope">
              <WarehouseName :stockCode="scope.row.stockCode" />
            </template>
          </el-table-column>
          <el-table-column label="仓位名称" align="center" min-width="80" prop="stockName" sortable='custom' />
          <el-table-column label="出入库类型" align="center" prop="inoutType" min-width="80" sortable='custom' />
          <el-table-column label="货物名称" align="center" prop="cargoName" min-width="80" sortable='custom' />
          <el-table-column label="货主名称" align="center" prop="custName" min-width="150" sortable='custom' />
          <el-table-column label="货物件数" align="center" prop="cargoNum" min-width="70" sortable='custom' />
          <el-table-column label="货物重量" align="center" prop="cargoWt" min-width="70" sortable='custom' />
          <el-table-column label="货物体积" align="center" prop="cargoVol" min-width="70" sortable='custom' />

          <el-table-column label="操作" fixed="right" width="200" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button v-if="!scope.row.isChildren" size="mini" type="text" icon="el-icon-edit"
                @click="showView(scope.row)" v-hasPermi="['jxc:ckcargo:edit']">查看
              </el-button>
              <el-button v-if="!scope.row.isChildren" size="mini" type="text" icon="el-icon-edit"
                @click="editView(scope.row)" v-hasPermi="['jxc:ckcargo:edit']">修改
              </el-button>
              <el-button v-if="!scope.row.isChildren && scope.row.billStatus == '1'" size="mini" type="text"
                icon="el-icon-edit" @click="recallBill(scope.row)" v-hasPermi="['jxc:ckcargo:edit']">撤回
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </div>
      <!-- 添加或修改仓库出库管理对话框 -->
    <el-dialog :title="title" destroy-on-close :visible.sync="open" width="80%" append-to-body
      :close-on-click-modal="false">
      <Transfer v-if="open" :readonly="readonly"  @close="handleClose"
        @refresh="refreshList"></Transfer>
    </el-dialog>
    </div>

    <!-- 添加或修改货物转场对话框 -->
    <div v-if="show == 1">
      <div style="display: flex;justify-content: space-between;align-items: center;">
        <div class="trans-title">转出库场</div>
        <el-button @click="cancel" style="right: 3%;position:absolute;">返 回</el-button>
      </div>
      <el-container>
        <el-header style="height: 260px; border: 2px solid #eee">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="8">
                <el-form-item label="货主名称" prop="custId">
                  <el-select v-model="form.custId" filterable placeholder="请输入货主名称" style="width: 100%" clearable
                    @change="setCustDetail">
                    <el-option v-for="item in custList" :key="item.id" :label="item.custName" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单据日期" prop="billDate">
                  <el-date-picker clearable v-model="form.billDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="请选择单据日期" style="width: 100%">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单据编号" prop="billNumber">
                  <bill-number-input ref="billInput" v-model="form.billNumber" type="ZC"></bill-number-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="货物名称" prop="cargoId">
                  <el-select v-model="form.cargoId" filterable placeholder="请输入货物名称" style="width: 100%" clearable
                    @change="setCargoDetail">
                    <el-option v-for="item in cargoList" :key="item.id" :label="item.cargoName" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="库场名称" prop="stockId">
                  <el-select v-model="form.stockId" filterable placeholder="请输入库场名称" style="width: 100%" clearable
                    @change="setOutStockDetail">
                    <el-option v-for="item in stockList" :key="item.stockId"
                      :label="warehouseName(item) + '-' + item.stockName" :value="item.stockId">
                      <WarehouseName :stockCode="item.stockCode"></WarehouseName><span>-{{ item.stockName }}</span>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="出库单据" prop="outBillNumber">
                  <el-select v-model="form.outBillNumber" filterable placeholder="请输入出库单据" style="width: 100%" clearable
                    @change="setNowStockNum">
                    <el-option v-for="item in listMap[outForm.stockId]" :key="item.billNumber" :label="item.billNumber"
                      :value="item.billNumber">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>

              <el-col :span="8">
                <el-form-item label="当前数量" prop="nowStockNum">
                  <el-input v-model="form.nowStockNum" disabled placeholder="请输入货物件数" style="width: 100%" />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="当前重量(吨)" prop="nowStockWt">
                  <el-input v-model="form.nowStockWt" disabled placeholder="请输入货物重量" style="width: 100%" />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="当前体积" prop="nowStockVol">
                  <el-input v-model="form.nowStockVol" disabled placeholder="请输入货物体积" style="width: 100%" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="当前价值" prop="nowCargoValue">
                  <el-input v-model="form.nowCargoValue" disabled style="width: 100%" />
                </el-form-item>
              </el-col>
            </el-row>


          </el-form>
        </el-header>

        <div class="stock-content">
          <div style="display: flex;align-items: center;">
            <div class="trans-title">转入库场</div>
            <el-button style="margin-left: 20px;" v-if="!tableReadonly" type="success" plain
              @click="addStockTrans">添加转入库场</el-button>
          </div>
          <el-form ref="listInForm" style="margin-top: 10px;" :model="form" v-enterboard>
            <el-table v-if="!loading" highlight-current-row ref="inTable" :data="form.newCargos" show-summary border
              stripe>
              <el-table-column v-if="!tableReadonly" label="操作" width="80">
                <template slot-scope="scope">
                  <el-button type="primary" size="small"
                    @click="delStockTrans(scope.row, scope.$index)">删除</el-button>
                </template>
              </el-table-column>
              <el-table-column label="库场" prop="stockName">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.stockId" filterable placeholder="请输入库场名称" style="width: 100%" clearable
                    @change="setInStockDetail">
                    <el-option v-for="item in allStockList" :key="item.id"
                      :label="warehouseName(item) + '-' + item.stockName" :value="item.id">
                      <WarehouseName :stockCode="item.stockCode"></WarehouseName><span>-{{ item.stockName }}</span>
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="件数" prop="cargoNum">
                <template slot-scope="scope">
                  <el-form-item prop="cargoNum" class="margin-none">
                    <div v-if="tableReadonly" class="nomal-text">{{ scope.row.cargoNum }}</div>
                    <el-input-number style="width: 100%" controls-position="right" v-else v-model="scope.row.cargoNum"
                      :min="0" :max="getMaxData(scope.row).cargoNum" :precision="2" placeholder="输入件数" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="重量" prop="cargoWt">
                <template slot-scope="scope">
                  <el-form-item class="margin-none" :prop="'newCargos.' + scope.$index + '.cargoWt'"
                    :rules="rules.cargoWt">
                    <div v-if="tableReadonly" class="nomal-text">{{ scope.row.cargoWt }}</div>
                    <el-input-number style="width: 100%" controls-position="right" v-else v-model="scope.row.cargoWt"
                      :min="0" :precision="2" :max="getMaxData(scope.row).cargoWt" placeholder="输入重量" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="体积" prop="cargoVol">
                <template slot-scope="scope">
                  <el-form-item prop="cargoVol" class="margin-none">
                    <div v-if="tableReadonly" class="nomal-text">{{ scope.row.cargoVol }}</div>
                    <el-input-number style="width: 100%" controls-position="right" v-else v-model="scope.row.cargoVol"
                      :min="0" :precision="2" :max="getMaxData(scope.row).cargoVol" placeholder="输入体积" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="价值" prop="cargoValue">
                <template slot-scope="scope">
                  <el-form-item prop="cargoValue" class="margin-none">
                    <div v-if="tableReadonly" class="nomal-text">{{ scope.row.cargoValue }}</div>
                    <el-input-number style="width: 100%" controls-position="right" v-else v-model="scope.row.cargoValue"
                      :min="0" :precision="2" :max="getMaxData(scope.row).cargoValue" placeholder="输入价值" />
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </div>

        <!-- <el-container>
          <el-main style="height: 500px; border: 2px solid #eee ; width: 200px;">

            <el-collapse v-model="activeName">
              <el-collapse-item name="1">
                <template slot="title">
                  <span style="color:#409EFF ; font-size: 18px;">原库场剩余量</span>
                </template>

                <el-form ref="outForm" :model="outForm">
                  <el-col :span="20">
                    <el-form-item label="剩余件数" prop="cargoNum">
                      <el-input disabled v-model="outForm.cargoNum" placeholder="请输入货物件数" style="width: 100%" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="剩余重量(吨)" prop="cargoWt">
                      <el-input disabled v-model="outForm.cargoWt" placeholder="请输入货物重量" style="width: 100%" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="剩余体积(立方米)" prop="cargoVol">
                      <el-input disabled v-model="outForm.cargoVol" placeholder="请输入货物体积" style="width: 100%" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="剩余价值" prop="cargoValue">
                      <el-input disabled v-model="outForm.cargoValue" placeholder="请输入货物体积" style="width: 100%" />
                    </el-form-item>
                  </el-col>

                </el-form>
              </el-collapse-item>
            </el-collapse>
          </el-main>
          <el-main style="height: 500px; border: 2px solid #eee ; width: 200px;margin-left:5px;">


            <el-collapse v-model="activeName">
              <el-collapse-item name="2">
                <template slot="title">
                  <span style="color:#409EFF ; font-size: 18px;">转入库场量</span>
                </template>

                <el-form ref="inForm" :rules="rules" :model="inForm">

                  <el-col :span="20">
                    <el-form-item label="库场名称" prop="inStockName">

                      <el-select v-model="inForm.stockId" filterable placeholder="请输入库场名称" style="width: 100%" clearable
                        @change="setInStockDetail">
                        <el-option v-for="item in allStockList" :key="item.id"
                          :label="warehouseName(item) + '-' + item.stockName" :value="item.id">
                          <WarehouseName :stockCode="item.stockCode"></WarehouseName><span>-{{ item.stockName }}</span>
                        </el-option>
                      </el-select>

                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="货物件数" prop="inCargoNum">
                      <el-input-number v-model="inForm.cargoNum" placeholder="请输入货物件数" style="width: 100%" :min="0"
                        :precision="2" :max="isShow ? Infinity : form.nowStockNum" @change="setInCargoNum" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="货物重量(吨)" prop="inCargoWt">
                      <el-input-number v-model="inForm.cargoWt" placeholder="请输入货物重量" style="width: 100%" :precision="2"
                        :min="0" :max="isShow ? Infinity : form.nowStockWt" @change="setInCargoWt" />
                    </el-form-item>
                  </el-col>


                  <el-col :span="20">
                    <el-form-item label="货物体积(立方米)" prop="inCargoVol">
                      <el-input-number v-model="inForm.cargoVol" placeholder="请输入货物体积" style="width: 100%" :min="0"
                        :precision="2" :max="isShow ? Infinity : form.nowStockVol" @change="setInCargoVol" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="货物价值" prop="cargoValue">
                      <el-input-number v-model="inForm.cargoValue" placeholder="请输入货物价值" style="width: 100%" :min="0"
                        :precision="2" :max="isShow ? Infinity : form.nowCargoValue" @change="setInCargoValue" />
                    </el-form-item>
                  </el-col>


                </el-form>
              </el-collapse-item>
            </el-collapse>
          </el-main>
        </el-container> -->

      </el-container>
      <div slot="footer" style="margin-top: 10px" class="dialog-footer">
        <el-button style="margin-right: 20px;" @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="!isShow && (form.billStatus && form.billStatus == '0')" @click="submitForm(true)"
          style="margin-right: 20px;">确认单据</el-button>
        <el-button type="primary" v-if="!isShow && (form.billStatus && form.billStatus == '1')" @click="recallBill"
          style="margin-right: 20px;">单据撤回</el-button>
        <el-button type="primary" v-if="!isShow && (form.billStatus && form.billStatus == '0')" @click="submitForm(false)"
          style="margin-right: 20px;">保 存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  pageListCkcargo,
  getCkcargo,
  delCkcargo,
  addCkcargo,
  updateCkcargo,
  addCkcargoConfirmOrder
} from "@/api/jxc/ckcargo";
import { listStock } from "@/api/jxc/stock";
import { listCust } from "@/api/jxc/cust";
import { listCargo } from "@/api/jxc/cargo";
import { listVessel, } from "@/api/jxc/vessel";
import { listBmvoyage } from "@/api/jxc/bmvoyage";
import { stockNumList, warehouseList } from '@/api/jxc/ckstock'
import { getTransferInfo, addTransfer, transferList, recallTransfer } from '@/api/jxc/ckbill'
import { listWarehouse } from "@/api/jxc/warehouse";
import BillList from '@/views/jxc/ckwarehouse/components/BillList';
import Transfer from './components/Transfer';

export default {
  name: "Cktransfer",
  dicts: ['sys_business_nature', 'sys_trade_nature', 'sys_inbound_outbound_type'],
  components: {
    BillList,
    Transfer,
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },

      tableData: [],

      activeName: ['1', '2', '3'],
      inCargoWt: null,
      inCargoVol: null,
      inCargoNum: null,
      outCargoWt: null,
      outCargoVol: null,
      outCargoNum: null,


      show: 0,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单据时间
      queryTime: [],
      // 船舶航次表格数据
      bmvoyageList: [],
      // 船舱表格数据
      vesselList: [],
      // 客户表格数据
      custList: [],
      // 货物表格数据
      cargoList: [],
      // 仓库表格数据
      stockList: [],
      warehouses: [],
      allStockList: [],
      listMap: {},
      // 货物转场表格数据
      ckcargoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billId: null,
        billDate: null,
        billNumber: null,
        inoutType: null,
        voyageId: null,
        voyageNo: null,
        vesselId: null,
        vesselName: null,
        cargoId: null,
        hsCode: null,
        cargoName: null,
        billNo: null,
        cargoSpec: null,
        originCountry: null,
        custId: null,
        custName: null,
        opType: null,
        tradType: null,
        cargoNum: null,
        cargoWt: null,
        cargoVol: null,
        stockId: null,
        stockCode: null,
        stockName: null,
        inStockId: null,
        inStockCode: null,
        inStockName: null,
        outStockId: null,
        outStockCode: null,
        outStockName: null,
        beginDate: null,
        endDate: null,
      },
      isShow: false,
      // 表单参数
      form: {
        outBillNumber: '',
        stockId: '',
        billStatus: '0',
        nowStockNum: 0,
        nowStockWt: 0,
        nowStockVol: 0,
        nowCargoValue: 0,
        newCargos: [],
      },
      outForm: {
        outBillNumber: '',
        stockId: '',
        nowStockNum: 0,
        nowStockWt: 0,
        nowStockVol: 0,
        nowCargoValue: 0,
      },
      inForm: {
        outBillNumber: '',
        stockId: '',
        nowStockNum: 0,
        nowStockWt: 0,
        nowStockVol: 0,
        nowCargoValue: 0,
      },
      // 表单校验
      rules: {
        billDate: [
          { required: true, message: "单据日期不能为空", trigger: "blur" }
        ],
        billNumber: [
          { required: true, message: "单据编号不能为空", trigger: "blur" }
        ],
        cargoId: [
          { required: true, message: "货物名称不能为空", trigger: "blur" }
        ],
        custId: [
          { required: true, message: "货主名称不能为空", trigger: "blur" }
        ],
        stockId: [
          { required: true, message: "库场不能为空", trigger: "blur" }
        ],
        outBillNumber: [
          { required: true, message: "单据编号不能为空", trigger: "blur" }
        ],
      },
      // 选中的单据
      selectedBill: {},

    };
  },
  computed: {
    tableReadonly() {
      return false;
    }
  },
  created() {
    this.getCustList();
    this.getCargoList();
    this.getVesselList();
    this.getBmvoyage();
    this.getAllStockList();
    listWarehouse({}).then(response => {
      this.warehouses = response.data;
    });

  },
  methods: {
    // 点击单据
    billClick(row) {
      const isChange = this.selectedBill.id !== row.id;
      this.selectedBill = row || {};
      this.queryParams.billNumber = this.selectedBill.billNumber;
      // 切换的时候重新搜索，否则刷新当前
      isChange ? this.handleQuery() : this.getList();
    },
    // 刷新列表
    refreshList() {
      this.queryParams.billNumber = '';
      this.$refs.billList.getList();
    },
    warehouseName(row) {
      const stock = this.$store.getters.warehouseData[row.stockCode] || {};
      return stock.stockName;
    },
    //航次
    setBmvoyageDetail(voyageId) {
      console.log(voyageId);

      this.bmvoyageList.forEach(ele => {
        if (ele.id == voyageId) {
          this.form.voyageNo = ele.voyageNo;
          this.form.vesselName = ele.vesselName;
          this.form.deptId = ele.deptId;
          this.form.vesselId = ele.vesselId;
          this.form.netTon = ele.netTon;
          this.form.opType = ele.opType;


        }
      })
    },
    getBmvoyage() {
      let param = {};
      listBmvoyage(param).then(response => {
        this.bmvoyageList = response.data;
      }).catch(e => {
      });
    },

    //船舶
    setVesselDetail(vesselId) {
      console.log(vesselId);
      this.vesselList.forEach(ele => {
        if (ele.id == vesselId) {
          console.log("aa:", ele.voyageNo);
          this.form.vesselName = ele.vesselName;

        }
      })
    },

    getVesselList() {
      let param = {};
      listVessel(param).then(response => {
        this.vesselList = response.data;
      }).catch(e => {
      });
    },

    //货物
    setCargoDetail(cargoId) {
      if (!this.form.custId) {
        this.form.cargoId = '';
        this.$modal.alertError("请先选择客户信息");
        return
      }
      this.form.stockId = '';
      this.form.outBillNumber = '';
      this.form.nowStockNum = 0;
      this.form.nowStockWt = 0;
      this.form.nowStockVol = 0;
      this.form.nowCargoValue = 0;
      this.$set(this.outForm, "cargoId", cargoId);
      this.$set(this.outForm, "stockId", "");
      this.$set(this.outForm, "outBillNumber", "");
      this.outForm.billNumber = "";
      this.outForm.cargoNum = 0;
      this.outForm.cargoWt = 0;
      this.outForm.cargoVol = 0;
      this.outForm.cargoValue = 0;
      this.cargoList.forEach(ele => {
        if (ele.id == cargoId) {
          this.form.cargoName = ele.cargoName;
          this.form.hsCode = ele.hsCode;
          this.form.cargoSpec = ele.cargoSpec;
          this.form.originCountry = ele.originCountry;
        }
      })
      this.getStockList(cargoId);
    },

    getCargoList() {
      let param = {};
      listCargo(param).then(response => {
        this.cargoList = response.data;
      }).catch(e => {
      });
    },

    //客户
    setCustDetail(custId) {
      this.form.stockId = '';
      this.form.cargoId = '';
      this.form.outBillNumber = '';
      this.form.nowStockNum = 0;
      this.form.nowStockWt = 0;
      this.form.nowStockVol = 0;
      this.form.nowCargoValue = 0;
      this.$set(this.outForm, "cargoId", "");
      this.$set(this.outForm, "stockId", "");
      this.$set(this.outForm, "outBillNumber", "");
      this.outForm.billNumber = "";
      this.outForm.cargoNum = 0;
      this.outForm.cargoWt = 0;
      this.outForm.cargoVol = 0;
      this.outForm.cargoValue = 0;
      this.custList.forEach(ele => {
        if (ele.id == custId) {
          this.form.custName = ele.custName;
          this.form.custCode = ele.custCode;

        }
      })
    },

    getCustList() {
      let param = {};
      listCust(param).then(response => {
        this.custList = response.data;
      }).catch(e => {
      });
    },


    //仓库
    setInStockDetail(stockId) {
      if (stockId == this.form.stockId) {
        stockId = '';
        this.$modal.alertError("入库仓库不能与出库仓库相同");
        return
      }
      this.allStockList.forEach(ele => {
        if (ele.id == stockId) {
          this.inForm.stockName = ele.stockName;
          this.inForm.stockCode = ele.stockCode;
        }
      })
    },
    //入库数量
    setInCargoNum() {
      this.outForm.cargoNum = this.form.nowStockNum - this.inForm.cargoNum
    },
    setInCargoWt() {
      this.outForm.cargoWt = this.form.nowStockWt - this.inForm.cargoWt
    },
    setInCargoVol() {
      this.outForm.cargoVol = this.form.nowStockVol - this.inForm.cargoVol
    },
    setInCargoValue() {
      this.outForm.cargoValue = this.form.nowCargoValue - this.inForm.cargoValue
    },
    //仓库
    setOutStockDetail(stockId) {
      if (stockId == this.inForm.stockId) {
        stockId = '';
        this.$modal.alertError("出库仓库不能与入库仓库相同");
        return
      }
      this.form.outBillNumber = '';
      this.form.nowStockNum = 0;
      this.form.nowStockWt = 0;
      this.form.nowStockVol = 0;
      this.form.nowCargoValue = 0;
      this.$set(this.outForm, "billNumber", "");
      this.outForm.billNumber = "";
      this.outForm.cargoNum = 0;
      this.outForm.cargoWt = 0;
      this.outForm.cargoVol = 0;
      this.outForm.cargoValue = 0;
      this.stockList.forEach(ele => {
        if (ele.stockId == stockId) {
          this.outForm.stockName = ele.stockName;
          this.outForm.stockCode = ele.stockCode;
          this.outForm.stockId = stockId;
        }
      })
    },
    //设置现有库存
    setNowStockNum(billNumber) {
      this.listMap[this.outForm.stockId].forEach(ele => {
        if (ele.billNumber == billNumber) {
          this.$set(this.form, "nowStockNum", ele.cargoNum);
          this.$set(this.form, "nowStockWt", ele.cargoWt);
          this.$set(this.form, "nowStockVol", ele.cargoVol);
          this.$set(this.form, "nowCargoValue", ele.cargoValue);
          this.outForm.billNumber = ele.billNumber;
          this.outForm.cargoNum = ele.cargoNum;
          this.outForm.cargoWt = ele.cargoWt;
          this.outForm.cargoVol = ele.cargoVol;
          this.outForm.cargoValue = ele.cargoValue;
          this.inForm.cargoNum = 0;
          this.inForm.cargoWt = 0;
          this.inForm.cargoVol = 0;
          this.inForm.cargoValue = 0;
        }
      })
    },
    getStockList(cargoId) {
      let param = {
        custId: this.form.custId,
        cargoId: cargoId,
      }
      warehouseList(param).then(response => {
        this.stockList = response.stockList
        this.listMap = response.listMap
      }).catch(e => {
      })
    },
    getAllStockList(cargoId) {
      let param = {
        custId: this.form.custId,
        cargoId: cargoId,
      }
      stockNumList(param).then(response => {
        this.allStockList = response.data
      }).catch(e => {
      })
    },


    /** 查询货物转场列表 */
    getList() {
      this.loading = true;
      if (this.queryTime != null) {
        this.queryParams.beginDate = this.queryTime[0];
        this.queryParams.endDate = this.queryTime[1];
      } else {
        this.queryParams.beginDate = null;
        this.queryParams.endDate = null;
      }
      transferList(this.queryParams).then(response => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.show = 0;

      this.reset();
    },
    // 表单重置
    reset() {
      this.stockList = []
      this.outForm = {
        cargoNum: 0,
        cargoWt: 0,
        cargoVol: 0,
        cargoValue: 0,

      };
      this.inForm = {
        stockId: '',
        cargoNum: 0,
        cargoWt: 0,
        cargoVol: 0,
        cargoValue: 0
      };
      this.form = {
        id: null,
        billStatus: '0',
        outBillNumber: '',
        nowStockNum: 0,
        nowStockWt: 0,
        nowStockVol: 0,
        nowCargoValue: 0,
        outCargoNum: 0,
        outCargoWt: 0,
        outCargoVol: 0,
        outCargoValue: 0,
        inCargoNum: 0,
        inCargoWt: 0,
        inCargoVol: 0,
        inCargoValue: 0,
        billId: null,
        billDate: null,
        billNumber: null,
        inoutType: null,
        voyageId: null,
        voyageNo: null,
        vesselId: null,
        vesselName: null,
        cargoId: null,
        hsCode: null,
        cargoName: null,
        billNo: null,
        cargoSpec: null,
        originCountry: null,
        custId: null,
        custName: null,
        opType: null,
        tradType: null,
        cargoNum: null,
        cargoWt: null,
        cargoVol: null,
        stockId: null,
        stockCode: null,
        stockName: null,
        remark: null,
        inStockId: null,
        inStockCode: null,
        inStockName: null,
        outStockId: null,
        outStockCode: null,
        outStockName: null,
        newCargos: [],
      };

      this.resetForm("form");
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryTime = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.names = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isShow = false;
      this.show = 1;

      this.reset();
      this.title = "添加货物转场";
    },

    showView(row) {
      this.isShow = true;
      this.handleUpdate(row)
    },
    editView(row) {
      this.isShow = false;
      this.handleUpdate(row)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.stockList = this.allStockList
      this.loading = true;
      getTransferInfo(id).then(response => {
        this.form = response.data
        this.form.stockId = response.data.oldCargo.stockId
        this.form.cargoId = response.data.oldCargo.cargoId
        this.form.outBillNumber = response.data.oldCargo.billNumber
        this.form.nowStockNum = response.data.oldCargo.cargoNum
        this.form.nowStockWt = response.data.oldCargo.cargoWt
        this.form.nowStockVol = response.data.oldCargo.cargoVol
        this.form.nowCargoValue = response.data.oldCargo.cargoValue
        // this.outForm = response.data.oldCargo
        // this.inForm = response.data.newCargo
        this.show = 1
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      })
    },
    getMaxData(row) {
      return {
        cargoNum: Infinity,
        cargoWt: Infinity,
        cargoValue: Infinity,
        cargoVol: Infinity
      };
    },
    delStockTrans(row, index) {
      this.form.newCargos.splice(index, 1);
    },
    // 添加转入库场
    addStockTrans() {
      this.form.newCargos.push({
        stockId: '',
        stockCode: '',
        stockName: '',
        cargoNum: 0,
        cargoWt: 0,
        cargoVol: 0,
        cargoValue: 0,
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.newCargos || this.form.newCargos.length === 0) {
            this.$modal.alertError("转入库场信息不能为空");
            return;
          }

          for (let index = 0; index < this.form.newCargos.length; index++) {
            const item = this.form.newCargos[index];
            if (!item.stockId) {
              this.$modal.alertError("转入库场信息不能为空");
              return;
            }
            if (!item.cargoNum && !item.cargoWt && !item.cargoVol) {
              this.$modal.alertError("货物数量、体积、重量 必须有一项大于0");
              return;
            }
          }

          this.form.submitStatus = status;
          // this.form.oldCargo =
          if (status) {
            this.$confirm(`确定提交当前单据吗？`, '温馨提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(() => {
              addTransfer(this.form).then(response => {
                this.$modal.msgSuccess("提交成功");
                this.reset()
                this.show = 0
                this.refreshList();
              });
            });
          } else {
            addTransfer(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.reset()
              this.show = 0
              this.refreshList();
            });
          }
        }
      });
    },    /** 撤回按钮 */
    recallBill(row) {
      const id = row.id || this.form.billId;
      this.$confirm(`确定撤回当前单据吗？`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        recallTransfer(id).then(response => {
          this.$modal.msgSuccess("撤回成功");
          this.reset()
          this.show = 0
          this.refreshList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.id || this.names;
      this.$modal.confirm('是否确认删除货物转场为"' + names + '"的数据项？').then(function () {
        return delCkcargo(ids);
      }).then(() => {
        this.refreshList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jxc/ckcargo/export', {
        ...this.queryParams
      }, `货物转场_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  overflow-y: scroll;
}

.left-container {
  width: 100%;
  height: 100%;
  display: flex;
}

.right-content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.search-item {
  margin-bottom: 6px;
}

.table {
  flex: 1;
}

.search-input {
  max-width: 150px;
}

.trans-title {
  color: #409EFF;
  font-size: large;
  font-weight: 500;
  line-height: 50px;
}

.stock-content {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  // height: 68vh;
}

.margin-none {
  margin: 0;
}
</style>