<script name="evaluateDialog" setup>
import { getCurrentInstance, nextTick, ref, watch } from 'vue'
import { judgeDetail, judgeSubmit } from '@/http/apis/expertManage/expertStore'

const { proxy } = getCurrentInstance(),
  props = defineProps({
    visible: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    },
    isEdit: {
      type: Boolean,
      default: true
    }
  }),
  emits = defineEmits(['close']),
  formData = ref({}),
  formRef = ref(),
  rules = {
    score: [{ required: true, message: '请输入' }],
    attended: [{ required: true, message: '请选择' }],
    performance: [{ required: true, message: '请选择' }],
    advised: [{ required: true, message: '请选择' }],
    leaveEarly: [{ required: true, message: '请选择' }],
    brokeRule: [{ required: true, message: '请选择' }],
    brokeRuleContent: [{ required: true, message: '请选择' }]
  },
  loading = ref(false),
  submit = async (formEl) => {
    if (!formEl) return
    await formEl.validate(async valid => {
      if (valid) {
        loading.value = true
        try {
          const postData = {
            ...formData.value
          }
          await judgeSubmit(postData)
          proxy.$message.success('提交成功！')
          loading.value = false
          emits('close', true)
        } catch (e) {
          loading.value = false
        }
      }
    })
  },
  getDetail = async () => {
    const res = await judgeDetail(props.data.meetingExpertId)
    formData.value = {
      ...res.data
    }
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      await nextTick()
      formRef.value.clearValidate()
      formData.value = {
        meetingExpertId: props.data.meetingExpertId,
        meetingId: props.data.meetingId
      }
      if (!props.isEdit) {
        getDetail()
      }
    }
  }
)
defineExpose({ formRef })
</script>

<template>
  <el-dialog :model-value="visible" :title="isEdit?'履职评价':'查看评价'" @close="emits('close')">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-suffix="："
    >
      <el-form-item v-if="isEdit" label="评分" prop="score">
        <el-input-number
          v-model="formData.score"
          placeholder="请对该专家在本次会议的表现进行评分，最低0分，满分10分"
          :min="0"
          :max="10"
          :controls="false"
          @mousewheel.prevent
        />
      </el-form-item>
      <el-form-item v-else label="评分">
        <span>{{ formData.score }}</span>
      </el-form-item>
      <el-form-item v-if="isEdit" label="专家是否参加" prop="attended">
        <el-radio-group v-model="formData.attended">
          <el-radio :label="1">准时参加</el-radio>
          <el-radio :label="2">迟到</el-radio>
          <el-radio :label="3">缺席</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-else label="专家是否参加">
        <span>{{ formData.attended===1?'准时参加':formData.attended===2?'迟到':'缺席' }}</span>
      </el-form-item>
      <template v-if="formData.attended!==3">
        <el-form-item v-if="isEdit" label="专家参与程度" prop="performance">
          <el-radio-group v-model="formData.performance">
            <el-radio :label="1">积极</el-radio>
            <el-radio :label="2">消极</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-else label="专家参与程度">
          <span>{{ formData.performance===1?'积极':formData.performance===2?'消极':'-' }}</span>
        </el-form-item>
        <el-form-item v-if="isEdit" label="专家是否提出专业建议" prop="advised">
          <el-radio-group v-model="formData.advised">
            <el-radio :label="true">有提出</el-radio>
            <el-radio :label="false">未提出</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-else label="专家是否提出专业建议">
          <span>{{ formData.advised?'有提出':'未提出' }}</span>
        </el-form-item>
        <el-form-item v-if="isEdit" label=" 专家是否早退" prop="leaveEarly">
          <el-radio-group v-model="formData.leaveEarly">
            <el-radio :label="false">未早退</el-radio>
            <el-radio :label="true">早退</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-else label="专家是否早退">
          <span>{{ formData.leaveEarly?'早退':'未早退' }}</span>
        </el-form-item>
        <el-form-item v-if="isEdit" label="专家有无违规行为" prop="brokeRule">
          <el-radio-group v-model="formData.brokeRule">
            <el-radio :label="false">无</el-radio>
            <el-radio :label="true">有</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-else label="专家有无违规行为">
          <span>{{ formData.brokeRule?'有':'无' }}</span>
        </el-form-item>
        <template v-if="formData.brokeRule">
          <el-form-item v-if="isEdit" label="违规行为" prop="brokeRuleContent">
            <el-input
              v-model="formData.brokeRuleContent"
              placeholder="请填写"
              type="textarea"
              :maxlength="200"
              show-word-limit
            />
          </el-form-item>
          <el-form-item v-else label="违规行为">
            <span>{{ formData.brokeRuleContent }}</span>
          </el-form-item>
        </template>
      </template>
    </el-form>
    <template v-if="isEdit" #footer>
      <span class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="submit(formRef)">提交</el-button>
        <el-button @click="emits('close')">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>
