<template>
  <el-dialog :title="title" v-model="visible" width="500px" :before-close="closeEvaluateDialog">
    <el-form :model="evaluateDialog.form" ref="evaluateFormRef" :rules="evaluateDialog.rules" label-width="120px">
      <el-form-item label="业务水平：" prop="busLevelScore">
        <el-rate
          :disabled="evaluateDialog.form.isPresent == '0'"
          v-model="evaluateDialog.form.busLevelScore"
          show-score
          text-color="#ff9900"
          score-template="{value} 分"
        ></el-rate>
      </el-form-item>
      <el-form-item label="工作态度：" prop="workAttitudeScore">
        <el-rate
          :disabled="evaluateDialog.form.isPresent == '0'"
          v-model="evaluateDialog.form.workAttitudeScore"
          show-score
          text-color="#ff9900"
          score-template="{value} 分"
        ></el-rate>
      </el-form-item>
      <el-form-item label="合规评审：" prop="rightApprovalScore">
        <el-rate
          :disabled="evaluateDialog.form.isPresent == '0'"
          v-model="evaluateDialog.form.rightApprovalScore"
          show-score
          text-color="#ff9900"
          score-template="{value} 分"
        ></el-rate>
      </el-form-item>
      <el-form-item label="出席情况：" prop="isPresent">
        <el-radio-group v-model="evaluateDialog.form.isPresent" :disabled="xmEvaluateStatus == 0">
          <el-radio label="1"> 出席 </el-radio>
          <el-radio label="0"> 未出席 </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="专家评语：" prop="expertComment">
        <el-input
          v-model="evaluateDialog.form.expertComment"
          :rows="5"
          resize="none"
          type="textarea"
          maxlength="200"
          show-word-limit
          placeholder="请输入专家评语"
        ></el-input>
      </el-form-item>
      <el-form-item label="线下评价文件：" prop="evaluateFile">
        <el-upload
          ref="uploadRef"
          :headers="upload.headers"
          :action="upload.url"
          :limit="1"
          accept=".xls,.xlsx,.doc,.docx,.txt,.zip,.pdf"
          v-model:file-list="evaluateDialog.form.evaluateFile"
          :auto-upload="true"
          drag
          style="flex: 1"
          :on-exceed="handleExceed"
        >
          <el-icon class="el-icon--upload">
            <i-ep-upload-filled />
          </el-icon>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <template #tip>
            <div class="text-center el-upload__tip">
              <span>仅允许导入.xls,.xlsx,.doc,.docx,.txt,.zip,.pdf 格式文件。</span>
            </div>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="closeEvaluateDialog">取消</el-button>
        <el-button type="primary" @click="confirmEvaluateDialog">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { setEvaluate } from '@/api/evaluate/evaluateDialog';
import { globalHeaders } from '@/utils/request';
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  row: {
    type: Object,
    default: () => ({})
  },
  type: {
    type: String,
    default: ''
  }
});
const xmEvaluateStatus = computed(() => props.row?.xmEvaluateStatus ?? 1);
const title = computed(() => {
  return '专家评价';
  // return props.type == 'mutual' ? '专家评价' : '';
});
const upload = {
  headers: globalHeaders(),
  url: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload'
};
const emit = defineEmits(['update:modelValue', 'updatelist']);
const visible = computed({
  get() {
    return props.modelValue;
  },
  set(val) {
    emit('update:modelValue', val);
  }
});
const evaluateFormRef = ref(null);
const evaluateDialog = ref({
  form: {
    busLevelScore: 0,
    workAttitudeScore: 0,
    rightApprovalScore: 0,
    isPresent: '1',
    expertComment: '',
    evaluateFile: []
  },
  rules: {
    busLevelScore: [{ required: true, message: '请选择业务水平分数', trigger: 'change' }],
    workAttitudeScore: [{ required: true, message: '请选择工作态度分数', trigger: 'change' }],
    rightApprovalScore: [{ required: true, message: '请选择合规评审分数', trigger: 'change' }],
    isPresent: [{ required: true, message: '请选择是否出席', trigger: 'change' }],
    expertComment: [{ required: true, message: '请输入专家评语', trigger: 'blur' }]
  }
});
watchEffect(() => {
  if (visible.value) {
    evaluateDialog.value.form.isPresent = xmEvaluateStatus.value.toString();
  }
});
watch(
  () => evaluateDialog.value.form.isPresent,
  (val) => {
    if (val == '0') {
      evaluateDialog.value.form.busLevelScore = 0;
      evaluateDialog.value.form.workAttitudeScore = 0;
      evaluateDialog.value.form.rightApprovalScore = 0;
    }
  }
);
const uploadRef = ref();
const closeEvaluateDialog = () => {
  evaluateFormRef.value.resetFields();
  emit('update:modelValue', false);
};
const handleExceed = () => {
  ElNotification({
    title: '提示',
    message: '最多只能上传一个文件',
    type: 'error'
  });
};
const confirmEvaluateDialog = () => {
  evaluateFormRef.value.validate(async (valid) => {
    if (!valid) return;
    const params = {
      'evaluateUser': props.row.userId,
      'itemId': props.row.itemId,
      'itemStage': props.row.itemStage,
      ...evaluateDialog.value.form,
      'evaluateFile': evaluateDialog.value.form.evaluateFile?.[0]?.response.data.ossId || ''
    };
    let res = await setEvaluate(params);
    if (res.code != 200) {
      ElNotification({
        title: '提示',
        message: res.msg,
        type: 'error'
      });
      return;
    }

    ElNotification({
      title: '提示',
      message: '评价成功',
      type: 'success'
    });
    emit('updatelist');
    closeEvaluateDialog();
  });
};
</script>
