<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import AppUpload from '@/components/base/AppUpload.vue'
import type { FormInstance, FormRules } from 'element-plus'
import { replyPosts } from '../api'

const ruleFormRef = ref<FormInstance>()
const emit = defineEmits<Emits>()
const props = defineProps({
  isShowReplyDialog: {
    type: Boolean
  },
  discussionId: {
    type: String
  }
})
interface Emits {
  (e: 'update:isShowReplyDialog', isShowReplyDialog: boolean): void
  (e: 'create'): void
}
const loading = ref(false)
const form: any = reactive({
  content: '',
  files: []
})

const rules = reactive<FormRules>({
  content: [{ required: true, message: '请填写正文内容', trigger: 'blur' }]
})

const videoLimit = computed(() => {
  return form.files.filter((item: any) => item.url.includes('.mp4'))
})
const imgLimit = computed(() => {
  return form.files.filter(
    (item: any) => item.url.includes('.png') || item.url.includes('.jpg') || item.url.includes('.jpeg')
  )
})

const beforeUploadFiles = (file: any) => {
  console.log(file)
  if (file.type.includes('video/') && file.size > 104857600) {
    ElMessage.warning('视频大小不能超过100MB')
    return false
  }
  if (file.type.includes('image/') && file.size > 2097152) {
    ElMessage.warning('图片大小不能超过2MB')
    return false
  }
  if (imgLimit.value.length >= 10 && (file.url.includes('.png') || file.url.includes('.jpg') || file.url.includes('.jpeg'))) {
    ElMessage.warning('最多只能上传10张图片')
    return false
  }
  if (videoLimit.value.length >= 1 && file.url.includes('.mp4')) {
    ElMessage.warning('最多只能上传1个视频')
    return false
  }
}
// 取消
const handleCancel = () => {
  emit('update:isShowReplyDialog', false)
}
// 确认
const handleConfirm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(valid => {
    if (valid) {
      loading.value = true

      form.files = JSON.stringify(form.files)
      const params: any = Object.assign({ discussion_id: props.discussionId, reply_type: '1', reply_id: '' }, form)
      replyPosts(params).then(() => {
        loading.value = false

        emit('update:isShowReplyDialog', false)
        emit('create')
      })
    }
  })
}
</script>
<template>
  <el-dialog :model-value="isShowReplyDialog" draggable title="回复新楼层" :before-close="handleCancel">
    <div style="max-height: 35vh; padding-right: 20px; overflow-y: auto">
      <el-form
        :model="form"
        label-position="right"
        label-width="auto"
        :rules="rules"
        ref="ruleFormRef"
        label-suffix="："
      >
        <el-form-item label="正文内容" prop="content">
          <el-input
            v-model="form.content"
            maxlength="500"
            placeholder="请输入正文内容"
            show-word-limit
            type="textarea"
          />
        </el-form-item>
        <el-form-item prop="files">
          <AppUpload v-model="form.files" accept="image/*,video/*" :beforeUploadFiles="beforeUploadFiles">
            <template #tip
              >支持最多上传10张图片，格式支持jpg，jpeg，png，2MB以内<br />视频最多上传1个，100Mb以内
            </template>
          </AppUpload>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <span>
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm(ruleFormRef)" :loading="loading">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.upload-video {
  display: flex;
  align-items: center;
  .upload-btn {
    position: absolute;
    top: 0;
    left: 0;
    width: 94px;
    line-height: 36px;
    background: rgba(250, 223, 230, 0.39);
    border: 1px solid #aa1941;
    border-radius: 20px;
    font-size: 14px;
    color: #aa1941;
    margin-right: 30px;
    text-align: center;
  }
}
.tips {
  font-size: 12px;
  color: #989898;
}
:deep(.el-upload-list__item) {
  margin-right: 20px;
}
</style>
