<script lang="ts">
export default {
    name: "testtask",//试验数据管理
};
</script>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'
import Page from '@/components/Pagination/page.vue'
import { tstaskPage, addtsTask, updatetsTask, deleteTsTaskById, deleteTsTaskByIds } from "@/api/testtask";

//定义表格数据
const tableData: any = ref([]);
// 查询数据
const dataduan = ref([])
const queryParams: any = ref({
    current: 1,
    size: 20,
    taskName: '',
    taskPlace: '',
    taskPerson: '',
    carrierType: '',
    deviceCode: '',
    startDate: '',
    endDate: ''
});
const total = ref(0);
//转化时间格式
function zhuandata(dateString: any) {
    // 创建Date对象
    const date = new Date(dateString);
    // 获取年、月、日
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
    const day = String(date.getDate()).padStart(2, '0');
    // 格式化为YYYY-MM-DD
    return `${year}-${month}-${day}`;
}
// 表格加载
const loading = ref(false)
//获取表格数据
function getdata() {
    loading.value = true
    if (dataduan.value && dataduan.value.length > 0 ) {
        const dataArr = []
        dataduan.value.forEach((item: any) => {
            dataArr.push(zhuandata(item))
        })
        queryParams.value.startDate = dataArr[0]
        queryParams.value.endDate = dataArr[1]
    }else{
        queryParams.value.startDate = ''
        queryParams.value.endDate = ''
    }

    tstaskPage(queryParams.value).then((res: any) => {
        loading.value = false
        tableData.value = res.data.records
        queryParams.value.current = res.data.current
        queryParams.value.size = res.data.size
        total.value = res.data.total
    })
}
//弹框命名
const title = ref("")
//控制弹框显隐
const frame = ref(false)
//新增项目弹框
function addproject() {
    frame.value = true
    title.value = "新增试验任务"
    projectForme.value = {
        taskName: "",//任务名称
        taskDate: "",//任务开始时间 
        taskPlace: "",//任务地点
        taskPerson: "",//任务人员
        carrierType: "",// 载体类型
        carrierName: "",//载体名称
        deviceName: "",//设备名称    
        taskCode: "",//任务编号
        deviceCode: ""//设备编号
    }
}
//修改项目弹框
function editproject(row: any) {
    projectForme.value = JSON.parse(JSON.stringify(row))
    title.value = "修改试验任务"
    frame.value = true
}
//删除项目弹框
function delproject(row: any) {
    ElMessageBox.confirm(
        '您确定要删除该项目吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            deleteTsTaskById({ id: row.id }).then((res: any) => {
                if (res.code == 0) {
                    getdata()
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                }
            })
        })
}
//表格多选
const tableIdarr = ref([])
function handleSelectionChange(val: any) {
    tableIdarr.value = val
}
//多选删除
function delprojectArr() {
    const ids = []
    tableIdarr.value.forEach((item: any) => {
        ids.push(item.id)
    })
    ElMessageBox.confirm(
        '您确定要删除该项目吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            deleteTsTaskByIds({ ids: ids.join(',') }).then((res: any) => {
                if (res.code == 0) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getdata()
                }
            })

        })
}
//弹框关闭
function handleClose(formEl: any) {
    // if (!formEl) return
    // formEl.resetFields()
    frame.value = false
}
//表单数据
const ruleFormRef = ref()
const projectForme: any = ref({
    taskName: "",//任务名称
    taskDate: "",//任务开始时间 
    taskPlace: "",//任务地点
    taskPerson: "",//任务人员
    carrierType: "",// 载体类型
    carrierName: "",//载体名称
    deviceName: "",//设备名称    
    taskCode: "",//任务编号
    deviceCode: ""//设备编号
})
//表单确定
async function submitForm(formEl: any) {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            if (projectForme.value.id) {
                updatetsTask(projectForme.value).then((res: any) => {
                    if (res.code == 0) {
                        getdata()
                        ElMessage.success("修改成功")
                        frame.value = false
                    }
                })
            } else {
                addtsTask(projectForme.value).then((res: any) => {
                    if (res.code == 0) {
                        getdata()
                        ElMessage.success("添加成功")
                        frame.value = false
                    }

                })
            }
        }
    })
}
//用户弹窗规则定义
const moderules = ref({
    taskName: [{ required: true, message: "请输入任务名称", trigger: "blur" }],
    taskCode: [{ required: true, message: "请输入任务编号", trigger: "blur" }]
});
onMounted(() => {
    getdata()
});


</script>

