<template>
  <div class="layout_container" style="height: 550px;">
    <div style="height: 500px;">
      <div class="line-wrapper">
        <div class="line-left">患者姓名：</div>
        <div class="line-right">{{data.patName}}</div>
      </div>
      <div class="line-wrapper">
        <div class="line-left">住院号：</div>
        <div class="line-right">{{data.patNo}}</div>
      </div>
      <div class="line-wrapper">
        <div class="line-left">编码员姓名：</div>
        <div class="line-right">{{data.coderName}}</div>
      </div>
      <div class="line-wrapper" style="align-items: flex-start">
        <div class="line-left">
          主要错误：
        </div>
        <div class="line-right">
          <el-checkbox-group v-model="majorError">
            <div>
              <el-checkbox value="主要诊断填写错误">主要诊断填写错误</el-checkbox>
            </div>
            <div>
              <el-checkbox value="主要诊断编码错误">主要诊断编码错误</el-checkbox>
            </div>
            <div>
              <el-checkbox value="主要手术填写错误">主要手术填写错误</el-checkbox>
            </div>
            <div>
              <el-checkbox value="主要手术编码错误">主要手术编码错误</el-checkbox>
            </div>
          </el-checkbox-group>
        </div>
      </div>
      <div class="line-wrapper">
        <div class="line-left">
          <span class="required">*</span>审核状态：
        </div>
        <div class="line-right">
          <el-radio-group v-model="auditState">
            <el-radio value="APPROVED">通过</el-radio>
            <el-radio value="REJECTED">不通过</el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="line-wrapper" style="align-items: flex-start">
        <div class="line-left">
          <span class="required">*</span>审核意见：
        </div>
        <div class="line-right">
          <el-input
              v-model="auditRemark"
              type="textarea"
              :rows="5"
              :placeholder="placeholder"
          />
        </div>
      </div>
      <div class="line-wrapper" style="align-items: flex-start">
        <div class="line-left">
        </div>
        <div class="line-right">
          <el-button
              @click="handleClickConfirm"
              size="default"
              type="primary"
              style="width: 100%"
          >
            提交审核
          </el-button>
          <div style="width: 100%; text-align: right; margin-top: 20px">
            <a href="#" style="text-decoration: underline" @click="viewAuditHistory">
              审核历史
            </a>
          </div>
        </div>
      </div>
    </div>

    <el-drawer v-model="showAuditHistory" title="组长审核历史记录">
      <el-timeline>
        <el-timeline-item
            v-for="item in histories"
            :timestamp="item.createTime"
            placement="top"
        >
          <el-card>
            <h4>
              编码员：{{ item.coderName }}
            </h4>
            <p v-if="item.majorError" class="major-error">主要错误：{{item.majorError}}</p>
            <p class="audit-remark">审核结果：{{ item.auditRemark }}</p>
            <p style="font-size: 12px; color: dimgray">
              <span>审核人：{{ item.auditName }}</span>
              <span style="margin-left: 32px">审核时间：{{ item.createTime }}</span>
            </p>
            <img :src="item.auditState === 'APPROVED' ? approveImg : rejectImg" alt="" class="img-state">
          </el-card>
        </el-timeline-item>
      </el-timeline>
      <div
          v-if="histories.length === 0"
          style="font-size: 36px;
          height: 86%;
          color: #777777;
          display: flex;
          align-items: center;
          justify-content: center"
      >
        没有有效的质控记录
      </div>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import {xcMessage} from "@/utils/xiaochan-element-plus";
import {submitLeaderAudit,fetchLeaderAudit} from "@/api/case-front-sheet";
import approveImg from "@/assets/approved.png";
import rejectImg from "@/assets/reject.png";

const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
  closeModal: {
    type: Function,
  }
})

const auditRemark = ref('')
const auditState = ref('APPROVED')
const majorError = ref([])
const placeholder = computed(() => {
  return auditState.value === 'APPROVED' ? '审核通过' : '请在此输入审核意见'
})

function handleClickConfirm() {
  if (auditState.value === 'REJECTED' && !auditRemark.value) {
    xcMessage.error('审核意见不能为空！');
    return
  }
  if (auditState.value === 'APPROVED' && majorError.value.length > 0) {
    xcMessage.error('勾选了主要错误，审核状态请选择【不通过】')
    return
  }
  props.data.auditState = auditState.value
  props.data.auditRemark = auditRemark.value
  props.data.majorErrorList = majorError.value
  submitLeaderAudit(props.data).then(res => {
    xcMessage.success('操作成功。')
    props.closeModal()
  })
}

const showAuditHistory = ref(false);
const histories = ref([])
function viewAuditHistory() {
  fetchLeaderAudit(props.data).then(res => {
    histories.value = res
    showAuditHistory.value = true;
  })
}

</script>

<style scoped>
.line-wrapper {
  display: flex;
  margin: 12px 0;
  align-items: center;
}
.line-left {
  width: 120px;
  text-align: right;
  padding-right: 8px;
}
.line-right {
  width: 300px;
  text-align: left;
  padding-left: 30px;
}
.required {
  margin-right: 4px;
  color: red;
}
.img-state {
  width: 64px;
  position: absolute;
  top: 0;
  right: 0;
}
</style>