<template>
    <div>
        <el-dialog v-model="dialogVisible" :close-on-click-modal="false" draggable width="1200px" height="500px">
            <el-card :body-style="cardBodyStyle">
                <el-form :inline="true" :model="query">
                    <el-form-item label="结算单号" style="margin-bottom: 5px;">
                        <el-input v-model="query.accountNo" 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.contrNo" 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.khContrNo" 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="handleSearch">查询</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-for="(item, index) in column" :key="index" :prop="item.prop" :label="item.label"
                        :width="item.width" header-align="center" align="center" show-overflow-tooltip
                        :formatter="formatter">
                    </el-table-column>
                    <el-table-column label="操作" header-align="center" align="center" show-overflow-tooltip width="200px"
                        fixed="right">
                        <template #default="{ row }">
                            <el-button plain @click="handleSelect(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">
                </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
}

onMounted(() => {
    getDictList()
    initTableData()
})

const props = defineProps({
    column: { // form查询项
        type: Array,
        default: () => [
            {
                prop: 'accountNo',
                label: '结算单号',
                width: 200,
            },
            {
                prop: 'tradePartnerCode',
                label: '供应商编号',
                width: 150,
            },
            {
                prop: 'tradePartnerName',
                label: '供应商名称',
                width: 180,
            },
            {
                prop: 'invoiceNo',
                label: '发票编号',
                width: 200,
            },
            {
                prop: 'invoiceNum',
                label: '发票号码',
                width: 150,
            },
            {
                prop: 'invoiceCnt',
                label: '发票张数',
                width: 100,
            },
            {
                prop: 'invoiceDate',
                label: '发票日期',
                width: 120,
            },
            {
                prop: 'invoiceAmount',
                label: '发票金额',
                width: 100,
            },
            {
                prop: 'transportMth',
                label: '运输方式',
                width: 100,
            },
            {
                prop: 'payMth',
                label: '付款方式',
                width: 100,
            },
            {
                prop: 'actBasis',
                label: '结算依据',
                width: 100,
            },
            {
                prop: 'actMth',
                label: '结算方式',
                width: 120,
            },
            {
                prop: 'actQty',
                label: '结算数量',
                width: 120,
            },
            {
                prop: 'totalAmountExcludeTax',
                label: '不含税金额合计',
                width: 150,
            },
            {
                prop: 'taxRate',
                label: '税率',
                width: 120,
            },
            {
                prop: 'glDateBegin',
                label: '结算起止日期',
                width: 220,
            },
            {
                prop: 'payAmount',
                label: '已付金额',
                width: 100,
            },
        ]
    },
    url: {
        type: String,
        default: 'pur/ActHead/queryPopup'
    },
    params: {
		type: Object,
		default: () => {
		}
	},
})

/**
 *  查询字典
 */
const dicList = ref({
    COM_PAY_MTH: [],
    TAX_RATE: [],
    MEASURING_UNIT: []
})
const getDictList = () => {
    var url = 'framework/Common/querySelectorList'
    var param = {
        type: JSON.stringify(['COM_PAY_MTH', 'TAX_RATE', 'MEASURING_UNIT'])
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            dicList.value = response.data.data
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
const formatter = (row, column, cellValue, index) => {
    if (!cellValue || cellValue == null || cellValue == '') {
        return cellValue
    }
    switch (column.property) {
        case 'payMth':
            cellValue = dicList.value.COM_PAY_MTH.find(item => item.dicCode == cellValue)?.dicName
            break
        case 'taxRate':
            cellValue = dicList.value.TAX_RATE.find(item => item.dicCode == cellValue)?.dicName
            break
        case 'uom':
            cellValue = dicList.value.MEASURING_UNIT.find(item => item.dicCode == cellValue)?.dicName
            break
        case 'glDateBegin':
            cellValue = row.glDateBegin + ' 至 ' + row.glDateEnd
            break
    }
    return cellValue
}
/**
 *  查询字典
 */


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

    }
})

const handleSearch = () => {
    currentPage.value = 1
    initTableData()
}

/**
 * 查询
 */
const query = ref({
    billNo: '',
    contrNo: '',
    tradePartnerCode: '',
})
const tableData = ref([])
const initTableData = () => {
    tableData.value = []
    query.value.tradePartnerCode = props.params.tradePartnerCode

    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 currentPage = ref(1)
const totalRows = ref(0)
const pageSize = ref(10)
const pageSizes = ref([10, 20, 30, 40])
const handleSizeChange = (val) => {
    pageSize.value = val
    initTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    initTableData()
}


/**
 * 选择
 */
const multipleSelection = ref([])
const tableDataLine = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
const emit = defineEmits(['receiveFromChild'])
const handleSelect = (row) => {
    emit('receiveFromChild', row)
    close()
}

defineExpose({
    open,
    close
})
</script>

<style></style>