<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="waybillName">
        <el-input v-model="queryParams.waybillName" placeholder="请输入发货单名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货单位" prop="consigneeUnit">
        <el-input v-model="queryParams.consigneeUnit" placeholder="请输入收货单位" clearable
          @keyup.enter.native="handleQuery" />
      </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-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" :disabled="!selectedPlan"
          title="请先选择发货计划">新增</el-button> -->
        <el-button type="success" plain icon="el-icon-refresh" size="mini" @click="handleQuery">刷新</el-button>
        <!-- <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button> -->
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border :data="deliveryWaybillList" highlight-current-row>
      <el-table-column label="发货单唯一ID" align="center" prop="waybillId" v-if="false" />
      <el-table-column label="发货单名称" align="center" prop="waybillName" />
      <el-table-column label="车牌" align="center" prop="licensePlate" width="100" />
      <el-table-column label="发货单位" align="center" prop="senderUnit" />
      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="principal" />
      <el-table-column label="负责人电话" align="center" prop="principalPhone" width="100" />
      <el-table-column label="完成状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <el-tag size="mini" type="success" v-if="scope.row.status === 1">已发货</el-tag>
          <el-tag size="mini" type="info" v-else>未发货</el-tag>
          <!-- <el-select v-model="scope.row.status" placeholder="请选择完成状态" @change="handleStatusChange(scope.row)">
            <el-option label="已发货" :value="1" />
            <el-option label="未发货" :value="0" />
          </el-select> -->
        </template>
      </el-table-column>
      <el-table-column label="订单合同号" align="center" prop="contractCode" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-paperclip" @click.stop="handleBind(scope.row)">绑定订单</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" :disabled="scope.row.status === 1" title="已发货的发货单不能修改"
            @click.stop="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :disabled="scope.row.status === 1"
            title="已发货的发货单不能删除" @click.stop="handleDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

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

    <el-dialog title="选择订单" :visible.sync="dialogVisible" width="80%">
      <order-table ref="orderTable" @row-click="submitBind" />
    </el-dialog>
  </div>
</template>

<script>
import { listDeliveryWaybill, updateDeliveryWaybill } from "@/api/wms/deliveryWaybill";
import OrderTable from "../components/OrderTable.vue";

export default {
  data() {
    return {
      deliveryWaybillList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        waybillName: '',
        consigneeUnit: ''
      },
      dialogVisible: false,
      currentWaybill: {},
      currentWaybillDetails: [],
      loading: false,
      showSearch: true,
    }
  },
  components: {
    OrderTable,
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 完成状态改变时的处理 */
    handleStatusChange(row) {
      // 确保在更新状态时包含waybillId
      updateDeliveryWaybillStatus({
        waybillId: row.waybillId,
        status: row.status
      }).then(() => {
        this.$modal.msgSuccess("状态更新成功");
        this.getList(); // 刷新列表
      });
    },
    /** 查询发货单列表 */
    getList() {
      this.loading = true;
      // 确保查询参数包含planId
      const params = {
        ...this.queryParams,
        // 如果选中了计划，传递planId，否则不传递该参数
        planId: this.selectedPlan ? this.selectedPlan.planId : undefined
      };
      listDeliveryWaybill(params).then(response => {
        this.deliveryWaybillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleBind(row) {
      this.dialogVisible = true;
      this.currentWaybill = row;
    },
    submitBind(order) {
      this.loading = true;
      this.dialogVisible = false;
      updateDeliveryWaybill({
        ...this.currentWaybill,
        orderId: order.orderId,
      }).then(() => {
        this.loading = false;
        this.$modal.msgSuccess("绑定成功");
        this.getList();
      }).catch(() => {
        this.$modal.msgError("绑定失败");
        this.dialogVisible = true;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/deliveryWaybill/export', {
        ...this.queryParams
      }, `deliveryWaybill_${new Date().getTime()}.xlsx`)
    },
  }
}
</script>