<template>
    <div class="page-container row-container">
        <!-- 左侧区域 -->
        <section class="list-part-container" style="flex: 3">
            <!-- 搜索区 -->
            <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true"
                style="margin-right: 0px">
                <el-form-item label="产品描述：">
                    <el-input placeholder="请输入产品描述" v-model="queryParams.productDescription"
                        @blur="trimBlur('productDescription')" @keydown.enter.prevent clearable style="width: 130px" />
                </el-form-item>
                <el-form-item label="批次号：">
                    <el-input placeholder="请输入批次号" v-model="queryParams.lotCode" @blur="trimBlur('lotCode')"
                        @keydown.enter.prevent clearable style="width: 140px" />
                </el-form-item>
                <el-form-item class="section-title" label="工段：">
                    <el-select-v2 v-model="queryParams.deptId" :options="deptList" placeholder="请选择工段"
                        style="width: 130px" @change="handleDeptChange" />
                </el-form-item>

                <el-form-item label="废品提交时间：">
                    <el-date-picker v-model="queryParams.startTime" type="date" value-format="YYYY-MM-DD"
                        :editable="false" clearable placeholder="请选择" style="width: 136px" @change="handleTimeChange" />
                    <span>到</span>
                    <el-date-picker v-model="queryParams.endTime" type="date" value-format="YYYY-MM-DD"
                        :editable="false" clearable placeholder="请选择" style="width: 136px" @change="handleTimeChange" />
                </el-form-item>
                <el-form-item style="margin-left: 0">
                    <el-button type="info" icon="Search" @click="handleQuery">搜索
                    </el-button>
                    <el-button type="success" icon="Download" @click="handleExport">导出</el-button>
                </el-form-item>
            </el-form>
            <div class="el-table-container">
                <div class="el-table-inner-container">
                    <el-table ref="rejectTable" :data="rejectList" v-loading="rejectLoading" show-summary
                        :summary-method="getTotalRejct" highlight-current-row height="100%"
                        @row-click="handleCheckRejectReason">
                        <el-table-column label="批次号" prop="lotCode" width="160" align="center" />
                        <el-table-column label="产品描述" prop="productDescription" align="center" />
                        <el-table-column label="工艺版本" prop="technologyVersion" align="center" />
                        <el-table-column label="废品数" prop="totalRejectNum" width="150" align="center">
                            <template #default="scope">
                                <span v-if="scope.row.totalRejectNum > 0" style="color: blue; cursor: pointer;">
                                    {{ scope.row.totalRejectNum }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="工段" prop="deptName" width="160" align="center" />
                        <el-table-column label="废品提交时间" prop="createTime" width="160" align="center" />
                    </el-table>
                </div>
            </div>
            <!-- 分页 -->
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getDayworkItemReject" />
        </section>
        <reject-dialog ref="rejectRecordsDialog" />
    </div>
</template>

<script setup name="rejectReport">
import {
    rejectReport, rejectReportList
} from "@/api/business/dayworkItemReject.js";
import { getDeptList } from "@/api/business/planDetailSubDetail.js";
import rejectDialog from "./DialogRejectRecords"
import queryStore from "@/store/modules/query";
import { ref } from "vue";
const { proxy } = getCurrentInstance();

/** 废品 */
const rejectTable = ref(null);
const rejectList = ref([]);
const rejectLoading = ref(false);
const loading = ref(false);
const isDispatch = ref(false);
const rejectTotal = ref(0)
const total = ref(0);
/**工段 */
const deptList = ref([]);
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    lotCode: null,
    productDescription: null,
    deptId: null
});

/***********************  工段相关事件  ****************************/
function getList() {
    getNowDate();
    loading.value = true;
    getDeptList().then((response) => {
        deptList.value = response.data.rows;
        isDispatch.value = response.data.others.isDispatch;
        loading.value = false;
        if (isDispatch.value) {
            deptList.value.unshift({ label: "全部", value: "0" });
        }
        queryParams.value.deptId = deptList.value[0].value;
        getDayworkItemReject();
    });
}
function getNowDate() {
    queryParams.value.startTime = proxy.moment().subtract(3, 'month').format("YYYY-MM-DD")
    queryParams.value.endTime = proxy.moment().format("YYYY-MM-DD")
}
function handleTimeChange() {

    getDayworkItemReject();
}

//切换工段
function handleDeptChange() {
    getDayworkItemReject();
}


/***********************  废品报表信息相关事件  ****************************/

/** 废品详情列表 */
function getDayworkItemReject() {
    if (queryParams.value.startTime > queryParams.value.endTime) {
        proxy.$modal.msgError('结束时间不能小于开始时间')
        return
    }
    const listQueryParams = queryStore().rejectReportParams;
    if (listQueryParams != null) {
        queryParams.value = listQueryParams;
    }
    if (queryParams.value.startTime != null) {
        queryParams.value.startTime = proxy.moment(queryParams.value.startTime).format('YYYY-MM-DD 00:00:00')
    }
    if (queryParams.value.endTime != null) {
        queryParams.value.endTime = proxy.moment(queryParams.value.endTime).format('YYYY-MM-DD 23:59:59')
    }
    rejectLoading.value = true;
    rejectReport(queryParams.value).then((res) => {
        rejectList.value = res.rows;
        total.value = res.total;
        rejectLoading.value = false;
    });
    getRejectTotal()
}
/** 导出按钮操作 */
function handleExport() {
    proxy.download('/ezhizao-dms-production/business/reject/download', {
        ...queryParams.value
    }, `废品报表${new Date().getTime()}.xlsx`)
}
/** 搜索按钮操作 */
function handleQuery() {
    const listQueryParams = JSON.parse(JSON.stringify(queryParams.value));
    queryStore().setRejectReportParams(listQueryParams);
    getDayworkItemReject();
}
//获得所有页的废品总数
function getRejectTotal() {
    rejectReportList(queryParams.value).then(res => {
        rejectTotal.value = res.data
    })
    console.log(rejectTotal.value)
}
//查看废品原因
function handleCheckRejectReason(row) {
    if (queryParams.value.startTime != null) {
        row.startTime = proxy.moment(queryParams.value.startTime).format('YYYY-MM-DD 00:00:00')
    }
    if (queryParams.value.endTime != null) {
        row.endTime = proxy.moment(queryParams.value.endTime).format('YYYY-MM-DD 23:59:59')
    }
    proxy.$refs.rejectRecordsDialog.open(row);
}
//计算废品总数
function getTotalRejct() {
    let sum = []
    sum[3] = '废品总数:' + rejectTotal.value
    return sum
}

function trimBlur(param) {
    queryParams.value[param] = queryParams.value[param].trim()
}

onMounted(() => {
    getList();
});
</script>
<style scoped>
.el-form--inline .el-form-item {
    margin-right: 20px;
}
</style>