<template>
  <my-page>
    <!-- 搜索部分 -->
    <template slot="header">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="mini" label-width="68px">
        <el-form-item label="单据编号" prop="billNumber" class="search-item">
          <el-input v-model="billQuery.billNumber" class="search-input" placeholder="请输入单据编号" clearable
                    @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="货物名称" prop="cargoName" class="search-item">
          <el-input v-model="billQuery.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="billQuery.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="billQuery.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="billQuery.stockName" placeholder="请输入库场名称" class="search-input" clearable
                    @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="单据日期" prop="queryTimeTwo" class="search-item">
          <el-date-picker clearable v-model="queryTimeTwo" style="width: 200px;" size="mini"
                          :picker-options="pickerOptions" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期"
                          type="daterange" value-format="yyyy-MM-dd" placeholder="请选择单据日期">
          </el-date-picker>
        </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>
    </template>
    <!-- 单据部分 -->
    <template slot="left">
      <BillList ref="billList" billType="3" :params="billQuery" @rowClick="billClick"></BillList>
    </template>
    <!-- 列表部分 -->
    <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="60vh" @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>
          <el-button v-if="!scope.row.isChildren && scope.row.billStatus == '0'" size="mini" type="text"
                     icon="el-icon-edit" @click="deleteBill(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" />

    <!-- 添加或修改仓库出库管理对话框 -->
    <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" :transferId="editId" @close="handleClose"
                @refresh="refreshList"></Transfer>
    </el-dialog>

  </my-page>
</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,deleteBill } 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: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 查看编辑的ID
      editId: '',
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单据时间
      queryTime: [],
      queryTimeTwo: [],
      // 客户表格数据
      custList: [],
      warehouses: [],
      // 弹出层标题
      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,
      },
      billQuery: {
        billNumber: null,
        cargoName: null,
        stockCode: null,
        stockName: null,
        customsNo: null,
        listNumber: null,
        beginDate: null,
        endDate: null,
        custName: null,
      },
      // 选中的单据
      selectedBill: {},
      // 是否点查看
      readonly: false,
    };
  },
  created() {
    // this.getCustList();
    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();
      if (isChange) {
        this.queryParams.pageNum = 1;
      }
      if (this.queryParams.billNumber) {
        this.getList();
      } else {
        this.tableData = [];
      }
    },
    // 刷新列表
    refreshList() {
      this.queryParams.billNumber = '';
      if (this.queryTimeTwo != null) {
        this.billQuery.beginDate = this.queryTimeTwo[0];
        this.billQuery.endDate = this.queryTimeTwo[1];
      } else {
        this.billQuery.beginDate = null;
        this.billQuery.endDate = null;
      }
      this.$nextTick(() => {
        this.$refs.billList.getList();
      })
    },
    handleClose() {
      this.open = false;
    },
    // getCustList() {
    //   let param = {};
    //   listCust(param).then(response => {
    //     this.custList = 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
      })
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      //this.getList();
      this.refreshList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryTime = [];
      this.queryTimeTwo = [];
      this.billQuery = {
        billNumber: null,
        cargoName: null,
        stockCode: null,
        stockName: null,
        customsNo: null,
        listNumber: null,
        beginDateTwo: null,
        endDateTwo: null,
        custName: null,
      }
      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.title = "新增货物转场单据";
      this.editId = '';
      this.readonly = false;
      this.open = true;
    },
    showView(row) {
      this.title = "查看货物转场单据";
      this.readonly = true;
      this.handleUpdate(row)
    },
    editView(row) {
      this.title = "编辑货物转场单据";
      this.readonly = false;
      this.handleUpdate(row)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids[0];
      this.editId = id;
      this.open = true;
    },
    /** 撤回按钮 */
    recallBill(row) {
      const id = row.id;
      this.$confirm(`确定撤回当前单据吗？`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        recallTransfer(id).then(response => {
          this.$modal.msgSuccess("撤回成功");
          this.refreshList();
        });
      });
    },
    /** 撤回按钮 */
    deleteBill(row) {
      const id = row.id;
      this.$confirm(`确定删除当前单据吗？`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteBill(id).then(response => {
          this.$modal.msgSuccess("删除成功");
          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;
  display: flex;
}

.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;
}



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

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