<template>
  <div ref="list">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <span class="text-gray" style="line-height: 28px;font-weight: 500;">运输货物计划：</span>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip effect="dark" :disabled="plans && plans.length > 0" content="请先选择运输车辆计划" placement="top">
          <el-button class="tool-btn" type="success" :disabled="!plans || plans.length === 0" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['carriage_order:plan:add']">新增货物计划</el-button>
        </el-tooltip>
      </el-col>
      <el-col :span="1.5">
        <el-button class="tool-btn" type="primary" plain icon="el-icon-edit" size="mini" :disabled="selection.length <= 0" @click="handleUpdate()"
          v-hasPermi="['carriage_order:plan:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button class="tool-btn" type="danger" plain icon="el-icon-delete" size="mini" :disabled="selection.length <= 0" @click="handleDelete()"
          v-hasPermi="['carriage_order:plan:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button class="tool-btn" type="warning" plain icon="el-icon-download" size="mini"  @click="handleExport"
                   v-hasPermi="['carriage_order:plan:export']">导出</el-button>
      </el-col>
      <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="table" v-loading="loading" :height="tableHeight" :data="list" :highlight-current-row="true" @sort-change="handleSortChange"
      @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" type="index" width="55" align="center" />
      <!-- <el-table-column label="订单号"  align="center" prop="orderNo" 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'></el-table-column>
      <el-table-column label="计划到达时间" align="center" prop="arrivalTime" width="180" sortable='custom'></el-table-column>
      <el-table-column label="实际发车时间" align="center" prop="realDepartTime" width="180" sortable='custom'></el-table-column>
      <el-table-column label="实际到达时间" align="center" prop="realArrivalTime" width="180" sortable='custom'>
      </el-table-column>
      <el-table-column label="计划装货重量" align="center" prop="loadWt" sortable='custom' />
      <el-table-column label="车牌号码" align="center" prop="vehicleNumber" sortable='custom' />
      <el-table-column label="司机姓名" align="center" prop="driverName" sortable='custom' />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click.stop="handleUpdate(scope.row)"
            v-hasPermi="['carriage_order:plan:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)"
            v-hasPermi="['carriage_order:plan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <el-dialog v-dialogDrag custom-class="dialog-edit" :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false">
      <TransCargoPlanEdit ref="edit" v-if="open" :editType="editType" :plans="plans" :trans="currentRow ? [currentRow]:selection" @saved="refresh"></TransCargoPlanEdit>
      <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 } from "@/api/trans/cargo";
import AutoTableHeight from '@/views/tm/components/AutoTableHeight';
import TransCargoPlanEdit from './TransCargoPlanEdit';

export default {
  name: 'TransPlan',
  mixins: [AutoTableHeight],
  components: {
    TransCargoPlanEdit,
  },
  data() {
    return {
      selection: [],
      currentRow: null,
      list: [],
      total: 0,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      title: '',
      open: false,
      editType: 'add',
      // 外部搜索参数
      searchParams: {},
    };
  },
  props: {
    plans: [Array],
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询车辆货物运输列表 */
    getList() {
      this.loading = true;
      let transId = '';
      if (this.plans && this.plans.length > 0) {
        transId = this.plans[0].id;
      }

      pageListCargo({
        ...this.queryParams,
        ...this.searchParams,
        transId
      }).then(response => {
        this.list = response.rows || [];
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery(params) {
      this.searchParams = params || {};
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
      this.$emit('change', selection);
    },
    // 点击行
    handleRowClick(row) {
      const index = this.selection.findIndex(val => val.id === row.id);
      this.$refs.table.toggleRowSelection(row, index < 0);
    },
    handleAdd() {
      this.editType = 'add';
      this.title = '新增货物运输计划';
      this.open = true;
    },
    handleUpdate(row) {
      this.currentRow = row;
      this.editType = 'update';
      this.title = '编辑货物运输计划';
      this.open = true;
    },
    handleDelete(row) {
      const ids = row ? row.id : this.selection.map(item => item.id);
      this.$modal.confirm(`是否确认删除${Array.isArray(ids) ?'选中的这些':'当前'}货物运输计划？`).then(function () {
        return delCargo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    cancel() {
      this.open = false;
    },
    submitForm() {
      this.$refs.edit.submitForm();
    },
    refresh() {
      this.open = false;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      let transId = '';
      if (this.plans && this.plans.length > 0) {
        transId = this.plans[0].id;
      }
      this.download('trans/cargo/export', {
        ...this.queryParams,
        ...this.searchParams,
        transId
      }, `运输计划_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
