<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-button class="tool-btn" type="success" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['carriage_order:plan:add']">新增运输计划</el-button>
      </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>
      <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="45" align="center" />
      <el-table-column label="" type="index" width="30" align="center" />
      <el-table-column label="订单号"  align="center" prop="order.orderNo" 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" prop="driverMobile" sortable='custom' />
      <el-table-column label="客户名称" align="center" prop="order.custName" sortable='custom' />
      <el-table-column label="备注" align="center" prop="remark" 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">
      <TransPlanEdit ref="edit" v-if="open" :editType="editType" :trans="currentRow ? [currentRow]:selection" @saved="refresh" @cancel="cancel"></TransPlanEdit>
    </el-dialog>
  </div>
</template>
<script>
import { pageListTrans, getTrans, delTrans, addTrans, updateTrans } from "@/api/order/trans";
import AutoTableHeight from '@/views/tm/components/AutoTableHeight';
import TransPlanEdit from './TransPlanEdit';

export default {
  name: 'TransPlan',
  mixins: [AutoTableHeight],
  components: {
    TransPlanEdit,
  },
  data() {
    return {
      selection: [],
      currentRow: null,
      list: [],
      total: 0,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      title: '',
      open: false,
      // 外部搜索参数
      searchParams: {},
      editType: 'add',
    };
  },
  props: {
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询车辆货物运输列表 */
    getList() {
      this.loading = true;
      pageListTrans({
        ...this.queryParams,
        ...this.searchParams,
      }).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 delTrans(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    cancel() {
      this.open = false;
    },
    submitForm() {
      this.$refs.edit.submitForm();
    },
    refresh() {
      this.open = false;
      this.getList();
    }
  }
}
</script>
