<script name="leaveDialog" setup>
import { ref, getCurrentInstance, watch } from 'vue'
import { leaveSave } from '@/http/apis/expertManage/expertReview'
import { storeToRefs } from 'pinia'
import store from '@/store'
const userInfo = storeToRefs(store.userStore).userInfo
const
  { proxy } = getCurrentInstance(),
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: {
      type: Object,
      default: undefined
    },
    title: {
      type: String,
      default: undefined
    }
  }),
  emits = defineEmits(['close']),
  formRef = ref(),
  form = ref({
  }),
  rules = {
    postscript: [{ required: true, message: '请输入请假原因', trigger: 'blur' }]
  },
  submit = async formEl => {
    if (!formEl) {
      return
    }
    await formEl.validate(async valid => {
      if (valid) {
        await leaveSave({
          ...form.value,
          type: 3,
          expertId: props.data.expertId || userInfo.value.userId,
          meetingId: props.data.meetingId
        })
        proxy.$message.success('提交成功！')
        emits('close', true)
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      console.log(props.data)
    } else {
      formRef.value.resetFields()
      form.value = {}
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    :title="props.title||`填写请假原因-${data?.meetingName}`"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="auto"
      label-suffix=":"
    >
      <el-row>
        <template v-if="data?.connecter">
          <el-col :span="24">
            <el-form-item
              label="联系人"
            >
              {{ data?.connecter }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="联系方式"
            >{{ data?.contact }}</el-form-item>
          </el-col>
        </template>
        <el-col :span="24">
          <el-form-item
            label="请假原因"
            prop="postscript"
          >
            <el-input
              v-model="form.postscript"
              maxlength="200"
              placeholder="请输入"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="flex justify-center">
        <el-button
          class="ml-22"
          type="primary"
          @click="submit(formRef)"
        >
          提交
        </el-button>
        <el-button
          @click="emits('close')"
        >
          关闭
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
