<template>
    <div>
        <el-dialog v-model="dialogVisible" :close-on-click-modal="false" draggable width="1100px" height="500px">
            <el-card :body-style="cardBodyStyle">
                <el-form inline :model="query">
                    <el-form-item label="模版条款标题" style="margin-bottom: 5px;">
                        <el-input v-model="query.clsTitle" placeholder="请输入" style="width: 200px; margin-right: 10px;"
                            clearable></el-input>
                    </el-form-item>
                    <el-form-item label="备注" style="margin-bottom: 5px;">
                        <el-input v-model="query.remark" placeholder="请输入" style="width: 200px; margin-right: 10px;"
                            clearable></el-input>
                    </el-form-item>
                </el-form>
            </el-card>

            <el-card :body-style="cardBodyStyle">
                <el-button plain type="primary" @click="search">查询</el-button>
                <el-button plain type="success" @click="handleSelect">保存</el-button>
            </el-card>

            <el-card :body-style="cardBodyStyle">
                <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange">
                    <el-table-column type="selection" fixed></el-table-column>
                    <!-- <el-table-column type="index" label="行号" align="center" width="60">
                        <template #default="scope">
                            <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
                        </template>
</el-table-column> -->
                    <el-table-column v-if="true" prop="cmpNm" width="120" label="公司名称" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="clsType" width="120" label="款型类型" header-align="center"
                        align="center" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ dicList.CONTRACT_TYPE.find(item => item.dicCode === row.clsType)?.dicName }}
                        </template>
                    </el-table-column>
                    <el-table-column v-if="true" prop="clsTitle" label="模板条款标题" header-align="center" align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="clsCont" label="模板条款内容" header-align="center" align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="remark" label="备注" header-align="center" align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <!-- <el-table-column label="操作" header-align="center" align="center" show-overflow-tooltip width="158px"
                        fixed="right">
                        <template #default="scope">
                            <el-button plain @click="handleSelect(scope.row)">选择</el-button>
                        </template>
                    </el-table-column> -->
                </el-table>
                <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="pageSizes"
                    :total="totalRows" background="background" layout="total, sizes, prev, pager, next, jumper"
                    @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top: 10px;">
                </el-pagination>
            </el-card>
        </el-dialog>
    </div>
</template>

<script setup>

import {
    ref,
    onMounted,
    watch,
    getCurrentInstance
} from 'vue'

const {
    proxy
} = getCurrentInstance()

import tools from '@/tools'
import { ElMessage } from 'element-plus';

const cardBodyStyle = ref(tools.style.card)

const dialogVisible = ref(false)
const open = () => {
    dialogVisible.value = true
}
const close = () => {
    dialogVisible.value = false
}

const props = defineProps({
    url: {
        type: String,
        default: 'cot/ConTmpClause/queryPopup'
    },
    params: {
        type: Object,
        required: true
    }
})
const query = ref({
    clsType: '',
    clsTitle: '',
    remark: '',
})

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

const emit = defineEmits(['receiveFromChild'])
const handleSelect = () => {
    if (multipleSelection.value.length < 1) {
        ElMessage({
            type: 'warning',
            message: '至少选择一条数据保存'
        })
        return
    }


    emit('receiveFromChild', multipleSelection.value)
    close()
}
const currentPage = ref(1)
const pageSize = ref(10)
const pageSizes = ref([10, 20, 30, 40])
const tableData = ref([])
const totalRows = ref()
const initTableData = () => {
    tableData.value = []

    query.value.cmpNm = props.params.cmpNm
    query.value.clsType = props.params.clsType

    var url = props.url
    var param = {
        page: currentPage.value,
        rows: pageSize.value,
        params: JSON.stringify(query.value)
    }

    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableData.value = response.data.data.records
            totalRows.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}

const search = () => {
    initTableData()
}
const handleSizeChange = (val) => {
    pageSize.value = val
    initTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    initTableData()
}

onMounted(() => {
    initTableData()
    getDictList()
})
watch(dialogVisible, (newValue, oldValue) => {
    if (newValue) {
        initTableData()
    } else {

    }
})
defineExpose({
    open,
    close
})


/**
 *  查询字典
 */
const dicList = ref({})
const getDictList = () => {
    var url = 'framework/Common/querySelectorList'
    var param = {
        type: JSON.stringify(['CONTRACT_TYPE'])
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            dicList.value = response.data.data
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
 *  查询字典
 */
</script>

<style></style>