<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <SearchBox ref="SearchBoxRef" formName="计划号" :formData="query" @PopupNo="PopupNo" @reset="reset">
            </SearchBox>
            <Popup_base_wl ref="PopupRefTwo" :key="timeKey1" @receive-from-child="retu"></Popup_base_wl>
            <Popup_base_ks ref="PopupRef" :key="timeKey2" @receive-from-child="retu2"></Popup_base_ks>
            <Popup_base_ks ref="PopupRef2" :key="timeKey2" @receive-from-child="retu3"></Popup_base_ks>


        </el-card>
        <ButtonBar @search="handleSearch" @add="handleAdd" @remove="handleRemoveBatch" @edit="handleEditBatch"
            @upload="handleUpload" @download="handleDownload" @import="handleImport" @export="handleExport"
            @infoDelete="deleteRecordInfo"></ButtonBar>

        <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="fsubbillno" label="计划号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="fitemname" label="物料名称" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>



                <el-table-column v-if="false" prop="fsndstation" label="发货单位编码/港口编码" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="fsndname" label="发货单位名称/港口" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="frcvstation" label="收货单位编码" width="360" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="frcvname" label="收货单位名称" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="mustrcv" label="是否收货确认" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.mustrcv == 'Y' ? '是' : '否' }}
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="mustsnd" label="是否发货确认" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.mustsnd == 'Y' ? '是' : '否' }}
                    </template>
                </el-table-column>

                <el-table-column v-if="true" prop="gbms" label="过磅模式" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                    <template #default="scope">
                        {{ gbmstype.find(x => x.id == scope.row.gbms).name == '' ? '' :
                            gbmstype.find(x => x.id == scope.row.gbms).name }}
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="jhbj" label="计划标记" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.jhbj == '1' ? '内部计划' : '其他计划' }}
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="ftc" label="创建时间" width="200" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="fpc" label="创建人姓名" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="fuc" label="创建人账号" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>


                <el-table-column label="操作" header-align="center" align="center" fixed="right" width="200px">
                    <template #default="scope">
                        <el-button plain type="warning" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button plain type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-card :body-style="cardBodyStyle">
            <el-pagination :current-page="currentPage" :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 :close-on-click-modal="false" draggable title="编辑" v-model="dialogVisible" width="1200px" top="5vh"
            @open="openDialog">

            <el-form :model="form" label-width="80px" width="100px" label-position="right" inline ref="ruleFormRef"
                :rules="rules">

                物料名称：<el-input v-model="form.wuzimc" style="width: 230px;" @click="handleTwo" readonly>
                    <template #append>
                        <el-button icon="Search" @click="handleTwo" />
                    </template>
                </el-input>


                收货单位：<el-input v-model="form.frcvname" style="width: 230px;" @click="handle" readonly>
                    <template #append>
                        <el-button icon="Search" @click="handle" />
                    </template>
                </el-input>
                发货单位：<el-input v-model="form.fsndname" @click="handle2" style="width: 230px;" readonly>
                    <template #append>
                        <el-button icon="Search" @click="handle2" />
                    </template>
                </el-input><br /><br />
                过磅模式：<el-select v-model="form.gbms" placeholder="请选择过磅模式" size="large" style="width: 240px">
                    <el-option v-for="item in gbmstype" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>



                <!-- 是否收货确认：<el-select v-model="form.mustrcv" placeholder="是否收货确认" size="large" style="width: 240px">
                    <el-option label="是" value="Y" />
                    <el-option label="否" value="N" />
                </el-select> -->
                是否收货确认：<el-switch v-model="form.mustrcv" active-text="是" inactive-text="否" />

                是否发货确认：<el-switch v-model="form.mustsnd" active-text="是" inactive-text="否" />
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="success" @click="handleSave(ruleFormRef)">保存</el-button>
                    <el-button type="danger" @click="handleCancelSave">取消</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog v-model="dialogTableVisibleDeleteInfo" title="信息" width="1200">
            <el-table :data="records" border highlight-current-row @selection-change="handleSelectionChange">
                <el-table-column type="index" label="序号" align="center" width="60">
                    <template #default="scope">
                        <span>{{ scope.$index + 1 }}</span>
                    </template>
                </el-table-column>

                <el-table-column v-if="true" prop="fsubbillno" label="计划号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="fitemname" label="物料名称" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>



                <el-table-column v-if="false" prop="fsndstation" label="发货单位编码/港口编码" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="fsndname" label="发货单位名称/港口" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="frcvstation" label="收货单位编码" width="360" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="frcvname" label="收货单位名称" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="mustrcv" label="是否收货确认" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.mustrcv == 'Y' ? '是' : '否' }}
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="mustsnd" label="是否发货确认" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.mustsnd == 'Y' ? '是' : '否' }}
                    </template>
                </el-table-column>

                <el-table-column v-if="true" prop="gbms" label="过磅模式" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                    <template #default="scope">
                        {{ gbmstype.find(x => x.id == scope.row.gbms).name == '' ? '' :
                            gbmstype.find(x => x.id == scope.row.gbms).name }}
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="jhbj" label="计划标记" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.jhbj == '1' ? '内部计划' : '其他计划' }}
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="ftc" label="创建时间" width="200" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="fpc" label="创建人姓名" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="fuc" label="创建人账号" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>


                <el-table-column label="操作" header-align="center" align="center" fixed="right" width="200px">
                    <template #default="scope">
                        <el-button plain type="danger" @click="reloadDeleteInfo(scope.row)">恢复</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>


    </div>
