<template>
    <div>
        <el-card :body-style="cardBodyStyle">
            <el-form inline>
                <el-row :span="24">
                    <el-col :span="5">
                        <el-form-item label="物料名称" prop="">
                            <el-select v-model="query.leibiemc" placeholder="请选择材料" clearable style="width: 200px;">
                                <el-option v-for="item in typeList" :key="item.itemCd" :label="item.itemNm"
                                    :value="item.itemNm"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="委托状态" prop="">
                            <el-select v-model="query.weituoztMc" placeholder="是否制样完成" clearable style="width: 200px;">
                                <!-- <el-option v-for="item in weituozt" :key="item.zhuangtaibm" :label="item.zhuangtaimc"
                                    :value="item.zhuangtaimc"></el-option> -->
                                    <el-option label="取样完成" value="取样完成"></el-option>
                                    <el-option label="制样接样" value="制样接样"></el-option>
                                    <el-option label="制样完成" value="制样完成"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="样品类型" prop="">
                            <el-select v-model="query.smpType" placeholder="请选择样品类型" clearable style="width: 180px;">
                                <!-- <el-option v-for="item in smpTypeList" :key="item.bm" :label="item.wtmc"
                                    :value="item.bm"></el-option> -->
                                    <el-option label="原料委托" value="Y"></el-option>
                                    <el-option label="成品委托" value="C"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="制样编号" prop="">
                            <el-input placeholder="请输入制样编号" v-model="query.zybh"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2">
                        <el-button type="primary" @click="show" link>高级查询
                            <el-icon v-if="showA">
                                <ArrowUp />
                            </el-icon>
                            <el-icon v-else>
                                <ArrowDown />
                            </el-icon>
                        </el-button>
                    </el-col>
                    <el-col :span="2">
                        <el-button type="primary" @click="reset" link>重置</el-button>
                    </el-col>
                </el-row>
                <el-row :span="24" v-if="showA">
                    <el-col :span="5">
                        <el-form-item label="取样提交时间" prop="">
                            <el-date-picker v-model="query.quyangtjsj" type="datetimerange" align="right" unlink-panels
                                range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
                                style="width: 403px;" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </el-card>
        <ButtonBar area="top" @search="handleSearch" @commit="handleCommit" @make="handleMake" @print="handlePrint"></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 prop="wulizybh" label="物理制样编号" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
                <el-table-column prop="quyangbh" label="取样编号" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
                <el-table-column prop="leibiemc" label="物料类别" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
                <!-- <el-table-column prop="jiliangh2" label="是否合并" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column> -->
<!--                <el-table-column prop="zhiyangdwname" label="制样单位" width="200" header-align="center" align="center"-->
<!--                    show-overflow-tooltip></el-table-column>-->
                <el-table-column prop="weituoztMc" label="委托状态" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
<!--                <el-table-column prop="yunshufs" label="运输方式" width="200" header-align="center" align="center"-->
<!--                    show-overflow-tooltip></el-table-column>-->
                <!-- <el-table-column prop="zhiyangdwname" label="制样单位" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column> -->
                <el-table-column prop="chuangjiansj" label="创建时间" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
                <el-table-column prop="quyangtjsj" label="取样提交时间" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
                <!-- <el-table-column prop="zhiyangsj" label="制样时间" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column> -->
                <el-table-column prop="zhiyangtjsj" label="制样提交时间" width="200" header-align="center" align="center"
                    show-overflow-tooltip></el-table-column>
                <!-- <el-table-column label="操作" header-align="center" align="center" width="200px" fixed="right">
                    <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-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="120px" label-position="right" inline ref="ruleFormRef" :rules="rules">
                        <el-form-item label="物料编码" prop="erpbm">
                            <el-autocomplete
                                v-model="form.erpbm"
                                :fetch-suggestions="querySearchErpbm"
                                placeholder="请输入物料编码"
                                clearable
                                style="width: 300px"
                                @select="handleBlur"
                            ></el-autocomplete>
                        </el-form-item>
                        <el-form-item label="物料名称">
                            <el-autocomplete
                                v-model="form.name"
                                :fetch-suggestions="querySearChName"
                                placeholder="请输入物料名称"
                                clearable
                                style="width: 300px"
                                @select="handleBlur"
                            ></el-autocomplete>
                        </el-form-item>
                        <el-form-item label="备注">
                            <el-input v-model="form.remarks" clearable style="width: 300px; height: 100px;" ></el-input>
                        </el-form-item>
                    </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>-->
    </div>
