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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<template>
  <el-dialog :title="modalOptions.title" v-model="visible" @close="close" width="60%">
    <el-form :model="form" label-width="100px" :rules="formRules" ref="formRef">
      <el-form-item label="岗位名称" prop="deptPositionId">
        <el-tree-select
          v-model="form.deptPositionId"
          :data="deptPositionTree"
          :props="{ value: 'id', label: 'label', children: 'children', disabled: checkDisabled }"
          placeholder="请选择岗位"
          clearable
          check-strictly
    :render-after-expand="false"
    check-on-click-node
          @change="handlePositionChange"
        >
          <template #default="{ data }">
            <span>
              <el-tag size="small" :type="data.type===1 ? 'success' : 'warning'" effect="plain" style="margin-right:4px;">
                {{ data.type === 1 ? '部门' : '岗位' }}
              </el-tag>
              {{ data.label }}
            </span>
          </template>
        </el-tree-select>
      </el-form-item>
      <el-form-item label="进厂日期" prop="factoryDate">
        <el-date-picker style="width: 100%" v-model="form.factoryDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" type="date" placeholder="请选择日期" clearable />
      </el-form-item>
      <el-form-item label="发放日期" prop="issueDate">
        <el-date-picker style="width: 100%" v-model="form.issueDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" type="date" placeholder="请选择日期" clearable />
      </el-form-item>
      <el-form-item label="员工与数量" required>
        <div style="width:100%">
          <el-table :data="form.laborIssueList" border size="small" :span-method="arraySpanMethod">
            <el-table-column label="员工名称" width="220">
              <template #default="scope">
                <span>{{ personList.find(p => p.id === scope.row.staffId)?.staffName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="劳保用品" prop="dictName" align="center" />
            <el-table-column label="发放数量" align="center" width="150">
              <template #default="scope">
                <el-input-number v-model="scope.row.num" :min="0" :step="1" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120" align="center">
              <template #default="scope">
                <el-button link type="danger" size="small" @click="removeItemRow(scope.$index)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="sendForm" :loading="loading">{{ modalOptions.confirmText }}</el-button>
      <el-button @click="closeModal">{{ modalOptions.cancelText }}</el-button>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { ref, getCurrentInstance, onMounted, nextTick } from 'vue'
import { useModal } from "@/hooks/useModal";
import { add, update } from "@/api/lavorissce/ledger";
import useFormData from "@/hooks/useFormData";
import useUserStore from "@/store/modules/user";
import {staffOnJobListPage} from "@/api/personnelManagement/employeeRecord.js";
import { getDept } from "@/api/collaborativeApproval/approvalProcess.js";
import { getDeptPositionTree, getDeptPositionByDeptIdLabor } from "@/api/lavorissce/issue";
import { deepCopySameProperties } from '@/utils/util'
import { ElMessage } from "element-plus";
import {staffJoinListPage} from "@/api/personnelManagement/onboarding.js";
 
const { proxy } = getCurrentInstance()
 
defineOptions({ name: "收入新增编辑" });
const emits = defineEmits(["success"]);
 
const formRef = ref(null)
const userStore = useUserStore();
const { sys_lavor_issue } = proxy.useDict("sys_lavor_issue")
const { sys_lavor_issue_type } = proxy.useDict("sys_lavor_issue_type")
 
const productOptions = ref([]);
const personList = ref([]);
const deptPositionTree = ref([]);
const laborSuppliesOptions = ref([]);
 
const formRules = {
  deptPositionId: [{ required: true, trigger: "blur", message: "请输入" }],
  adoptedDate: [{ required: true, trigger: "change", message: "请选择" }],
  factoryDate: [{ required: true, trigger: "change", message: "请选择" }],
  issueDate: [{ required: true, trigger: "change", message: "请选择" }],
}
 
const { form, resetForm } = useFormData({
  deptPositionId: undefined,
  dictType: undefined,
  dictId: undefined,
  adoptedDate: undefined,
  factoryDate: undefined,
  issueDate: undefined,
  laborIssueList: [ { staffId: undefined, num: undefined } ],
});
 
function getPersonList() {
  staffOnJobListPage({current: -1, size:-1, staffState: 1}).then(res => { personList.value = res.data.records })
}
function getProductOptions() {
  getDept().then(res => { productOptions.value = res.data })
}
 
function clearValidate() { formRef.value?.clearValidate() }
function resetFormAndValidate() {
  resetForm()
  clearValidate()
  getProductOptions()
  getPersonList()
}
 
function removeItemRow(index) {
  form.laborIssueList.splice(index, 1)
}
 
const {
  id,
  visible,
  loading,
  openModal,
  modalOptions,
  closeModal,
} = useModal({ title: "劳保台账" });
 
function sendForm() {
  formRef.value?.validate(async valid => {
    if (!valid) return
    if (!Array.isArray(form.laborIssueList) || form.laborIssueList.length === 0) {
      ElMessage.error('请至少添加一行员工与数量')
      return
    }
    for (const row of form.laborIssueList) {
      if (!row.staffId) { ElMessage.error('请选择员工'); return }
      if (!row.dictId) { ElMessage.error('请选择劳保用品'); return }
      if (row.num == null || row.num === '' || Number(row.num) < 0) { ElMessage.error('请输入数量'); return }
    }
    const payload = { ...form }
    const { code } = id.value ? await update({ id: id.value, ...payload }) : await add(payload)
    if (code == 200) {
      emits("success")
      ElMessage({ message: "操作成功", type: "success" })
      close()
    } else {
      loading.value = false
    }
  })
}
 
function close() {
  resetFormAndValidate()
  closeModal()
}
 
async function loadForm(row) {
  openModal(row.id)
  await nextTick()
  deepCopySameProperties(row, form)
}
// 将部门岗位数据转换为树形结构
function mapDeptToTree(d) {
  const node = {
    id: d.id,
    label: d.name,
    type: d.type,
    children: [],
  }
  const kids = Array.isArray(d.children) ? d.children : []
  for (const c of kids) {
    if (c.type === 1) {
      node.children.push(mapDeptToTree(c))
    } else if (c.type === 2) {
      node.children.push({
        id: c.id,
        label: c.name,
        type: c.type,
      })
    }
  }
  return node
}
 
async function loadDeptPositionTree() {
  try {
    const res = await getDeptPositionTree()
    const data = res?.data || res || []
    deptPositionTree.value = data.map(d => mapDeptToTree(d))
  } catch (e) {
    console.error('加载部门岗位树失败:', e)
    deptPositionTree.value = []
  }
}
 
async function handlePositionChange(deptPositionId) {
  console.log('选择的岗位ID:', deptPositionId)
  if (!deptPositionId) {
    laborSuppliesOptions.value = []
    form.dictId = undefined
    form.laborIssueList = []
    return
  }
  const res = await getDeptPositionByDeptIdLabor({ id: deptPositionId })
  laborSuppliesOptions.value = res?.data || []
  
  const staffRes = await staffJoinListPage({ current: -1, size: -1, staffState: 1, deptPositionId: deptPositionId })
  const staffList = staffRes?.data?.records || []
  personList.value = staffList
  const suppliesList = laborSuppliesOptions.value || []
  
  if (staffList.length > 0 && suppliesList.length > 0) {
    form.laborIssueList = []
    staffList.forEach(staff => {
      suppliesList.forEach(supply => {
        form.laborIssueList.push({
          staffId: staff.id,
          dictId: supply.dictId,
          dictName: supply.dictName,
          num: supply.num || 0
        })
      })
    })
  } else {
    form.laborIssueList = []
  }
}
 
function arraySpanMethod({ row, column, rowIndex, columnIndex }) {
  if (columnIndex === 0) {
    const staffId = row.staffId
    const sameStaffRows = form.laborIssueList.filter(item => item.staffId === staffId)
    const firstIndex = form.laborIssueList.findIndex(item => item.staffId === staffId)
    if (rowIndex === firstIndex) {
      return {
        rowspan: sameStaffRows.length,
        colspan: 1
      }
    } else {
      return {
        rowspan: 0,
        colspan: 0
      }
    }
  }
}
 
function checkDisabled(data) {
  return data.type === 1
}
 
onMounted(() => {
  loadDeptPositionTree()
  getPersonList()
})
 
defineExpose({ openModal, loadForm })
</script>