<template>
    <div>
        <div class="record-box">
            <div class="sou_title">
                <div class="sou_title_left">
                    <el-input style="margin-right: 10px ;" v-model="queryParams.taskName" clearable @change="getdata()"
                        placeholder="任务名称"></el-input>
                    <el-input style="margin-right: 10px ;" v-model="queryParams.taskPlace" clearable @change="getdata()"
                        placeholder="任务地点"></el-input>
                    <el-input style="margin-right: 10px ;" v-model="queryParams.taskPerson" clearable
                        @change="getdata()" placeholder="任务人员"></el-input>
                    <el-input style="margin-right: 10px ;" v-model="queryParams.carrierType" clearable
                        @change="getdata()" placeholder="载体类型"></el-input>
                    <el-input style="margin-right: 10px ;" v-model="queryParams.deviceCode" clearable
                        @change="getdata()" placeholder="任务设备"></el-input>
                    <el-date-picker v-model="dataduan" type="daterange" range-separator="至" @change="getdata()"
                        style="width: 200%;margin-right: 10px ;" start-placeholder="开始时间" end-placeholder="结束时间" />
                    <el-button type="primary" @click="getdata()">搜索</el-button>
                </div>
                <div>
                    <el-button type="primary" @click="addproject()">新增</el-button>
                    <el-button type="primary" @click="delprojectArr()" :disabled="tableIdarr.length == 0">删除</el-button>
                </div>
            </div>

            <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange"
                :header-cell-style="{ background: 'rgb(250 250 250)', height: '50px' }"
                style="width: 100%; height: calc(100vh - 275px);margin-bottom: 20px;" border>
                <el-table-column type="selection" width="40" />
                <el-table-column prop="taskCode" label="任务编号" width="90"></el-table-column>
                <el-table-column prop="taskName" label="任务名称" width="120"></el-table-column>
                <el-table-column prop="carrierType" label="载体类型" width="90"></el-table-column>
                <el-table-column prop="carrierName" label="载体名称" width="120"></el-table-column>
                <el-table-column prop="deviceCode" label="设备编号" width="90"></el-table-column>
                <el-table-column prop="deviceName" label="设备名称" width="120"></el-table-column>
                <el-table-column prop="taskDate" label="任务开始时间" width="170" align="center"></el-table-column>
                <el-table-column prop="taskPlace" label="任务地点"></el-table-column>
                <el-table-column prop="taskPerson" label="任务人员"></el-table-column>
                <el-table-column fixed="right" label="操作" width="80" align="center">
                    <template #default="scope">
                        <span
                            style="display: flex;display: -webkit-flex;justify-content: space-around;-webkit-justify-content: space-around; ">
                            <img src="@/assets/MenuIcon/lbcz_xg.png" alt="" title="修改" @click="editproject(scope.row)"
                                style="cursor: pointer;">
                            <img src="@/assets/MenuIcon/lbcz_sc.png" alt="" title="删除" @click="delproject(scope.row)"
                                style="cursor: pointer;">
                        </span>
                    </template>
                </el-table-column>
            </el-table>
            <Page :total="total" v-model:size="queryParams.size" v-model:current="queryParams.current"
                @pagination="getdata()">
            </Page>
        </div>
        <el-dialog :title="title" v-model="frame" width="30%" :before-close="handleClose" top="30px" draggable
            destroy-on-close>
            <el-form ref="ruleFormRef" style="max-width: 600px" :model="projectForme" :rules="moderules"
                label-width="auto" class="demo-ruleForm" status-icon>
                <el-form-item label="任务编号" prop="taskCode">
                    <el-input v-model="projectForme.taskCode" />
                </el-form-item>
                <el-form-item label="任务名称" prop="taskName">
                    <el-input v-model="projectForme.taskName" />
                </el-form-item>
                <el-form-item label="任务时间">
                    <!-- <el-input v-model="projectForme.taskDate" /> -->
                    <el-date-picker v-model="projectForme.taskDate" type="datetime" format="YYYY-MM-DD HH:mm:ss"
                        style="width:100%;" value-format="YYYY-MM-DD HH:mm:ss" />
                </el-form-item>
                <el-form-item label="任务地点">
                    <el-input v-model="projectForme.taskPlace" />
                </el-form-item>
                <el-form-item label="任务人员">
                    <el-input v-model="projectForme.taskPerson" />
                </el-form-item>
                <el-form-item label="载体类型">
                    <el-input v-model="projectForme.carrierType" />
                </el-form-item>
                <el-form-item label="载体名称">
                    <el-input v-model="projectForme.carrierName" />
                </el-form-item>
                <el-form-item label="设备编号">
                    <el-input v-model="projectForme.deviceCode" />
                </el-form-item>
                <el-form-item label="设备名称">
                    <el-input v-model="projectForme.deviceName" />
                </el-form-item>
                <el-form-item>
                    <div style="width: 100%;display: flex;justify-content: end;">
                        <el-button type="primary" @click="submitForm(ruleFormRef)">确定</el-button>
                        <el-button @click="handleClose(ruleFormRef)">取消</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<style lang="scss" scoped>
.record-box {
    padding: 20px;
    width: 100%;
    height: calc(100vh - 130px);
    overflow: auto;
    background-color: rgba(255, 255, 255, 1);
    border: none;
    border-radius: 3px;
    box-sizing: border-box;

    .sou_title {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        .sou_title_left {
            width: 80%;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

    }
}
</style>