</template>

<script setup>


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

const {
    proxy
} = getCurrentInstance()

import tools from '@/tools'
const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)


const dialogTableVisibleDeleteInfo = ref(false)
const records = ref([])
const deleteRecordInfo = () => {
    dialogTableVisibleDeleteInfo.value = true
    var url = 'tms/OrderTrans/queryDelete'
    var param = {

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

const reloadDeleteInfo = (tt) => {
    ElMessageBox.confirm(
        '确定恢复本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            var url = 'tms/OrderTrans/updateDelete'
            var param = {
                params: JSON.stringify(tt)
            }
            proxy.$axios.get(url, param).then(response => {
                if (response.data.code == '0') {
                    records.value = response.data.data
                    // console.log(records.value)
                    deleteRecordInfo()
                    getTableData()
                } else {
                    ElMessage.error('操作失败！' + response.data.msg)

                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })

}


/**
 * 头信息
 */

const deptCode = ref('')
const deptName = ref('')
const deptId = ref('')

/**
 * 头信息
 */
onMounted(() => {
    const temp = JSON.parse(localStorage.getItem('deptTree'))
    deptCode.value = temp[0].deptCode
    deptName.value = temp[0].deptName
    deptId.value = temp[0].id
    handleSearch()
})
const form = ref({
    frcvname: '',
    fsndname: '',
    gbms: '',
    mustrcv: '',
    mustsnd: '',
    wuzibm: '',
    wuzimc: '',
    deptCode: '',
    deptName: '',
    deptId: '',
})
const gbmstype = ref([
    { id: '1', name: '固定车皮' },
    { id: '2', name: '先皮后毛' },
    { id: '3', name: '先毛后皮' },
])
const timeKey1 = ref(null)
const timeKey2 = ref(null)
const timeKey3 = ref(null)

const PopupRef = ref(null)
const PopupRef2 = ref(null)
const PopupRefTwo = ref(null)

import ButtonBar from '@/components/ButtonBar.vue'
import Popup_base_wl from "@/views/bas/Popup_base_wl.vue"
import Popup_base_ks from "@/views/bas/Popup_base_ks.vue"

/**
 *   查询
 */
const tem = ref('')
const tem2 = ref('')
const tem3 = ref('')
const shdw = ref('')
const fhdw = ref('')
const wlmc = ref('')
const wlbm = ref('')
const retu = (a) => {
    form.value.wuzibm = a.itemCd
    form.value.wuzimc = a.itemNm
}

const retu2 = (a) => {
    tem2.value = a
    form.value.frcvname = a.shortname
}

const retu3 = (a) => {
    tem3.value = a
    form.value.fsndname = a.shortname
}



const query = ref({
    fsubbillno: '',
})
const tableData = ref([])
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}

const getTableData = () => {
    var url = 'tms/OrderTrans/query'
    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 pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(5)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */

const handleTwo = () => {
    PopupRefTwo.value.open()
}
const handle = () => {
    PopupRef.value.open()
}

const handle2 = () => {
    PopupRef2.value.open()
}


const dialogVisible = ref(false)

const ruleFormRef = ref()
const rules = reactive({
})

const handleAdd = () => {
    form.value = {
        frcvname: '',
        fsndname: '',
        gbms: '',
        mustrcv: 'N',
        mustsnd: 'N',
        wuzibm: '',
        wuzimc: '',
        deptCode: '',
        deptName: '',
        deptId: '',
    }
    dialogVisible.value = true
}
/**
 * 新增
 */


/**
 * 保存、修改、删除
 */

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
    form.value.deptId = deptId.value
    form.value.deptCode = deptCode.value
    form.value.deptName = deptName.value

    if (form.value.mustrcv) {
        form.value.mustrcv = 'Y'
    } else {
        form.value.mustrcv = 'N'
    }
    if (form.value.mustsnd) {
        form.value.mustsnd = 'Y'
    } else {
        form.value.mustsnd = 'N'
    }
    if (form.value.frcvname == '' || form.value.fsndname == '' ||
        form.value.gbms == '' || form.value.mustrcv == '' ||
        form.value.mustsnd == '' || form.value.wuzimc == ''
    ) {
        ElMessage.warning('警告：有未录入的数据，请重新录入！')

        return
    }
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'tms/OrderTrans/save'
            var param = {
                json: JSON.stringify(form.value),
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    form.value = response.data.data
                    ElMessage({
                        message: '保存成功',
                        type: 'success',
                    })
                    getTableData()
                    dialogVisible.value = false
                } else {
                    // ElMessage.error('操作失败！' + response.data.msg)
                    ElMessage({
                        message: '操作失败' + response.data.msg,
                        type: 'error'
                    })
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
const handleCancelSave = () => {
    dialogVisible.value = false
}
const handleEdit = (row) => {
    dialogVisible.value = true
    form.value = {...row}
    form.value.mustrcv = (row.mustrcv === 'Y' ? true : false)
    form.value.mustsnd = (row.mustsnd === 'Y' ? true : false)
    form.value.wuzimc = row.fitemname
}
const handleDelete = (row) => {
    ElMessageBox.confirm(
        '确定删除本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            var url = 'tms/OrderTrans/remove'
            var param = {
                id: row.id
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getTableData()
                } else {
                    ElMessage({
                        type: 'error',
                        message: '删除失败' + response.data.msg
                    });
                }
            });
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

/**
 * 保存、修改、删除
 */

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

const handleRemoveBatch = () => {
    console.log('handleRemoveBatch')

    if (multipleSelection.value.length < 1) {
        ElMessage({
            type: 'warning',
            message: '请选择要删除的数据!'
        })
        return
    }

    ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        var ids = []
        multipleSelection.value.forEach((item, index) => {
            ids.push(item.id)
        })
        var url = 'tms/OrderTrans/removeBatch'
        var param = {
            ids: JSON.stringify(ids)
        }
        proxy.$axios.post(url, param).then(response => {
            if (response.data.code == '0') {
                ElMessage({
                    type: 'success',
                    message: '操作成功!',
                });
                getTableData()
                dialogVisible.value = false
            } else {
                ElMessage({
                    type: 'error',
                    message: '操作失败!' + response.data.msg
                });
            }
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

const handleEditBatch = () => {
    console.log('handleEditBatch')
    if (multipleSelection.value.length != 1) {
        ElMessage({
            type: 'warning',
            message: '请选择一条数据编辑!'
        })
        return
    }
    handleEdit(multipleSelection.value[0])
}

const handleUpload = () => {
    console.log('handleUpload')
}

const handleDownload = () => {
    console.log('handleDownload')
}

const handleImport = () => {
    console.log('handleImport')
}

const handleExport = () => {
    console.log('handleExport')
}

</script>

<style></style>