<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <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="cargoName">
        <el-input
          v-model="queryParams.cargoName"
          placeholder="请输入运输货物"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="vehicleNumber">
        <el-input
          v-model="queryParams.vehicleNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable  @keyup.enter.native="handleQuery">
          <el-option
            v-for="option in statusOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="承运企业" prop="custName">
          <el-input
            v-model="queryParams.custName"
            placeholder="请输入承运企业"
            clearable
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      <el-form-item label="录入时间">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['carriage_order:finished:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cargoList" @sort-change="handleSortChange"  :summary-method="getSummaries" :show-summary="true"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单号" align="center" prop="orderNo" sortable='custom'/>
      <el-table-column label="订单状态" align="center" prop="status" sortable='custom'>
        <template slot-scope="scope">
          {{ scope.row.status == '1' ? '已完成' : '未完成' }}
        </template>
      </el-table-column>
      <el-table-column label="承运企业" align="center" prop="custName" sortable='custom'/>
      <el-table-column label="司机" align="center" prop="driverName" sortable='custom'/>
      <el-table-column label="车牌号" align="center" prop="vehicleNumber" sortable='custom'/>
      <el-table-column label="运输货物" align="center" prop="cargoName" sortable='custom' />
      <el-table-column label="装货毛重" align="center" prop="grossWeightLoading" sortable='custom' />
      <el-table-column label="装货车重" align="center" prop="pierCarWeight" sortable='custom' />
      <el-table-column label="装货净重" align="center" prop="realLoadWt" sortable='custom' />
      <el-table-column label="卸货毛重" align="center" prop="grossWeightUnloading" sortable='custom' />
      <el-table-column label="卸货车重" align="center" prop="plantCarWeight" sortable='custom' />
      <el-table-column label="卸货净重" align="center" prop="realTransWt" sortable='custom' />
      <el-table-column label="磅差(吨)" prop="poundDifference" align="center" sortable='custom' />
      <el-table-column label="录入时间" prop="inputDate" align="center" sortable='custom' />
      <el-table-column label="备注" align="center" prop="remark" sortable='custom' />
      <el-table-column label="计划发货时间" align="center" prop="departTime" width="180" sortable='custom'>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划到货时间" align="center" prop="arrivalTime" width="180" sortable='custom'>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际发货时间" align="center" prop="realDepartTime" width="180" sortable='custom'>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realDepartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际到货时间" align="center" prop="realArrivalTime" width="180" sortable='custom'>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realArrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="REMARK" sortable='custom'/>
    </el-table>

    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运输装载对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-col :span="12">
          <el-form-item label="订单Id" prop="orderId">
            <el-input v-model="form.orderId" placeholder="请输入订单Id"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="运输计划ID" prop="transId">
            <el-input v-model="form.transId" placeholder="请输入运输计划ID"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="货物ID" prop="orderCargoId">
            <el-input v-model="form.orderCargoId" placeholder="请输入货物ID"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="运输货物" prop="cargoName">
            <el-input v-model="form.cargoName" placeholder="请输入运输货物"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工厂总重" prop="plantWeight">
            <el-input v-model="form.plantWeight" placeholder="请输入工厂总重"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="码头总重" prop="pierWeight">
            <el-input v-model="form.pierWeight" placeholder="请输入码头总重"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="装货实际毛重" prop="grossWeightLoading">
            <el-input v-model="form.grossWeightLoading" placeholder="请输入装货实际毛重"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卸货实际毛重" prop="grossWeightUnloading">
            <el-input v-model="form.grossWeightUnloading" placeholder="请输入卸货实际毛重"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卸货实际净重" prop="netWeightUnloading">
            <el-input v-model="form.netWeightUnloading" placeholder="请输入卸货实际净重"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="磅差" prop="poundDifference">
            <el-input v-model="form.poundDifference" placeholder="请输入磅差"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计装货重量" prop="loadWt">
            <el-input v-model="form.loadWt" placeholder="请输入预计装货重量"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际装货重量" prop="realLoadWt">
            <el-input v-model="form.realLoadWt" placeholder="请输入实际装货重量"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际到货重量" prop="realTransWt">
            <el-input v-model="form.realTransWt" placeholder="请输入实际到货重量"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计装货体积" prop="loadVol">
            <el-input v-model="form.loadVol" placeholder="请输入预计装货体积"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际装货体积" prop="realLoadVol">
            <el-input v-model="form.realLoadVol" placeholder="请输入实际装货体积"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际到货体积" prop="realTransVol">
            <el-input v-model="form.realTransVol" placeholder="请输入实际到货体积"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预计装货件数" prop="loadNum">
            <el-input v-model="form.loadNum" placeholder="请输入预计装货件数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际装货件数" prop="realLoadNum">
            <el-input v-model="form.realLoadNum" placeholder="请输入实际装货件数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际到货件数" prop="realTransNum">
            <el-input v-model="form.realTransNum" placeholder="请输入实际到货件数"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划发货时间" prop="departTime">
            <el-date-picker clearable
                            v-model="form.departTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择计划发货时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划到货时间" prop="arrivalTime">
            <el-date-picker clearable
                            v-model="form.arrivalTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择计划到货时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际发货时间" prop="realDepartTime">
            <el-date-picker clearable
                            v-model="form.realDepartTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择实际发货时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际到货时间" prop="realArrivalTime">
            <el-date-picker clearable
                            v-model="form.realArrivalTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择实际到货时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="REMARK">
            <el-input v-model="form.REMARK" placeholder="请输入备注"/>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {pageListCargo, getCargo, delCargo, addCargo, updateCargo,pageListCargoBySearch,getSum} from "@/api/trans/cargo";

export default {
  name: "TransCargo",
  data() {
    return {
      sum: [],
      statusOptions: [
        { label: '已完成', value: '1' },
        { label: '未完成', value: '0' },
      ],
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运输装载表格数据
      cargoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        transId: null,
        orderCargoId: null,
        cargoName: null,
        plantWeight: null,
        pierWeight: null,
        grossWeightLoading: null,
        grossWeightUnloading: null,
        netWeightUnloading: null,
        poundDifference: null,
        loadWt: null,
        realLoadWt: null,
        realTransWt: null,
        loadVol: null,
        realLoadVol: null,
        realTransVol: null,
        loadNum: null,
        realLoadNum: null,
        realTransNum: null,
        departTime: null,
        arrivalTime: null,
        realDepartTime: null,
        realArrivalTime: null,
        REMARK: null,
        inputDate: null,
        inputDateBegin: null,
        inputDateEnd: null,
        status: null,
        custName: null,
        orderNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          {required: true, message: "订单Id不能为空", trigger: "blur"}
        ],
        transId: [
          {required: true, message: "运输计划ID不能为空", trigger: "blur"}
        ],
        orderCargoId: [
          {required: true, message: "货物ID不能为空", trigger: "blur"}
        ],
        cargoName: [
          {required: true, message: "运输货物不能为空", trigger: "blur"}
        ],
        plantWeight: [
          {required: true, message: "工厂总重不能为空", trigger: "blur"}
        ],
        pierWeight: [
          {required: true, message: "码头总重不能为空", trigger: "blur"}
        ],
        grossWeightLoading: [
          {required: true, message: "装货实际毛重不能为空", trigger: "blur"}
        ],
        grossWeightUnloading: [
          {required: true, message: "卸货实际毛重不能为空", trigger: "blur"}
        ],
        netWeightUnloading: [
          {required: true, message: "卸货实际净重不能为空", trigger: "blur"}
        ],
        poundDifference: [
          {required: true, message: "磅差不能为空", trigger: "blur"}
        ],
        loadWt: [
          {required: true, message: "预计装货重量不能为空", trigger: "blur"}
        ],
        realLoadWt: [
          {required: true, message: "实际装货重量不能为空", trigger: "blur"}
        ],
        realTransWt: [
          {required: true, message: "实际到货重量不能为空", trigger: "blur"}
        ],
        loadVol: [
          {required: true, message: "预计装货体积不能为空", trigger: "blur"}
        ],
        realLoadVol: [
          {required: true, message: "实际装货体积不能为空", trigger: "blur"}
        ],
        realTransVol: [
          {required: true, message: "实际到货体积不能为空", trigger: "blur"}
        ],
        loadNum: [
          {required: true, message: "预计装货件数不能为空", trigger: "blur"}
        ],
        realLoadNum: [
          {required: true, message: "实际装货件数不能为空", trigger: "blur"}
        ],
        realTransNum: [
          {required: true, message: "实际到货件数不能为空", trigger: "blur"}
        ],
        departTime: [
          {required: true, message: "计划发货时间不能为空", trigger: "blur"}
        ],
        arrivalTime: [
          {required: true, message: "计划到货时间不能为空", trigger: "blur"}
        ],
        realDepartTime: [
          {required: true, message: "实际发货时间不能为空", trigger: "blur"}
        ],
        realArrivalTime: [
          {required: true, message: "实际到货时间不能为空", trigger: "blur"}
        ],
        REMARK: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSumBySearch();
  },
  methods: {
    getSumBySearch() {
      return getSum(this.queryParams).then(response => {
        // 构建汇总数据数组
        const sums = [
          "合计",
          "",
          "",
          "",
          "",
          "",
          "",
          response.totalGrossWeight,
          response.totalPierCarWeight,
          response.totalRealLoadWt,
          response.totalGrossWeightUnloading,
          response.totalPlantCarWeight,
          response.totalRealTransWt,
          response.totalPoundDifference,
        ];
        this.sum = sums;
      });
    },
    getSummaries(){
      return this.sum;
    },
    /** 查询运输装载列表 */
    getList() {
      this.loading = true;
      if (this.dateRange != null && this.dateRange.length > 0) {
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      } else {
        this.queryParams.beginDate = null;
        this.queryParams.endDate = null;
      }
      pageListCargoBySearch(this.queryParams).then(response => {
        this.cargoList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
      this.getSumBySearch();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: null,
        orderId: null,
        transId: null,
        orderCargoId: null,
        cargoName: null,
        plantWeight: null,
        pierWeight: null,
        grossWeightLoading: null,
        grossWeightUnloading: null,
        netWeightUnloading: null,
        poundDifference: null,
        loadWt: null,
        realLoadWt: null,
        realTransWt: null,
        loadVol: null,
        realLoadVol: null,
        realTransVol: null,
        loadNum: null,
        realLoadNum: null,
        realTransNum: null,
        departTime: null,
        arrivalTime: null,
        realDepartTime: null,
        realArrivalTime: null,
        REMARK: null,
      };
      this.resetForm("form");
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      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.reset();
      this.open = true;
      this.title = "添加运输装载";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ID = row.ID || this.ids;
      getCargo(ID).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运输装载";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ID != null) {
            updateCargo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCargo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.ID || this.ids;
      const names = row.ID || this.names;
      this.$modal.confirm('是否确认删除运输装载为"' + names + '"的数据项？').then(function () {
        return delCargo(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('trans/cargo/exportBySearch', {
        ...this.queryParams
      }, `运输计划_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