</template>

<script setup>
import ButtonBar from "@/components/ButtonBar.vue";
import SearchBox from "@/components/SearchBox.vue";
import {
    ref,
    reactive,
    onMounted,
    getCurrentInstance
} from 'vue'
import style from '@/tools/style'
import { ElMessage, ElMessageBox } from "element-plus";


onMounted(() => {
    queryType()
    queryWeiTuoZT()
    queryWTLXType()
    handleSearch()
})


const { proxy } = getCurrentInstance()
const cardBodyStyle = ref(style.card)
const dialogVisible = ref(false)
const form = ref({

})
const tableData = ref([])
const query = ref({
    leibiemc: '',
    weituoztMc: '取样完成',
    smpType: '',
    zybh: '',
    quyangtjsj: ''
})
const showA = ref(false)
const show = () => {
    showA.value = !showA.value
}

/**
 * 重置查询条件
 */
const reset = () => {
    Object.keys(query.value).map(key => {
        query.value[key] = ''
    })
    
}
const typeList = ref([])
const queryType = () => {
    var url = 'sci/platform/queryType'
    var param = {}
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            typeList.value = response.data.data
            console.log(response.data.data)
            console.log(typeList.value)
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
// 查询委托状态
const weituozt = ref([])
const queryWeiTuoZT = () => {
    var url = 'sci/platform/queryWeiTuoZT'
    var param = {}
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            weituozt.value = response.data.data
            console.log(response.data.data)
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}
const smpTypeList = ref([])
// 查询委托类型
const queryWTLXType = () => {
    var url = 'sci/Weituogl/query_YLType'
    var param = {}
    proxy.$axios.get(url, param).then(response => {
        if (response.data.code == '0') {
            smpTypeList.value = response.data.data
            // console.log(smpTypeList.value)
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}


// ==========按钮功能===========
// 搜索
const handleSearch = () => {
    currentPage.value = 1
    getTableData()
}
const handlePrint = () => {
    console.log('打印')
}
// 制样完成提交
const handleCommit = () => {
    if (multipleSelection.value.length < 1) {
        ElMessage({
            type: 'warning',
            message: '请选择要提交的数据!'
        })
        return
    }
    var ids = []
    multipleSelection.value.forEach((item, index) => {
        ids.push(item.id)
    })
    var userName = localStorage.getItem('userDesc')
    var url = 'sci/make/commit'
    let dept = localStorage.getItem('dept')
    dept = JSON.parse(dept)
    let deptCode = dept[0].deptCode
    var param = {
        ids: JSON.stringify(ids),
        userName: userName,
        deptCode
    }
    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
            });
        }
    })
}
// 批量删除
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
    console.log(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 = 'cot/SignRequest/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 handleMake = () => {
    if (multipleSelection.value.length < 1) {
        ElMessage({
            type: 'warning',
            message: '请选择要提交的数据!'
        })
        return
    }
    var ids = []
    multipleSelection.value.forEach((item, index) => {
        ids.push(item.id)
    })
    var userName = localStorage.getItem('userDesc')
    var url = 'sci/make/makeReceive'
    let dept = localStorage.getItem('dept')
    dept = JSON.parse(dept)
    let deptCode = dept[0].deptCode
    var param = {
        ids: JSON.stringify(ids),
        userName: userName,
        deptCode
    }
    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
            });
        }
    })
}

// 删除
const handleDelete = (row) => {
    ElMessageBox.confirm(
        '确定删除本条记录?',
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
    }
    )
        .then(() => {
            // 假数据
            const fakeData = {
                code: 0,
                msg: '删除成功'
            };

            // 模拟接口响应
            if (fakeData.code == 0) {
                ElMessage({
                    type: 'success',
                    message: '删除成功',
                });
                getTableData();
            } else {
                ElMessage({
                    type: 'error',
                    message: '删除失败' + fakeData.msg,
                });
            }
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            });
        });
};
// 编辑
const handleEdit = () => {
    console.log('handleEdit')
}

// ==========查询数据===========
const getTableData = () => {
    var url = 'sci/make/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
            // console.log(response.data.data.records[0])
            console.log(tableData.value)
            totalRows.value = response.data.data.total
        } else {
            ElMessage.error('操作失败！' + response.data.msg)
        }
    })
}

/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([10, 20, 30, 40])
const pageSize = ref(10)
const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
}
const handleCurrentChange = (val) => {
    currentPage.value = val
    getTableData()
}
</script>

<style scoped></style>