tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
<template>
  <div class="container2">
    <div class="title" v-if="!editId">
      新建日历记事
      <span class="back" @click="back" v-if="showBack"> <i class="iconfont icon-fanhui"></i> 返回 </span>
    </div>
    <div class="title" v-else>
      <span>修改日历记事</span>
      <span class="back" @click="back" v-if="showBack"> <i class="iconfont icon-fanhui"></i> 返回 </span>
    </div>
 
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="calendarHoliday" status-icon ref="form" label-width="auto" @submit.prevent :rules="rules">
            <el-form-item label="标题" prop="title">
              <el-input placeholder="请输入标题" v-model="calendarHoliday.title"></el-input>
            </el-form-item>
 
            <el-form-item label="日历时间" prop="date_range">
              <el-date-picker
                v-model="calendarHoliday.date_range"
                type="daterange"
                range-separator="到"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item label="类型" prop="type">
              <el-select v-model="calendarHoliday.type" placeholder="类型" clearable filterable>
                <el-option v-for="item in calendarTypeList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
 
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm">保 存</el-button>
              <el-button @click="resetForm">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
 
<script>
import { reactive, ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import Calendar from '@/model/calendar'
import CalendarHoliday from '@/model/calendarHoliday'
import SysDictItemModel from '@/model/sysDictItem'
import dayjs from 'dayjs'
 
export default {
  components: {},
  props: {
    editId: {
      type: String,
      default: null,
    },
    showBack: {
      type: Boolean,
      default: true,
    },
    pointDate: {
      type: String,
      default: null,
    },
  },
  setup(props, context) {
    const form = ref(null)
    const loading = ref(false)
    const business_content = ref(null)
    const calendarTypeList = ref([])
 
    const calendarHoliday = reactive({
      id: '',
      title: '',
      date_range: [],
      start_date: '',
      type: '',
      end_date: '',
    })
 
    const listAssign = (a, b) => Object.keys(a).forEach(key => {
      a[key] = b[key] || a[key]
    })
 
    /**
     * 表单规则验证
     */
    const { rules } = getRules()
 
    onMounted(() => {
      if (props.editId) {
        getCalendarHoliday()
      }
      if (props.pointDate) {
        const tmp_date_range = []
        tmp_date_range.push(props.pointDate)
        tmp_date_range.push(props.pointDate)
        calendarHoliday.date_range = tmp_date_range
      }
 
      loadDictDitems()
    })
 
    // 根据字典类型查询字典
    const loadDictDitems = async () => {
      calendarTypeList.value = await SysDictItemModel.getSysDictItemListByType('calendar_type')
    }
 
    const getCalendarHoliday = async () => {
      loading.value = true
      const res = await CalendarHoliday.getCalendarHoliday(props.editId)
 
      listAssign(calendarHoliday, res)
      calendarHoliday.value = res
 
      const tmp_date_range = []
      tmp_date_range.push(res.start_date)
      tmp_date_range.push(res.end_date)
 
      calendarHoliday.value.date_range = tmp_date_range
      calendarHoliday.date_range = tmp_date_range
 
      calendarHoliday.type = `${res.type}`
 
      loading.value = false
    }
 
    // 重置表单
    const resetForm = () => {
      form.value.resetFields()
    }
 
    const submitForm = async formName => {
      form.value.validate(async valid => {
        if (valid) {
          let res = {}
          calendarHoliday.start_date = calendarHoliday.date_range[0]
          calendarHoliday.end_date = calendarHoliday.date_range[1]
          if (props.editId) {
            res = await CalendarHoliday.editCalendarHoliday(props.editId, calendarHoliday)
            context.emit('editClose')
          } else {
            res = await CalendarHoliday.createCalendarHoliday(calendarHoliday)
            resetForm(formName)
            if (props.editId) {
              context.emit('editClose')
            }
          }
          if (res.code < window.MAX_SUCCESS_CODE) {
            ElMessage.success(`${res.message}`)
          }
        } else {
          console.error('error submit!!')
          ElMessage.error('请将信息填写完整')
        }
      })
    }
 
    const back = () => {
      context.emit('editClose')
    }
 
    const handleContentChange = val => {
      calendarHoliday.business_content = val
    }
 
    return {
      back,
      calendarHoliday,
      form,
      rules,
      resetForm,
      submitForm,
 
      business_content,
      handleContentChange,
      calendarTypeList,
    }
  },
}
 
/**
 * 表单验证规则
 */
function getRules() {
  /**
   * 验证回调函数
   */
  const checkInfo = (rule, value, callback) => {
    if (!value) {
      callback(new Error('信息不能为空'))
    }
    callback()
  }
  const rules = {
    title: [{ message: '标题不能为空', trigger: 'blur', required: true }],
    date_range: [{ message: '日历时间不能为空', trigger: 'blur', required: true }],
    type: [{ message: '日历类型不能为空', trigger: 'blur', required: true }],
  }
  return { rules }
}
</script>
 
<style lang="scss" scoped>
.container2 {
  .title {
    width: 100%;
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
 
    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }
 
  .wrap {
    padding: 20px;
    width: 100%;
  }
 
  .submit {
    float: left;
  }
}
</style>