sen
2026-01-15 13aedf29e82c77e7ea4a4f5cd826ba378006026a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<template>
  <el-dialog v-model="dialogVisible" title="请选择 关联应付账单" width="85%" destroy-on-close @close="handleClose"
    append-to-body>
    <div class="search-bar">
      <el-form inline :model="queryParams" class="search-form" size="default">
        <el-form-item label="系统编号:">
          <el-input v-model="queryParams.systemNo" placeholder="请输入系统编号" style="width: 180px" clearable />
        </el-form-item>
 
        <el-form-item label="账单名称:">
          <el-input v-model="queryParams.billName" placeholder="请输入账单名称" style="width: 180px" clearable />
        </el-form-item>
 
        <el-form-item label="供应商名称:">
          <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" style="width: 180px" clearable />
        </el-form-item>
 
        <!-- <el-form-item label="状态:">
          <el-select v-model="queryParams.status" style="width: 150px;" placeholder="请选择状态" clearable>
            <el-option v-for="dict in sys_bill_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
 
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleSearch">搜索</el-button>
          <el-button plain icon="RefreshLeft" @click="handleReset">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
 
    <el-table ref="customerTableRef" :data="customerList" border size="small" style="width: 100%"
      :highlight-current-row="true" row-key="id" @current-change="handleRowSelect" class="customer-table">
      <el-table-column prop="systemNo" label="系统编号" min-width="150" show-overflow-tooltip />
      <el-table-column prop="billName" label="账单名称" min-width="150" show-overflow-tooltip />
      <el-table-column prop="supplierName" label="供应商名称" min-width="150" show-overflow-tooltip />
      
      <el-table-column prop="isInternalSettlement" label="是否内部结算" min-width="120">
        <template #default="scope">
          {{ dictFormat(sys_whether_type, scope.row.isInternalSettlement) }}
        </template>
      </el-table-column>
 
      <el-table-column prop="internalSettlementUnit" label="内部结算单位" min-width="120" show-overflow-tooltip />
      <el-table-column prop="documentCount" label="单据数量" min-width="100" />
      <el-table-column prop="totalAmount" label="应结算金额" min-width="120" />
      
      <el-table-column prop="currency" label="币制" min-width="100">
        <template #default="scope">
          {{ dictFormat(sys_currency, scope.row.currency) }}
        </template>
      </el-table-column>
 
      <el-table-column prop="discountAmount" label="减免金额" min-width="100" />
      <el-table-column prop="paidAmount" label="已付金额" min-width="100" />
      <el-table-column prop="pendingAmount" label="待付金额" min-width="100" />
 
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          {{ dictFormat(sys_bill_status, scope.row.status) }}
        </template>
      </el-table-column>
    </el-table>
 
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
 
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定选择</el-button>
      </div>
    </template>
  </el-dialog>
</template>
 
<script setup lang="ts">
import { ref, reactive, watch, nextTick } from 'vue';
import type { Table } from 'element-plus';
import { ElMessage } from 'element-plus';
import useCurrentInstance from "@/utils/useCurrentInstance";
import { listPayableBillManagement } from "@/api/cwgl/payableBillManagement";
 
interface Customer {
  id: string | number;
  systemNo: string;
  billName: string;
  supplierName: string;
  isInternalSettlement: string;
  internalSettlementUnit: string;
  documentCount: number;
  totalAmount: number;
  currency: string;
  discountAmount: number;
  paidAmount: number;
  pendingAmount: number;
  status: string | number;
}
 
const props = defineProps({
  visible: { type: Boolean, default: false },
  defaultSelectedId: { type: [String, Number], default: '' },
  // 新增:接收默认状态
  defaultStatus: { type: [String, Number], default: '' } 
});
 
const emit = defineEmits(['confirm', 'close', 'update:visible']);
 
const { proxy } = useCurrentInstance();
// 获取所需的字典
const { sys_bill_status, sys_currency, sys_whether_type } = proxy.useDict(
  'sys_bill_status', 
  'sys_currency', 
  'sys_whether_type'
);
 
const dictFormat = (dict: any, value: any) => {
  return proxy.selectDictLabel(dict, value);
};
 
const dialogVisible = ref(false);
const customerList = ref<Customer[]>([]);
const total = ref(0);
const selectedRow = ref<Customer | null>(null);
const customerTableRef = ref<InstanceType<typeof Table>>();
 
const queryParams = reactive({
  systemNo: '',
  billName: '',
  supplierName: '',
  status: '',
  pageNum: 1,
  pageSize: 10
});
 
const getDataList = async () => {
  try {
    const res = await listPayableBillManagement(queryParams);
    if (res.code === 200) {
      customerList.value = res.rows;
      total.value = res.total;
      return res.rows;
    }
  } catch (err) {
    console.error('加载列表失败:', err);
  }
  return [];
};
 
const autoSelectRow = (list: Customer[]) => {
  if (!props.defaultSelectedId || list.length === 0) return;
  nextTick(() => {
    const target = list.find(item => String(item.id) === String(props.defaultSelectedId));
    if (target && customerTableRef.value) {
      customerTableRef.value.setCurrentRow(target);
      selectedRow.value = target;
    }
  });
};
 
const handleSearch = () => {
  queryParams.pageNum = 1;
  getList();
};
 
const handleReset = () => {
  // 重置其他搜索字段
  queryParams.systemNo = '';
  queryParams.billName = '';
  queryParams.customerName = '';
  
  // 核心处理:如果有默认状态值就恢复默认值,没有才设为空
  if (props.defaultStatus !== undefined && props.defaultStatus !== null && props.defaultStatus !== '') {
    queryParams.status = props.defaultStatus;
  } else {
    queryParams.status = '';
  }
 
  handleSearch();
};
 
const getList = () => {
  getDataList().then((list) => {
    autoSelectRow(list);
  });
};
 
const handleRowSelect = (val: Customer | null) => {
  selectedRow.value = val;
};
 
const handleConfirm = () => {
  if (!selectedRow.value) {
    ElMessage.warning('请先点击表格选择一行数据');
    return;
  }
  emit('confirm', selectedRow.value);
  handleClose();
};
 
const handleClose = () => {
  emit('update:visible', false);
  emit('close');
};
 
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal;
  if (newVal) {
    // 关键逻辑:如果 props 传了默认状态就用它,否则设置为空字符串
    // 使用 queryParams.status = props.defaultStatus || ''; 
    // 但考虑到 '0' 可能是有效值,建议判断是否为 undefined 或 null
    if (props.defaultStatus !== undefined && props.defaultStatus !== null) {
        queryParams.status = props.defaultStatus;
    } else {
        queryParams.status = '';
    }
 
    // 重置页码为第一页并加载数据
    queryParams.pageNum = 1;
    getList();
  }
});
</script>