<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item prop="startTime">
                <el-date-picker
                    v-model="queryParams.startTime"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="开始日期"
                    @change="() => queryParams.endTime = ''"
                    style="width: 130px"
                />
            </el-form-item>
            <el-form-item>-</el-form-item>
            <el-form-item prop="endTime">
                <el-date-picker
                    v-model="queryParams.endTime"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="结束日期"
                    :picker-options="{ disabledDate: (t) => t.getTime() < new Date(queryParams.startTime).getTime() - 86400000}"
                    style="width: 130px"
                />
            </el-form-item>
            <el-form-item  prop="keyWord">
                <el-input
                    v-model="queryParams.keyWord"
                    placeholder="请输入关键词"
                    clearable
                    @keyup.enter.native="handleQuery"
                    maxlength="20"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="tableList" ref="multipleTable" border :span-method="objectSpanMethod" :max-height="650">
            <el-table-column label="序号" align="center" width="80" type="index">
                <template scope="scope">
                    <span v-if="scope.$index==0">合计：</span>
                    <span v-else>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index }}</span>
                </template>
            </el-table-column>
            <el-table-column label="物资名称" align="center" prop="typeName" :show-overflow-tooltip="true"/>
            <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true"/>
            <el-table-column label="计量单位" align="center" prop="unitName" :show-overflow-tooltip="true"/>
            <el-table-column label="维修合格数量" align="center" prop="repairedNum" :show-overflow-tooltip="true">
                <template scope="scope">
                    <!-- 当 manageType 为 '编码' 时，使用可点击的 span -->
                    <span class="clickText" v-if="scope.row.manageType=='0'" @click="openRecords(scope.row)">
                        {{ scope.row.repairedNum }}
                    </span>
                    <!-- 否则，直接显示数字 -->
                    <span v-else>
                        {{ scope.row.repairedNum }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="入库数量" align="center" prop="inputNum" :show-overflow-tooltip="true"/>
            <el-table-column label="维修提交时间" align="center" prop="repairTime" width="100"/>
            <el-table-column label="入库时间" align="center" prop="inputTime" :show-overflow-tooltip="true"/>
            <el-table-column label="入库人员" align="center" prop="inputPersonName" :show-overflow-tooltip="true"/>
            <el-table-column label="待入库数量" align="center" prop="pendingInputNum" :show-overflow-tooltip="true"/>
            <el-table-column label="入库单号" align="center" prop="code" :show-overflow-tooltip="true"/>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 弹窗 -->
        <el-dialog :title="title" :visible.sync="openRecord" width="1200px" append-to-body>
            <el-form :model="dialogQuery" ref="dialogQuery" size="small" :inline="true" label-width="80px">
                <el-form-item label="关键字" prop="keyWord">
                    <el-input
                        v-model="dialogQuery.keyWord"
                        placeholder="请输入关键字"
                        clearable :maxlength="30"
                        style="width: 240px"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary"
                        icon="el-icon-search" size="mini"
                        @click="handleDialogQuery"
                        >查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button
                        icon="el-icon-refresh"
                        size="mini"
                        @click="resetDialogQuery"
                        >重置</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button
                        type="warning"
                        icon="el-icon-download"
                        size="mini"
                        @click="handleDialogExport"
                        >导出</el-button>
                </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="dialogList" height="500px">
                <el-table-column label="序号" align="center" width="80" type="index" :index="indexContinuation(dialogQuery.pageNum, dialogQuery.pageSize)">
                </el-table-column>
                <el-table-column label="机具名称" align="center" prop="typeName" :show-overflow-tooltip="true"/>
                <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true"/>
                <el-table-column label="设备编码" align="center" prop="maCode" :show-overflow-tooltip="true"/>
                <el-table-column label="入库人员" align="center" prop="inputPersonName" :show-overflow-tooltip="true"/>
                <el-table-column label="入库时间" align="center" prop="inputTime" :show-overflow-tooltip="true"/>
            </el-table>
            <pagination
                v-show="dialogTotal > 0"
                :total="dialogTotal"
                :page.sync="dialogQuery.pageNum"
                :limit.sync="dialogQuery.pageSize"
                @pagination="getDialogDataList"
                />
        </el-dialog>
    </div>
</template>

<script>
    import { getRepairInputListApi, getRepairInputListNoPageApi, getRepairInputDetailsListApi } from "@/api/report/report";
    export default {
    name: "TestedInBoundReport",
    data() {
        return {
        // 遮罩层
        loading: false,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        showHouse: false,
        dateRange:[],
        ids:[],
        // 查询参数
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            keyWord:undefined,
            startTime: '',
            endTime: '',
        },
        // 总条数
        total: 0,
        //表格数据
        tableList: [],
        // 弹出层标题
        title: "",
        //弹窗
        openRecord: false,
        dialogList: [], //在库编码设备列表
        dialogQuery: {
            pageNum: 1,
            pageSize: 10,
            keyWord: undefined,
        },
        dialogTotal: 0,
        };
    },
    created() {
        const end = new Date()
        let start = new Date()
        start.setMonth(start.getMonth() - 1)
        this.dateRange = [this.format(start), this.format(end)]
        this.getList()
    },
    methods: {
        format(date) {
            const y = date.getFullYear()
            const m = String(date.getMonth() + 1).padStart(2, '0')
            const day = String(date.getDate()).padStart(2, '0')
            return `${y}-${m}-${day}`
        },
        // 合并单元格 rowIndex=行数  columnIndex=列数
        // 这里是合并table的除表头外的第一行的第二列 + 除表头外的第二行的第二列
        // 注意列数和行数从 0 开始
        objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
            if (rowIndex === 0 && columnIndex == 0) {
                let rowspan = 1
                let colspan = 4
                return { rowspan, colspan }
            }
            // 解决偏移的重要代码
            // 重点在 else if 判断必须加因为合并单元格数据会出现偏移、
            // 否则如果是 第二行 的 第二列 那么隐藏原有第二行 的 第二列的数据使他隐藏
            else if (rowIndex === 0 && columnIndex < 4) {
                return {
                    rowspan: 0,
                    colspan: 0
                }
            }
        },
        /** 查询列表 */
        async getList() {
            this.loading = true
            const params = {
                keyWord: this.queryParams.keyWord,
                startTime: this.queryParams.startTime,
                endTime: this.queryParams.endTime,
                pageSize: this.queryParams.pageSize,
                pageNum: this.queryParams.pageNum
            }
            const res = await getRepairInputListApi(params)
            if(res.data.rows.length>0){
                this.tableList = res.data.rows;
            }else{
                this.tableList=[]
            }
            this.total = res.data.total;
            let param = {
                keyWord: this.queryParams.keyWord,
                startTime: this.queryParams.startTime,
                endTime: this.queryParams.endTime,
            }
            const response = await getRepairInputListNoPageApi(param)
                let obj = {
                    repairedNum: response.data.repairedNum||0,//维修合格数量
                    inputNum: response.data.inputNum||0,//入库数量
                    pendingInputNum: response.data.pendingInputNum||0,//待入库数量
                }
                this.tableList.unshift(obj)
                this.loading = false

        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            const end = new Date()
            let start = new Date()
            start.setMonth(start.getMonth() - 1)
            this.dateRange = [this.format(start), this.format(end)]
            this.queryParams.keyWord=null;
            this.handleQuery();
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        handleExport() {
          const formatTime = (date) => {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');
            return `${year}${month}${day}_${hours}${minutes}${seconds}`;
          };

          const currentTime = formatTime(new Date());
            this.download(
                "/material/bm_report/exportRepairInputList",
                { ...this.queryParams },
                `修饰入库报表_${currentTime}.xlsx`
            );
        },
        //查看弹框数据
        openRecords(row) {
            this.openRecord = true
            this.dialogQuery.keyWord = ""
            this.dialogQuery.taskId = row.taskId;
            this.dialogQuery.typeId = row.typeId;
            this.getDialogDataList()
        },
        /** 查询弹框列表 */
        getDialogDataList() {
            getRepairInputDetailsListApi(this.dialogQuery).then((response) => {
                this.dialogList = response.data.rows
                this.dialogTotal = response.data.total
            })
        },
        /** 弹框搜索操作 */
        handleDialogQuery() {
            this.dialogQuery.pageNum = 1
            this.getDialogDataList()
        },
        /** 弹框重置操作 */
        resetDialogQuery(){
            this.dialogQuery.keyWord = "";
            this.handleDialogQuery()
        },
        /** 弹框导出操作 */
        handleDialogExport() {
          const formatTime = (date) => {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');
            return `${year}${month}${day}_${hours}${minutes}${seconds}`;
          };

          const currentTime = formatTime(new Date());
            this.download(
                'material/bm_report/exportRepairInputDetailsList',
                { ...this.dialogQuery},
                `修饰入库报表_入库设备_${currentTime}.xlsx`,
            )
        },

    }
    };
</script>

<style lang="scss" scoped>
    .image-type {
        /* 旋转图片 */
        transform: rotate(-90deg);
        /* 确保旋转后的图片不会超出容器 */
        max-width: 100%;
        /* 保持图片的宽高比 */
        width: 40px;
        height: 80px;
    }
    .sign-type{
        width: 80px;
        height: 40px;
    }
    .uploadImg {
        padding-top: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .deviceCode {
        margin-top: 10px;
        padding-bottom: 20px;
        font-size: 18px;
    }
    ::v-deep.el-table .fixed-width .el-button--mini {
        width: 60px !important;
        margin-bottom: 10px;
    }
    .clickText {
        color: #02a7f0;
        cursor: pointer;
    }
</style>
