<template>
    <xc-dialog-v2
            v-model="dialog"
            title="申请解锁病历质控限制"
            @closed="emits('closed')"
            show-button
            @cancel="emits('closed')"
            @confirm="confirm">
        <el-form label-width="80" label-position="right">

            <div style="color: red">
                注意：
                请医生备注需要整改的内容。所有病历整改，须在基于事实的基础上进行，不得篡改。所有修改均将留痕，各位务必严谨、谨慎对待。
            </div>


            <el-form-item label="解除限制：">
                <el-checkbox-group v-model="data.unlockArray">
                    <el-checkbox :label="item" v-for="(item,index) in limit "/>
                </el-checkbox-group>
            </el-form-item>
            <el-form-item label="有效时间：">
                <el-date-picker v-model="data.effectiveTime"
                                type="datetime"
                                disabled
                                :disabled-date="disDate"
                                style="width: 180px"
                                format="YYYY-MM-DD HH:mm:ss"
                                value-format="YYYY-MM-DD HH:mm:ss"/>
                审核通过后 【{{ data.effectiveTime }}】后该申请失效。
            </el-form-item>
            <el-form-item label="申请备注：">
                <el-input v-model="data.requestRemarks"
                          type="textarea"
                          :autosize="{ minRows:3, maxRows: 6 }"
                          :maxlength="100"
                          show-word-limit/>
            </el-form-item>
        </el-form>
    </xc-dialog-v2>
</template>

<script setup name="EmrDialog">
import XcDialogV2 from "@/components/xiao-chan/dialog/XcDialogV2.vue";
import {requestToUnlockMedicalRecords} from "@/api/zhu-yuan-yi-sheng/emr-control-rule";
import {getServerDateApi} from "@/api/public-api";
import {getDateRangeFormatDate, getFormatDatetime} from "@/utils/date";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import {stringIsBlank} from "@/utils/blank-utils";
import {
    query
} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";
import moment from "moment";

const props = defineProps({
    newDate: null
})

const emits = defineEmits(['closed'])
const dialog = ref(true)

const limit = ['转科创建病历', '出院编辑']
const data = ref({
    unlockJson: '',
    unlockArray: [],
    requestRemarks: '',
    effectiveTime: ''
})


const confirm = async () => {
    if (data.value.unlockArray.length === 0) {
        return xcMessage.error('请选择需要解锁的规则。')
    }
    if (stringIsBlank(data.value.requestRemarks)) {
        return xcMessage.error('请输入解锁备注。')
    }
    if (stringIsBlank(data.value.effectiveTime)) {
        return xcMessage.error('请输入有效时间。')
    }
    data.value.unlockJson = JSON.stringify(data.value.unlockArray)
    data.value.patNo = query.value.patNo
    data.value.times = query.value.times
    let res = await requestToUnlockMedicalRecords(data.value);
    emits('closed')
}


const disDate = (time) => {
    return time.getTime() < props.newDate.getTime();
}

onMounted(async () => {
    let d = await getServerDateApi();
    data.value.effectiveTime = getFormatDatetime(moment(d).add(3, 'day'))
})

</script>

<style scoped lang="scss">

</style>
