<template>
  <my-page>
    <template slot="left">
      <CustList style="width: 320px;" custType="0" @change="handleCustsChange"></CustList>
    </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 class="filter-input" v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="货物名称" prop="cargoInfo">
          <el-input class="filter-input" v-model="queryParams.cargoInfo" placeholder="请输入货物名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable multiple>
            <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="开始时间" prop="beginDate">
          <el-date-picker class="filter-input" v-model="queryParams.beginDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker class="filter-input" v-model="queryParams.endDate" value-format="yyyy-MM-dd" type="date" 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="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['consign_order:finished:edit']">撤销</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['consign_order:finished:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="orderList" :highlight-current-row="true" @sort-change="handleSortChange"
        @selection-change="handleSelectionChange">
        <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="custName" sortable='custom' />
        <el-table-column label="订单号" align="center" prop="orderNo" sortable='custom' />
        <el-table-column label="货物名称" align="center" prop="cargoInfo" sortable='custom' />
        <el-table-column label="运价" align="center" prop="freightRates" sortable='custom' />
        <el-table-column label="发货地址" align="center" prop="dispatchPlaceDetail" sortable='custom' />
        <el-table-column label="收货地址" align="center" prop="receivePlaceDetail" sortable='custom' />
        <el-table-column label="日期" align="center" prop="orderDate" width="180" sortable='custom'>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleRead(scope.row)"
              v-hasPermi="['consign_order:finished:query']">查看</el-button>
            <el-popconfirm
            title="确定撤销为未完成吗？"
            @confirm="recall(scope.row)"
            >
              <el-button slot="reference" style="margin-left: 5px;" size="mini" type="text" icon="el-icon-edit"
                v-hasPermi="['consign_order:finished:edit']">撤销</el-button>
            </el-popconfirm>
          </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="80%" append-to-body :close-on-click-modal="false">
        <ConsignOrderEdit ref="edit" v-if="open" :bid="showId" :readonly="readonly" @saved="refresh" @cancel="cancel"></ConsignOrderEdit>
      </el-dialog>
    </div>
  </my-page>
</template>

<script>
import { pageListSource, getSource, delSource, addSource, revokeOrder } from "@/api/cargo/source";
import CustList from '@/views/tm/components/CustList';
import ConsignOrderEdit from '@/views/tm/components/ConsignOrderEdit';

export default {
  name: "FinishedOrder",
  // dicts: ['order_status'],
  components: {
    CustList,
    ConsignOrderEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 托运订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        cargoInfo: null,
        beginDate: null,
        endDate: null,
        status: '1',
        orderByColumn: 'orderDate',
        isAsc: 'DESC',
      },
      showId: null,
      readonly: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleCustsChange(custs) {
      const cust = custs[0] || {};
      this.queryParams.custId = cust.id;
      this.handleQuery();
    },
    /** 查询托运订单列表 */
    getList() {
      this.loading = true;
      pageListSource(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(e => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    refresh() {
      this.open = false;
      this.getList();
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams);
      this.getList();
    },
    /** 查看按钮操作 */
    handleRead(row) {
      const id = row.id || this.ids;
      this.open = true;
      this.showId = id;
      this.readonly = true;
      this.title = '托运订单信息';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      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;
    },
    // 撤销
    recall(row) {
      revokeOrder({id: row.id}).then(res => {
        this.$modal.msgSuccess("撤销成功");
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tm/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
