<template>
    <div id="submit-report-layout-target" class="hc-page-box conclusion">
        <hc-new-card>
            <template #header>
                <div class="hc-conclusion-header-box">
                    <div class="hc-conclusion-icon-box">
                        <HcIcon name="file-ppt-2" fill />
                    </div>
                    <div class="ml-2 conclusion-name-box">
                        <span class="text-xl text-cut conclusion-alias">编写结论、提交报告</span>
                        <div class="text-xs text-cut conclusion-name">{{ projectInfo.name }}</div>
                    </div>
                </div>
            </template>
            <template #extra>
                <el-button type="danger" round @click="writeReportClick">下一步、编写报告</el-button>
                <el-button type="danger" plain round @click="toBackClick">返回主页</el-button>
            </template>

            <el-container class="hc-table-layout">
                <el-container id="hc_table_container" class="hc-table-container">
                    <HcCardItem id="hc_table_data" title="抽检统计：">
                        <template #extra>
                            <span class="text-red">{{ tips }}</span>
                        </template>
                        <TableStats :contract-id="contractId" :project-id="projectId" @change-tips="changeTips" />
                    </HcCardItem>
                    <HcCardItem id="hc_table_score" title="专家验收评分表：">
                        <template #header>
                            <span>专家验收评分表：</span>
                            <el-checkbox v-model="searchFormScore.score" class="size-xl" @change="changeScore">生成打分表</el-checkbox>
                        </template>
                        <template #extra>
                            <div class="w-40">
                                <el-select v-model="searchFormScore.unitType" placeholder="参建方类型表">
                                    <el-option label="总承包单位" value="1" />
                                    <el-option label="监理单位" value="2" />
                                    <el-option label="施工单位" value="3" />
                                </el-select>
                            </div>
                        </template>
                        <TableScore :contract-id="contractId" :project-id="projectId" :type="searchFormScore.unitType" :state="state" />
                    </HcCardItem>
                </el-container>
                <el-aside id="hc_table_aside" class="hc-table-aside">
                    <HcCardItem title="抽检意见记录汇总：">
                        <TableOpinion ref="tableOpinionRef" :contract-id="contractId" :project-id="projectId" @opinion-tap="tableOpinionTap" />
                    </HcCardItem>
                </el-aside>
            </el-container>
        </hc-new-card>

        <!-- 抽检记录详情 -->
        <hc-new-dialog v-model="isOpinionModal" title="抽检记录详情" widths="60%" is-table :footer="false" @close="onOpinionModalClose">
            <div class="hc-table-opinion-text">
                <div class="title">案卷题名：{{ opRow.archiveName }}</div>
                <div class="opinion-text">
                    <div class="text-1">抽检意见：</div>
                    <div v-if="!opinionTextEdit" class="text-2">{{ opinionText }}</div>
                    <div v-if="opinionTextEdit" class="text-2 input">
                        <el-input v-model="opinionText" style="flex: 1;" :rows="3" placeholder="请输入抽检意见" type="textarea" resize="none" />
                        <el-button class="ml-4" hc-btn type="primary" :loading="submitLoading" @click="opinionTextEditHihe">保存</el-button>
                    </div>
                    <div v-if="!opinionTextEdit " class="text-blue text-hover icon-1" @click="opinionTextEditShow">
                        <HcIcon v-if="!state" name="edit-2" />
                    </div>
                </div>
            </div>
            <div class="hc-table-opinion-pdf">
                <HcPdf
                    :src="opRow.filePdf"
                />
            </div>
        </hc-new-dialog>
    </div>
</template>

<script setup>
import { onActivated, onMounted, ref } from 'vue'
import { useAppStore } from '~src/store'
import { useRouter } from 'vue-router'
import TableStats from './components/conclusion/table-stats.vue'
import TableOpinion from './components/conclusion/table-opinion.vue'
import TableScore from './components/conclusion/table-score.vue'
import initialgApi from '~api/initial/initial'
import writeApi from '~api/transfer/write-conclusion'
import { isNullES } from 'js-fast-way'

//变量
const router = useRouter()
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId)
const contractId = ref(useAppState.getContractId)
const projectInfo = ref(useAppState.getProjectInfo)

