<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <!-- <SearchBox ref="SearchBoxRef" formName="计划号" :formData="queryM" @PopupNo="PopupNo" @reset="reset">
            </SearchBox> -->
            <el-select v-model="classesa" clearable placeholder="请选择班次" size="large" style="width: 240px" @change="classesChange">
                <el-option v-for="item in tableDataClasses" :key="item.id" :label="item.name" :value="item.name" />
            </el-select>
        </el-card>
        <ButtonBar @search="handleSearchM" @add="handleAddM" @remove="handleRemoveBatchM" @edit="handleEditBatchM">
        </ButtonBar>
        <el-card :body-style="cardBodyStyle">
            <el-table :data="tableDataM" @cell-click="showUnitInput" border highlight-current-row
                @selection-change="handleSelectionChange">

                <el-table-column type="index" label="序号" align="center" width="60">
                    <template #default="scope">
                        <span>{{ scope.$index + (currentPageM - 1) * pageSizeM + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="planNumber" label="计划号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="{ row, column }">
                        <!-- <el-input v-if="tableRowEditId === row.id && tableColumnEditIndex === column.id"
                            @blur="blurValueInput(row, column)" @keyup.enter="blurValueInput(row, column)"
                            v-model="row.planNumber" ref="planNumberRef" />
                        <el-button type="primary" @click="selectPlanPre">选择计划</el-button> -->
                        <el-button v-if="tableRowEditId === row.id && tableColumnEditIndex === column.id"
                            @click="selectPlanPreA(row, column)" type="text">
                            {{ row.planNumber }}
                        </el-button>
                        <span v-else>{{ row.planNumber }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="carNumber" label="车号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="{ row, column }">
                        <!-- <el-input v-if="tableRowEditId === row.id && tableColumnEditIndex === column.id"
                            @blur="blurValueInput(row, column)" @keyup.enter="blurValueInput(row, column)"
                            v-model="row.carNumber" ref="carNumberRef" /> -->
                        <el-button v-if="tableRowEditId === row.id && tableColumnEditIndex === column.id"
                            @click="selectCarPreB(row, column)" type="text">
                            {{ row.carNumber }}
                        </el-button>
                        <span v-else>{{ row.carNumber }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="driverName" label="司机姓名" width="100" header-align="center"
                    align="center" show-overflow-tooltip>
                    <template #default="{ row, column }">
                        <el-button v-if="tableRowEditId === row.id && tableColumnEditIndex === column.id"
                            @click="selectDriverPreC(row, column)" type="text">
                            {{ row.driverName }}
                        </el-button>
                        <span v-else>{{ row.driverName }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="driverPhone" label="司机联系方式" width="150" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="classes" label="班次" width="100" header-align="center" align="center"
                    show-overflow-tooltip>
                    <template #default="{ row, column }">
                        <el-button v-if="tableRowEditId === row.id && tableColumnEditIndex === column.id"
                            @click="selectClassesPreD(row, column)" type="text">
                            {{ row.classes }}
                        </el-button>
                        <span v-else>{{ row.classes }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="startTime" label="开始时间" width="180" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="endTime" label="结束时间" width="180" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column label="操作" header-align="center" align="center" width="100px">
                    <template #default="scope">
                        <!-- <el-button plain type="warning" @click="handleEditM(scope.row)">编辑</el-button> -->
                        <el-button plain type="danger" @click="handleDeleteM(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-card :body-style="cardBodyStyle">
            <el-pagination :current-page="currentPageM" :page-size="pageSizeM" :page-sizes="pageSizesM"
                :total="totalRowsM" background="background" layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChangeM" @current-change="handleCurrentChangeM">
            </el-pagination>
        </el-card>
        <!-- 新增 -->
        <el-dialog :close-on-click-modal="false" draggable title="编辑" v-model="dialogVisibleM" width="1200px" top="5vh"
            @open="openDialog">


            <el-button type="primary" @click="handleSearchB">白班</el-button>
            <el-button type="primary" @click="handleSearchZ">中班</el-button>
            <el-button type="primary" @click="handleSearchY">夜班</el-button>
            <el-card :body-style="cardBodyStyle">

                日期：<el-date-picker v-model="selecttime" type="date" value-format="YYYY-MM-DD" format="YYYY-MM-DD"
                    style="width: 200px;" @change="changeTime"></el-date-picker><br /><br />
                车号：<el-input disabled v-model="carNumber" style="max-width: 240px;" placeholder="请选择车辆"
                    class="input-with-select">
                    <template #append>
                        <el-button icon="Search" @click="selectCarPre" />
                    </template>
                </el-input>&#160;&#160;
                司机：<el-input disabled v-model="driverName" style="max-width: 240px" placeholder="请选择司机"
                    class="input-with-select">
                    <template #append>
                        <el-button icon="Search" @click="selectDriverPre" />
                    </template>
                </el-input><br /><br />
                计划业务：<el-input disabled v-model="planNumber" style="max-width: 240px" placeholder="请选择计划号"
                    class="input-with-select">
                    <template #append>
                        <el-button icon="Search" @click="selectPlanPre" />
                    </template>
                </el-input>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;

            </el-card>

            <el-card :body-style="cardBodyStyle">
                <el-table height="400px" :data="tableDataPc" border highlight-current-row
                    @selection-change="handleSelectionChange">

                    <el-table-column v-if="false" prop="id" label="主键" width="120" header-align="center" align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="planNumber" label="计划号" width="120" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="carNumber" label="车号" width="120" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="driverName" label="司机姓名" width="100" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="false" prop="timeEffective" label="有效时间" width="150" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="driverPhone" label="司机联系方式" width="150" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="classes" label="班次" width="70" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="false" prop="timeEffective" label="有效时间" width="180" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="startTime" label="开始时间" width="180" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" prop="endTime" label="结束时间" width="180" header-align="center"
                        align="center" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column v-if="true" label="操作" width="172" header-align="center" align="center"
                        show-overflow-tooltip fixed="right">
                        <template #default="{ row }">
                            <el-button type="info" plain @click="DeletePc(row)">取消</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
            <!-- <el-card :body-style="cardBodyStyle">
    <el-pagination :current-page="currentPagePc" :page-size="pageSizePc" :page-sizes="pageSizesPc"
        :total="totalRowsPc" background="background" layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChangePc" @current-change="handleCurrentChangePc">
    </el-pagination>
</el-card> -->




            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="handleSave">保存</el-button>
                    <el-button type="danger" @click="handleCancelSaveM">取消</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 选车 -->
        <el-dialog v-model="dialogSelectCar" title="车辆信息" width="800">
            <div style="display:flex;align-items:center;">
                车号：<el-input v-model="queryCl.carno" clearable style="width: 220px;" placeholder="请输入车号..." />
                &nbsp&nbsp<el-button type="primary" @click="handleSearchCl">查询</el-button>
            </div>
            <br />

            <el-table height="220px" :data="tableDataCl" border highlight-current-row
                @selection-change="handleSelectionChange">
                <el-table-column type="index" label="序号" align="center" width="60">
                    <template #default="scope">
                        <span>{{ scope.$index + (currentPageCl - 1) * pageSizeCl + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column v-if="true" prop="carno" label="车号" width="120" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carStatus" label="车辆状态" width="80" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="false" prop="carTypeid" label="车型id" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carType" label="车型" width="120" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carFdjh" label="发动机号" width="120" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carPfbz" label="排放标准" width="80" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="carBearing" label="轴承" width="80" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>

                <el-table-column fixed="right" label="操作" header-align="center" align="center" width="100px">
                    <template #default="scope">
                        <el-button type="info" @click="handleSelectCl(scope.row)">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-card :body-style="cardBodyStyle">
                <el-pagination :current-page="currentPageCl" :page-size="pageSizeCl" :page-sizes="pageSizesCl"
                    :total="totalRowsCl" background="background" layout="total, sizes, prev, pager, next, jumper"
                    @size-change="handleSizeChangeCl" @current-change="handleCurrentChangeCl">
                </el-pagination>
            </el-card>
        </el-dialog>

        <!-- 选司机 -->
        <el-dialog v-model="dialogSelectDriver" title="司机信息" width="800">
            <div style="display:flex;align-items:center;">
                司机姓名:<el-input v-model="queryInSj.drivername" clearable style="width: 220px;"
                    placeholder="请输入司机姓名..." />
                &nbsp&nbsp<el-button type="primary" @click="handleSearchSj">查询</el-button>
            </div>
            <br />
            <el-table height="220px" :data="tableDataSj" border highlight-current-row
                @selection-change="handleSelectionChange">
                <el-table-column type="index" label="序号" align="center" width="60">
                    <template #default="scope">
                        <span>{{ scope.$index + (currentPageSj - 1) * pageSizeSj + 1 }}</span>
                    </template>
                </el-table-column>

                <el-table-column v-if="true" prop="drivername" label="司机姓名" width="100" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="driverstatus" label="司机状态" width="80" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column v-if="true" prop="driveridcard" label="身份证号" width="180" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>


                <el-table-column v-if="true" prop="tel" label="司机电话" width="120" header-align="center" align="center"
                    show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="carJszbh" label="驾驶证编号" width="100" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>

                <el-table-column v-if="true" prop="xingbie" label="驾驶人员性别" width="70" header-align="center"
                    align="center" show-overflow-tooltip>
                </el-table-column>


                <el-table-column fixed="right" label="操作" header-align="center" align="center" width="100px">
                    <template #default="scope">
                        <el-button type="info" @click="handleSelectSj(scope.row)">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-card :body-style="cardBodyStyle">
                <el-pagination :current-page="currentPageSj" :page-size="pageSizeSj" :page-sizes="pageSizesSj"
                    :total="totalRowsSj" background="background" layout="total, sizes, prev, pager, next, jumper"
                    @size-change="handleSizeChangeSj" @current-change="handleCurrentChangeSj">
                </el-pagination>
            </el-card>
        </el-dialog>

        <!-- 选计划 -->
        <el-dialog v-model="dialogSelectPlan" title="计划信息" width="800">
            <div style="display:flex;align-items:center;">
                计划号:<el-input v-model="queryPlan.plan" clearable style="width: 220px;" placeholder="请输入计划号..." />
                &nbsp&nbsp<el-button type="primary" @click="handleSearchPlan">查询</el-button>
            </div>
            <br />
            <el-card :body-style="cardBodyStyle">
                <el-table height="200px" :data="tableData" border highlight-current-row>
                    <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="100px">
                        <template #default="scope">
                            <el-button type="info" @click="handleSelectPlan(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>

        <!-- 选班次 -->
        <el-dialog v-model="dialogSelectClasses" title="班次信息" width="800">
            <el-card :body-style="cardBodyStyle">
                <el-table height="200px" :data="tableDataClasses" border highlight-current-row>
                    <el-table-column v-if="true" prop="name" label="班次" width="120" header-align="center" align="center"
                        show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="操作" header-align="center" align="center" fixed="right" width="100px">
                        <template #default="scope">
                            <el-button type="info" @click="handleSelectClasses(scope.row)">选择</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </el-dialog>



    </div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'

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

const {
    proxy
} = getCurrentInstance()

import tools from '@/tools'
const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)
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
    handleSearchPc()
    handleSearchM()
    // timerId.value = setInterval(getTableDataM, 2000)
})

onBeforeUnmount(() => {
    clearTimeout(timerId.value);
})

const tableDataClasses = ref([
    { id: '1', name: '白班' },
    { id: '2', name: '中班' },
    { id: '3', name: '夜班' }
])
const dialogSelectClasses = ref(false)

/**
 *   查询
 */


const classesa = ref('')
const queryM = ref({
    planNumber: '',
    classes:''
})
const tableDataM = ref([])
const handleSearchM = () => {
    currentPageM.value = 1
    getTableDataM()
}

const getTableDataM = () => {
    var url = 'tms/Indispatchcar/queryM'
    var param = {
        page: currentPageM.value,
        rows: pageSizeM.value,
        params: JSON.stringify(queryM.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableDataM.value = response.data.data.records
            totalRowsM.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
 *   查询
 */

/**
 * 分页
 */
const currentPageM = ref(1)
const totalRowsM = ref(0)
const pageSizesM = ref([5, 10, 20, 30, 40])
const pageSizeM = ref(5)
const handleSizeChangeM = (val) => {
    pageSizeM.value = val
    getTableDataM()
}
const handleCurrentChangeM = (val) => {
    currentPageM.value = val
    getTableDataM()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisibleM = ref(false)
const formM = ref({
})
const ruleFormRef = ref()
const rules = reactive({
})

const handleAddM = () => {
    formM.value = {
    }
    plantt.value = ''
    dialogVisibleM.value = true
}
/**
 * 新增
 */


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

const openDialog = () => {
}

const handleSaveM = (ruleFormRef) => {
    ruleFormRef.validate((valid, fields) => {
        if (valid) {
            var url = 'tms/Indispatchcar/saveM'
            var param = {
                json: JSON.stringify(formM.value),
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    formM.value = response.data.data
                    ElMessage({
                        message: '保存成功',
                        type: 'success',
                    })
                    getTableDataM()
                    dialogVisibleM.value = false
                } else {
                    // ElMessage.error('操作失败！' + response.data.msg)
                    ElMessage({
                        message: '操作失败' + response.data.msg,
                        type: 'error'
                    })
                }
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}
const handleCancelSaveM = () => {
    dialogVisibleM.value = false
}
const handleEditM = (row) => {
    dialogVisibleM.value = true
    formM.value = {...row}
}
const handleDeleteM = (row) => {
    ElMessageBox.confirm(
        '确定删除本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            var url = 'tms/Indispatchcar/removeM'
            var param = {
                id: row.id
            }
            proxy.$axios.post(url, param).then(response => {
                if (response.data.code == 0) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getTableDataM()
                } else {
                    ElMessage({
                        type: 'error',
                        message: '删除失败' + response.data.msg
                    });
                }
            });
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}

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

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

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

    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/Indispatchcar/removeBatchM'
        var param = {
            ids: JSON.stringify(ids)
        }
        proxy.$axios.post(url, param).then(response => {
            if (response.data.code == '0') {
                ElMessage({
                    type: 'success',
                    message: '操作成功!',
                });
                getTableDataM()
                dialogVisibleM.value = false
            } else {
                ElMessage({
                    type: 'error',
                    message: '操作失败!' + response.data.msg
                });
            }
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

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

/**
 * 新增dlg
 */
/**
 * 头信息
 */
/**
 * 格式化时间
 */
function formatDate(date) {
    let year = date.getFullYear()
    let month = (date.getMonth() + 1).toString().padStart(2, '0')
    let day = date.getDate().toString().padStart(2, '0')

    return `${year}-${month}-${day}`
}
/**
 * 格式化时间
 */

/**
  * 定义表单
  */
const form = ref({
    deptCode: '',
    deptName: '',
    deptId: '',
    selecttime: '',
    carNumber: '',
    driverName: '',
    timeEffective: '',
    driverPhone: '',
    classes: '',
    startTime: '',
    endTime: '',
    planNumber: ''
})
//重置表单
const resetForm = () => {
    form.value = {
        deptCode: '',
        deptName: '',
        deptId: '',
        selecttime: '',
        carNumber: '',
        driverName: '',
        timeEffective: '',
        driverPhone: '',
        classes: '',
        startTime: '',
        endTime: '',
    }
}
/**
 * 表单
 */
/**
 * 查询条件
 */
const tableDataPc = ref([])
const query = ref({
    classes: '',
    selecttime: ''
})
/**
 * 查询条件
 */
/**
 * 司机
 */
const driverName = ref('')
const driverPhone = ref('')
/**
 * 司机
 */
/**
 * 计划号
 */
const planNumber = ref('')
/**
 * 计划号
 */
/**
 * 车号
 */
const carNumber = ref('')
/**
 * 车号
 */
/**
 * 日期
 */
const selecttime = ref(formatDate(new Date()))
const changeTime = (val) => {
    selecttime.value = val
    getTableDataPc()
}
/**
 * 日期
 */
/**
 * 班次
 */
const classes = ref('白班')
/**
 * 班次
 */

/**
* 查询 访问后端
*/
const handleSearchPc = () => {
    getTableDataPc()
}
const getTableDataPc = () => {
    query.value.classes = classes.value
    query.value.selecttime = selecttime.value
    var url = 'tms/Indispatchcar/queryPcByClassesWithSelecttime'
    var param = {
        params: JSON.stringify(query.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableDataPc.value = response.data.data
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
* 查询 访问后端
*/
/**
 * 派车
 */
const handleSearchB = () => {
    console.log('ni')
    classes.value = '白班'
    handleSearchPc()
}
const handleSearchZ = () => {
    classes.value = '中班'
    handleSearchPc()
}
const handleSearchY = () => {
    classes.value = '夜班'
    handleSearchPc()
}
/**
 * 派车
 */
/**
  * 删除车次
*/
const DeletePc = (row) => {

    ElMessageBox.confirm(
        '确定要删除该条记录吗？',
        'Warning',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        var url = 'tms/Indispatchcar/deletePc'
        var data = {
            id: row.id,
            params: JSON.stringify({
                deptCode: deptCode.value,
                deptName: deptName.value,
                deptId: deptId.value
            })
        }
        proxy.$axios.post(url, data).then(response => {
            if (response.data.code == '0') {
                ElMessage.success('删除成功')
                handleSearchPc()
            } else {
                ElMessage.error('操作失败！' + response.data.msg)
            }
        })
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消删除',
        })
    })

}
/**
  * 删除车次
*/
/**
 * 选择车辆
 */
const dialogSelectCar = ref(false)
const selectCarPre = (row) => {
    handleSearchCl()
    dialogSelectCar.value = true
    formCar.value = {...row}
}
/**
 * 派车
 */
const queryCl = ref({
    carno: ''
})
const tableDataCl = ref([])
const handleSearchCl = () => {
    currentPageCl.value = 1
    getTableDataCl()
}

const getTableDataCl = () => {
    var url = 'tms/Incarinfo/query'
    var param = {
        page: currentPageCl.value,
        rows: pageSizeCl.value,
        params: JSON.stringify(queryCl.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableDataCl.value = response.data.data.records
            totalRowsCl.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
 *   查询车辆
 */
/**
 * 分页车辆
 */
/**
 * 表格编辑
 */
const formCar = ref({

})
/**
 * 表格编辑
 */
const currentPageCl = ref(1)
const totalRowsCl = ref(0)
const pageSizesCl = ref([5, 10, 20, 30, 40])
const pageSizeCl = ref(5)
const handleSizeChangeCl = (val) => {
    pageSizeCl.value = val
    getTableDataCl()
}
const handleCurrentChangeCl = (val) => {
    currentPageCl.value = val
    getTableDataCl()
}
/**
 * 分页车辆
 */
//车来那个开始选择
const handleSelectCl = (clall) => {
    // if (clall.carStatus != '空闲') {
    //     ElMessage.error('无法选中，车辆为非空闲状态')
    //     return
    // }
    if (plantt.value != '1') {
        ElMessage.success('操作成功')
        carNumber.value = clall.carno
    }

    if (plantt.value === '1') {
        formPlan.value.carNumber = clall.carno
        if (formPlan.value.carNumber == '' || formPlan.value.carNumber == null) {
            ElMessage.error('车号不能为空')
            return
        }

        var url = 'tms/Indispatchcar/updatePcOver'
        var data = {
            type:'车号',
            params: JSON.stringify(formPlan.value)
        }
        proxy.$axios.post(url, data).then(response => {
            if (response.data.code == '0') {
                ElMessage.success('保存成功')

                dialogVisibleM.value = false
                dialogSelectCar.value = false
                handleSearchPc()
                getTableDataM()
                plantt.value = ''
            } else {
                ElMessage.error('操作失败！' + response.data.msg)
                dialogVisibleM.value = false
                dialogSelectCar.value = false
                plantt.value = ''
            }
        })
    }

}
/**
 * 选择车辆
 */
/**
 * 选择司机
 */
const dialogSelectDriver = ref(false)
const selectDriverPre = () => {
    handleSearchSj()
    dialogSelectDriver.value = true
}
/**
*   查询司机
*/
const queryInSj = ref({
    drivername: ''
})
const tableDataSj = ref([])
const handleSearchSj = () => {
    currentPageSj.value = 1
    getTableDataSj()
}

const getTableDataSj = () => {
    var url = 'tms/Driver/queryIn'
    var param = {
        page: currentPageSj.value,
        rows: pageSizeSj.value,
        params: JSON.stringify(queryInSj.value)
    }
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            tableDataSj.value = response.data.data.records
            totalRowsSj.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
/**
 *   查询司机
 */
/**
 * 分页司机
 */
const currentPageSj = ref(1)
const totalRowsSj = ref(0)
const pageSizesSj = ref([5, 10, 20, 30, 40])
const pageSizeSj = ref(5)
const handleSizeChangeSj = (val) => {
    pageSizeSj.value = val
    getTableDataSj()
}
const handleCurrentChangeSj = (val) => {
    currentPageSj.value = val
    getTableDataSj()
}
/**
 * 分页司机
 */
const handleSelectSj = (sjall) => {
    // if (sjall.driverstatus != '空闲') {
    //     ElMessage.error('无法选中，司机为非空闲状态')
    //     return
    // }
    if (plantt.value != '1') {
        ElMessage.success('操作成功')
        driverName.value = sjall.drivername
        driverPhone.value = sjall.tel
    }

    if (plantt.value === '1') {
        formPlan.value.driverName = sjall.drivername
        formPlan.value.driverPhone = sjall.tel
        if (formPlan.value.driverName == '' || formPlan.value.driverName == null) {
            ElMessage.error('司机姓名不能为空')
            return
        }

        var url = 'tms/Indispatchcar/updatePcOver'
        var data = {
            type:'司机姓名',
            params: JSON.stringify(formPlan.value)
        }
        proxy.$axios.post(url, data).then(response => {
            if (response.data.code == '0') {
                ElMessage.success('保存成功')

                dialogVisibleM.value = false
                dialogSelectDriver.value = false
                handleSearchPc()
                getTableDataM()
                plantt.value = ''
            } else {
                ElMessage.error('操作失败！' + response.data.msg)
                dialogVisibleM.value = false
                dialogSelectDriver.value = false
                plantt.value = ''
            }
        })
    }
}
/**
 * 选择司机
 */
/**
 * 选择计划
 */
/**
 * 表格修改
 */
const formPlan = ref({
})
/**
 * 表格修改
 */
const tableData = ref([])
const dialogSelectPlan = ref(false)
const selectPlanPre = (row) => {
    handleSearch()
    dialogSelectPlan.value = true
    // formPlan.value = row
}
const queryPlan = ref({
    fsubbillno: ''
})
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}
const getTableData = () => {
    var url = 'tms/OrderTrans/query'
    var param = {
        page: currentPage.value,
        rows: pageSize.value,
        params: JSON.stringify(queryPlan.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 gbmstype = ref([
    { id: '1', name: '固定车皮' },
    { id: '2', name: '先皮后毛' },
    { id: '3', name: '先毛后皮' },
])
const plantt = ref('')
const handleSelectPlan = (row) => {

    if (plantt.value != '1') {
        planNumber.value = row.fsubbillno
        ElMessage.success('操作成功')
        if (formPlan.value.planNumber == '' || form.value.planNumber == null) {
            ElMessage.error('计划号不能为空')
            return
        }
    }
    if (plantt.value === '1') {
        formPlan.value.planNumber = row.fsubbillno

        var url = 'tms/Indispatchcar/updatePcOver'
        var data = {
            type:'计划号',
            params: JSON.stringify(formPlan.value)
        }
        proxy.$axios.post(url, data).then(response => {
            if (response.data.code == '0') {
                ElMessage.success('保存成功')

                dialogVisibleM.value = false
                dialogSelectPlan.value = false
                handleSearchPc()
                getTableDataM()
                plantt.value = ''
            } else {
                ElMessage.error('操作失败！' + response.data.msg)
                dialogVisibleM.value = false
                dialogSelectPlan.value = false
                plantt.value = ''
            }
        })
    }



}
/**
 * 选择计划
 */
/**
 * 保存表单
 */
const handleSave = () => {
    form.value = {
        deptCode: deptCode.value,
        deptName: deptName.value,
        deptId: deptId.value,
        selecttime: selecttime.value,
        carNumber: carNumber.value,
        driverName: driverName.value,
        timeEffective: '',
        driverPhone: driverPhone.value,
        classes: classes.value,
        startTime: '',
        endTime: '',
        planNumber: planNumber.value
    }
    if (form.value.planNumber == '' || form.value.planNumber == null) {
        ElMessage.error('计划号不能为空')
        return
    }
    if (form.value.classes == '' || form.value.classes == null) {
        ElMessage.error('班次不能为空')
        return
    }
    if (form.value.driverPhone == '' || form.value.driverPhone == null) {
        ElMessage.error('司机电话不能为空')
        return
    }
    if (form.value.driverName == '' || form.value.driverName == null) {
        ElMessage.error('司机姓名不能为空')
        return
    }
    if (form.value.selecttime == '' || form.value.selecttime == null) {
        ElMessage.error('时间不能为空')
        return
    }
    if (form.value.carNumber == '' || form.value.carNumber == null) {
        ElMessage.error('车号不能为空')
        return
    }
    if (form.value.deptId == '' || form.value.deptId == null) {
        ElMessage.error('部门ID不能为空')
        return
    }
    if (form.value.deptName == '' || form.value.deptName == null) {
        ElMessage.error('部门名称不能为空')
        return
    }
    if (form.value.deptCode == '' || form.value.deptCode == null) {
        ElMessage.error('部门编码不能为空')
        return
    }
    var url = 'tms/Indispatchcar/savePcOver'
    var data = {
        params: JSON.stringify(form.value)
    }
    proxy.$axios.post(url, data).then(response => {
        if (response.data.code == '0') {
            ElMessage.success('保存成功')
            handleSearchPc()
            handleSearchM()
            dialogVisibleM.value = false
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
            dialogVisibleM.value = false
        }
    })

}
/**
 * 保存表单
 */
/**
 * 新增dlg
 */
/**
 * 编辑
 */
const tableRowEditId = ref(null)
const tableColumnEditIndex = ref(null)
const planNumberRef = ref(null)
const carNumberRef = ref(null)
const driverNameRef = ref(null)
const classesRef = ref(null)

const showUnitInput = (row, column) => {

    tableRowEditId.value = row.id; // 确定点击的单元格在哪行
    tableColumnEditIndex.value = column.id; // 确定点击的单元格在哪列
    // nextTick(() => {
    //     if (column.property == 'planNumber') {
    //         planNumberRef.value.focus()
    //     } else if (column.property == 'carNumber') {
    //         carNumberRef.value.focus()
    //     } else if (column.property == 'driverName') {
    //         driverNameRef.value.focus()
    //     } else if (column.property == 'classes') {
    //         classesRef.value.focus()
    //     }
    // })
}

const blurValueInput = (row, column) => {



    // 清除编辑状态
    tableRowEditId.value = null;
    tableColumnEditIndex.value = null;
}
const selectPlanPreA = (row) => {
    plantt.value = '1'
    formPlan.value = {...row}
    handleSearch()
    dialogSelectPlan.value = true
}

const selectCarPreB = (row) => {
    plantt.value = '1'
    formPlan.value = {...row}
    handleSearchCl()
    dialogSelectCar.value = true
}

const selectDriverPreC = (row) => {
    plantt.value = '1'
    formPlan.value = {...row}
    handleSearchSj()
    dialogSelectDriver.value = true
}

const selectClassesPreD = (row) => {
    plantt.value = '1'
    formPlan.value = {...row}
    dialogSelectClasses.value = true
}


const handleSelectClasses = (row) => {
    if (plantt.value === '1') {
        formPlan.value.classes = row.name
        if (formPlan.value.classes == '' || formPlan.value.classes == null) {
            ElMessage.error('班次不能为空')
            return
        }

        var url = 'tms/Indispatchcar/updatePcOver'
        var data = {
            type:'班次',
            params: JSON.stringify(formPlan.value)
        }
        proxy.$axios.post(url, data).then(response => {
            if (response.data.code == '0') {
                ElMessage.success('保存成功')

                dialogVisibleM.value = false
                dialogSelectClasses.value = false
                handleSearchPc()
                getTableDataM()
                plantt.value = ''
            } else {
                ElMessage.error('操作失败！' + response.data.msg)
                dialogVisibleM.value = false
                dialogSelectClasses.value = false
                plantt.value = ''
            }
        })
    }
}
const timerId = ref(null)

const classesChange = (val) =>{
    queryM.value.classes = val
    handleSearchM()
}

/**
 * 编辑
 */
</script>

<style></style>