create.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <hc-card scrollbar is-action-btn class="create-gist">
  3. <template #header>
  4. <div class="flex-1 text-center text-[24px] font-bold">工作重点填写</div>
  5. </template>
  6. <el-form ref="baseFormRef" :model="baseForm" :rules="baseFormRules" label-position="left" label-width="auto" size="large">
  7. <el-form-item label="项目阶段:" prop="workFocusStage">
  8. <el-select v-model="baseForm.workFocusStage" placeholder="请选择">
  9. <el-option v-for="item in projectStage" :key="item.value" :label="item.label" :value="item.value" />
  10. </el-select>
  11. </el-form-item>
  12. <el-form-item label="目标任务:" prop="targetPlan">
  13. <el-input v-model="baseForm.targetPlan" placeholder="请输入" clearable type="textarea" />
  14. </el-form-item>
  15. </el-form>
  16. <template v-for="(item, index) in workFocusEntityList" :key="index">
  17. <hc-card-item class="gist-detail">
  18. <div v-if="!formInfo.id" class="hc-form-gist-action hc-flex">
  19. <div class="add hc-flex-center h-[24px] w-[24px]" @click="addGistList">
  20. <hc-icon name="add" fill />
  21. </div>
  22. <div class="subtract hc-flex-center h-[24px] w-[24px]" @click="delGistList(index)">
  23. <hc-icon name="subtract" fill />
  24. </div>
  25. </div>
  26. <el-form :ref="(el) => setGistRefs(el, index)" :model="item" :rules="gistFormRules" label-position="left" label-width="auto" size="large">
  27. <el-form-item label="选择年份:" prop="startYear">
  28. <hc-date-year v-model="item.startYear" v-model:end="item.endYear" text1="开始年份" text2="结束年份" :disabled="!!formInfo.id" />
  29. </el-form-item>
  30. <el-form-item label="工作任务:" prop="workPlan">
  31. <el-input v-model="item.workPlan" placeholder="请输入" clearable type="textarea" />
  32. </el-form-item>
  33. <el-form-item label="责任单位:" prop="dutyUnit">
  34. <el-input v-model="item.dutyUnit" placeholder="请输入" clearable />
  35. </el-form-item>
  36. </el-form>
  37. </hc-card-item>
  38. </template>
  39. <template #action>
  40. <el-button v-if="formInfo.id" type="info" @click="cancelClick">取消</el-button>
  41. <el-button v-if="!formInfo.id" :loading="saveLoading" color="#20C98B" type="primary" class="text-white" @click="saveClick">创建工作要点</el-button>
  42. <el-button v-else :loading="saveLoading" type="warning" @click="saveClick">保存</el-button>
  43. </template>
  44. </hc-card>
  45. </template>
  46. <script setup>
  47. import { onMounted, ref, watch } from 'vue'
  48. import { deepClone, formValidate, getArrValue, getObjValue, isNullES } from 'js-fast-way'
  49. import { getDictionaryData } from '~src/utils/tools'
  50. import mainApi from '~api/project/gist'
  51. const props = defineProps({
  52. form: {
  53. type: Object,
  54. default: () => ({}),
  55. },
  56. })
  57. //事件
  58. const emit = defineEmits(['back'])
  59. //监听权限
  60. const formInfo = ref(props.form)
  61. watch(() => props.form, (data) => {
  62. formInfo.value = data
  63. })
  64. //渲染完成
  65. onMounted(() => {
  66. getDataApi()
  67. })
  68. //获取接口数据
  69. const projectStage = ref([])
  70. const getDataApi = async () => {
  71. projectStage.value = await getDictionaryData('projectStage', true)
  72. const form = getObjValue(formInfo.value)
  73. if (!isNullES(form.id)) {
  74. baseForm.value = {
  75. id: form.id,
  76. workFocusStage: form.workFocusStage,
  77. targetPlan: form.targetPlan,
  78. }
  79. workFocusEntityList.value = [{
  80. startYear: form.startYear,
  81. endYear: form.endYear,
  82. workPlan: form.workPlan,
  83. dutyUnit: form.dutyUnit,
  84. }]
  85. }
  86. }
  87. //基础表单
  88. const baseFormRef = ref(null)
  89. const baseForm = ref({ workFocusStage:null, targetPlan: '' })
  90. const baseFormRules = {
  91. workFocusStage: {
  92. required: true,
  93. trigger: 'blur',
  94. message: '请选择项目阶段',
  95. },
  96. targetPlan: {
  97. required: true,
  98. trigger: 'blur',
  99. message: '请输入目标任务',
  100. },
  101. }
  102. //任务列表表单
  103. const workFocusEntityList = ref([{}])
  104. //处理ref
  105. const gistRefs = ref([])
  106. const setGistRefs = (el, index) => {
  107. if (el) {
  108. try {
  109. gistRefs.value[index] = el
  110. } catch (error) {
  111. gistRefs.value.push(el)
  112. }
  113. }
  114. }
  115. //任务列表表单
  116. const gistFormRules = {
  117. startYear: {
  118. required: true,
  119. trigger: 'blur',
  120. message: '请选择年份',
  121. },
  122. workPlan: {
  123. required: true,
  124. trigger: 'blur',
  125. message: '请填写工作任务',
  126. },
  127. dutyUnit: {
  128. required: true,
  129. trigger: 'blur',
  130. message: '请填写责任单位',
  131. },
  132. }
  133. //新增工作重点
  134. const addGistList = () => {
  135. const list = getArrValue(workFocusEntityList.value)
  136. list.push({})
  137. }
  138. //删除工作重点
  139. const delGistList = (index) => {
  140. const list = deepClone(workFocusEntityList.value)
  141. if (list.length <= 1) {
  142. window.$message.warning('至少需要保留一个任务')
  143. return
  144. }
  145. list.splice(index, 1)
  146. workFocusEntityList.value = list
  147. }
  148. //取消
  149. const cancelClick = () => {
  150. emit('back')
  151. }
  152. //创建 保存
  153. const saveLoading = ref(false)
  154. const saveClick = async () => {
  155. //验证基础表单
  156. const isForm = await formValidate(baseFormRef.value)
  157. //验证列表表单
  158. let isGistForm = true
  159. const refs = getArrValue(gistRefs.value)
  160. for (let i = 0; i < refs.length; i++) {
  161. const form = await formValidate(refs[i])
  162. if (!form) isGistForm = false
  163. }
  164. if (!isForm || !isGistForm) return
  165. saveLoading.value = true
  166. //处理表单数据
  167. const form = baseForm.value, gist = workFocusEntityList.value
  168. let newWorkForm = []
  169. for (let i = 0; i < gist.length; i++) {
  170. newWorkForm.push({
  171. ...form,
  172. ...gist[i],
  173. })
  174. }
  175. //发起请求
  176. const { error, code, msg } = await mainApi.submit({
  177. workFocusEntityList: newWorkForm,
  178. })
  179. //判断状态
  180. saveLoading.value = false
  181. if (!error && code === 200) {
  182. window?.$message?.success(msg)
  183. if (!isNullES(form.id)) {
  184. baseForm.value = {}
  185. workFocusEntityList.value = [{}]
  186. cancelClick()
  187. }
  188. } else {
  189. window.$message.error(msg ?? '操作失败')
  190. }
  191. }
  192. </script>
  193. <style lang="scss">
  194. .hc-div-new-card-box.create-gist .el-card.hc-card-box.hc-new-card-box .hc-card-main .hc-card-main-body {
  195. padding: 14px;
  196. .el-scrollbar__bar.is-vertical {
  197. right: -16px;
  198. }
  199. }
  200. .hc-card-item-box.gist-detail {
  201. padding: 20px;
  202. background: #f7f7f7;
  203. border-radius: 5px;
  204. margin-top: 14px;
  205. .hc-form-gist-action {
  206. position: absolute;
  207. color: #101010;
  208. z-index: 999;
  209. right: 0;
  210. div {
  211. cursor: pointer;
  212. font-size: 20px;
  213. border-radius: 2px;
  214. i {
  215. margin-top: 1px;
  216. }
  217. }
  218. .add {
  219. background: #409eff;
  220. color: white;
  221. &:hover {
  222. background: #337ecc;
  223. }
  224. }
  225. .subtract {
  226. margin-left: 8px;
  227. background: #f56c6c;
  228. color: white;
  229. &:hover {
  230. background: #c45656;
  231. }
  232. }
  233. }
  234. }
  235. </style>