//渲染完成
onMounted(() => {
    setSplitRef()
    getScore()
   
})
onActivated(()=>{
    getStatus()
})
//初始化设置拖动分割线
const setSplitRef = () => {
    //配置参考： https://split.js.org/#/?direction=vertical&snapOffset=0
    setTimeout(() => {
        window.$split(['#hc_table_container', '#hc_table_aside'], {
            sizes: [80, 20],
            snapOffset: 0,
            minSize: [400, 600],
        })
        window.$split(['#hc_table_data', '#hc_table_score'], {
            direction: 'vertical',
            snapOffset: 0,
            minSize: 200,
        })
    }, 800)
}
//获取是否可以修改
const state = ref(false)
const getStatus = async ()=>{
    const { error, code, data } = await initialgApi.getTable({
        projectId: projectId.value,
    })
    if (!error && code === 200) {
        state.value = data['status'] === 2 ? true : false
    } 
}
const searchFormScore = ref({
    unitType: '1', score: false,
})
//获取是否打分
const getScore = async ()=>{
    const { error, code, data, msg } = await writeApi.creatScore({
     projectId:projectId.value,

    })
    if (!error && code === 200) {
       if (data === 1) {
        searchFormScore.value.score = true
       } else {
        searchFormScore.value.score = false
       }
    } else {
        searchFormScore.value.score = false
    }
}
const changeScore = async (val)=>{
    const { error, code, data, msg } = await writeApi.updateScore({
     projectId:projectId.value,
     isSelect:val ? 1 : 0,
    })
    if (!error && code === 200) {
        window.$message.success(msg)
    }
}
//查看意见
const isOpinionModal = ref(false)
const opRow = ref({})
const tableOpinionTap = (row) => {
    isOpinionModal.value = true
    opRow.value = row
    opinionText.value = row.opinion
}
const onOpinionModalClose = () => {
    isOpinionModal.value = false
    tableOpinionRef.value.getTableData()
}
const tableOpinionRef = ref(null)
//抽检记录意见
const opinionText = ref('')
const opinionTextEdit = ref(false)

const opinionTextEditShow = () => {
    opinionTextEdit.value = true
}
const submitLoading = ref(false)
const opinionTextEditHihe = async () => {
    submitLoading.value = true
    const { error, code, data, msg } = await initialgApi.saveInspect({
     ... opRow.value,
     opinion:opinionText.value,

    })
    submitLoading.value = false
    if (!error && code === 200) {
        window.$message.success(msg)
      

    }
    if (isNullES(opinionText.value)) {
        onOpinionModalClose()
    }
    opinionTextEdit.value = false
  
}

//编写报告
const writeReportClick = async () => {
    const { error, code, data, msg } = await writeApi.checkTable({
        projectId: projectId.value,
    })
 
    if (!error && code === 200) {
            if (data) {
                 router.push({
                    name: 'transfer-write-report',
                })
            } 
     
    }
}

//返回
const toBackClick = () => {
    router.push({
        name: 'transfer-initial-expert',
    })
}
//预警提示
const tips = ref('预警提示：抽检率未达到验收要求，目前至抽检了4.5%')
const changeTips = (val)=>{
    tips.value = val
}
</script>

<style lang="scss" scoped>
@import '~style/transfer/scoped/submit-report.scss';

.hc-conclusion-header-box {
    position: relative;
    display: flex;
    align-items: flex-start;
    .hc-conclusion-icon-box {
        font-size: 28px;
        color: var(--el-color-primary);
    }
    .conclusion-name-box {
        flex: auto;
        position: relative;
        overflow: hidden;
        .conclusion-alias {
            color: var(--el-color-primary);
        }
        .conclusion-name {
            margin-top: 4px;
            color: #838791;
        }
    }
}
.hc-table-opinion-text {
    position: relative;
    color: #010101;
    font-size: 16px;
    margin-bottom: 10px;
    .title {
        position: relative;
        margin-bottom: 10px;
    }
    .opinion-text {
        position: relative;
        display: flex;
        align-items: flex-start;
        height: 73px;
        .text-2 {
            position: relative;
        }
        .text-2.input {
            position: relative;
            display: flex;
            align-items: flex-end;
            flex: 1;
        }
        .icon-1 {
            position: relative;
            margin-top: 3px;
            margin-left: 12px;
        }
    }
}
.hc-table-opinion-pdf {
    position: relative;
    height: calc(100% - 118px);
}
</style>

<style lang="scss">
@import '~style/transfer/submit-report.scss';
.hc-page-box.conclusion .hc-table-layout .hc-table-aside {
    width: var(--el-aside-width,600px);
}
</style>
