<template>
    <div class="app-container" id="returnInDetail">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="80px"
        >
            <!-- <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入关键字"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>-->

            <el-form-item label="类型名称" prop="typeId">
                <el-select
                    v-model="queryParams.typeId"
                    placeholder="请选择类型名称"
                    clearable
                    filterable
                    style="width: 240px"
                >
                    <el-option
                        v-for="typeItem in typeList"
                        :key="typeItem.typeId"
                        :label="typeItem.typeName"
                        :value="typeItem.typeId"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="规格型号" prop="modelId">
                <treeselect
                    v-model="queryParams.modelId"
                    default-expand-all
                    :options="equipmentTypeList"
                    placeholder="请选择规格型号"
                    :disable-branch-nodes="true"
                    style="width: 240px"
                    noChildrenText="没有数据了"
                    noOptionsText="没有数据"
                    noResultsText="没有搜索结果"
                />
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    :loading="loading"
                >
                    查询
                </el-button>
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button icon="el-icon-back" size="mini" @click="back"
                    >返回</el-button
                >
            </el-col>
            <el-col :span="1.5" v-if="!isView">
                <el-button type="primary" size="mini" @click="completeBack"
                    >完成退料</el-button
                >
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="deviceList">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
                :index="indexContinuation(queryParams.pageNum, queryParams.pageSize)"
            >
                <!-- <template slot-scope="scope">
                    <span>{{
                        (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
                    }}</span>
                </template> -->
            </el-table-column>
            <el-table-column
                label="类型名称"
                align="center"
                prop="typeName"
                :show-overflow-tooltip="true"
            />
            <el-table-column type="expand" v-if="isPlural">
                <template slot-scope="scope">
                    <div class="nested-table-container">
                        <el-table
                            :data="scope.row.maTypeDetails"
                            style="width: 100%"
                        >
                            <!-- 子表格的列 -->
                            <el-table-column
                                label="序号"
                                align="center"
                                type="index"
                            />
                            <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="partNum"
                                :show-overflow-tooltip="true"
                            />
                        </el-table>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="规格型号"
                align="center"
                prop="typeCode"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="申请数量"
                align="center"
                prop="preNum"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="退料数量"
                align="center"
                :show-overflow-tooltip="true"
            >
                <template slot-scope="scope">
                    <span>
                        {{
                            (scope.row.partNum =
                                scope.row.manageType == 0 ||
                                scope.row.manageType == 1
                                    ? scope.row.num
                                    : scope.row.partNum)
                        }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                width="300"
            >
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        @click="handleViewCode(scope.row)"
                        v-if="
                            scope.row.manageType == '0' ||
                            scope.row.manageType == '1' ||
                            scope.row.manageType == '2'
                        "
                    >
                        查看
                    </el-button>
                    <!-- <el-button
            size="mini"
            @click="handleViewNum(scope.row)"
            v-if="scope.row.manageType=='1'"
          >查看</el-button>-->
                    <el-button
                        size="mini"
                        type="primary"
                        @click="handleCodeReturn(scope.row)"
                        v-if="
                            !isView &&
                            scope.row.manageType == '0' &&
                            scope.row.num > 0
                        "
                    >
                        编码退料
                    </el-button>
                    <el-button
                        size="mini"
                        type="primary"
                        @click="handleNumReturn(scope.row)"
                        v-if="
                            !isView &&
                            (scope.row.manageType == '1' ||
                                scope.row.manageType == '2') &&
                            scope.row.num > 0
                        "
                    >
                        数量退料
                    </el-button>
                    <el-button
                        size="mini"
                        icon="el-icon-zoom-in"
                        @click="handleBackup(scope.row)"
                        v-if="!isView && scope.row.num < scope.row.preNum"
                    >
                        撤回
                    </el-button>
                </template>
            </el-table-column>
        </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="openCode"
            width="1200px"
            append-to-body
        >
            <div style="margin-left: 40px; margin-bottom: 10px"
                >退料数量：{{ returnNum }}</div
            >
            <el-form
                :model="codeQuery"
                ref="codeQuery"
                size="small"
                :inline="true"
                label-width="100px"
            >
                <el-form-item label="设备编码" prop="maCode">
                    <el-input v-model="codeQuery.maCode" maxlength="20" />
                </el-form-item>
                <el-form-item>
                    <el-button
                        type="primary"
                        icon="el-icon-search"
                        size="mini"
                        @click="handleCodeQuery"
                        >查询</el-button
                    >
                    <el-button
                        icon="el-icon-refresh"
                        size="mini"
                        @click="resetCodeQuery"
                        >重置</el-button
                    >
                </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8" v-if="!dialogIsView">
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        size="mini"
                        :disabled="multiple"
                        @click="saveCodeBackBatch(1)"
                        >批量合格</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        size="mini"
                        :disabled="multiple"
                        @click="saveCodeBackBatch(2)"
                        >批量维修</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        size="mini"
                        :disabled="multiple"
                        @click="saveCodeBackBatch(3)"
                        >批量报废</el-button
                    >
                </el-col>
            </el-row>
            <el-table
                v-loading="loading"
                :data="codeList"
                height="500"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="序号"
                    align="center"
                    width="80"
                    type="index"
                    :index="indexContinuation(codeQuery.pageNum, codeQuery.pageSize)"
                >
                    <!-- <template slot-scope="scope">
                        <span>{{
                            (codeQuery.pageNum - 1) * 10 + scope.$index + 1
                        }}</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="maCode"
                    :show-overflow-tooltip="true"
                />
                <el-table-column
                    label="状态"
                    align="center"
                    prop="maStatus"
                    :show-overflow-tooltip="true"
                />
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    v-if="!dialogIsView"
                    width="250"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="success"
                            @click="saveCodeBack(scope.row, 1)"
                            >合格</el-button
                        >
                        <el-button
                            size="mini"
                            type="primary"
                            @click="saveCodeBack(scope.row, 2)"
                            >待维修</el-button
                        >
                        <el-button
                            size="mini"
                            type="warning"
                            @click="saveCodeBack(scope.row, 3)"
                            >预报废</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="codeTotal > 0"
                :total="codeTotal"
                :page.sync="codeQuery.pageNum"
                :limit.sync="codeQuery.pageSize"
                @pagination="getCodeList"
            />
        </el-dialog>

        <!-- 数量退料弹窗 -->
        <el-dialog
            :title="title"
            :visible.sync="openNum"
            width="1200px"
            append-to-body
        >
            <div v-if="dialogData.typeMange == 2">
                <div style="margin-bottom: 30px">
                    接收抱杆类型：{{ dialogData.typeName }}，型号：{{
                        dialogData.typeCode
                    }}，付数：{{ returnNum }}。接收明细如下
                </div>
                <el-row :gutter="10" class="mb8" v-if="!dialogIsView">
                    <el-col :span="1.5">
                        <el-button
                            type="primary"
                            size="mini"
                            v-if="returnNum > 0"
                            @click="saveNumReturn(dialogData.typeMange)"
                        >
                            保存
                        </el-button>
                    </el-col>
                </el-row>
                <el-table :data="dialogData.maTypeDetails" style="width: 100%">
                    <!-- 子表格的列 -->
                    <el-table-column label="序号" align="center" type="index" />
                    <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="partNum"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="合格数量"
                        align="center"
                        prop="qualifiedNum"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.number="scope.row.qualifiedNum"
                                placeholder="请输入合格数量"
                                type="number"
                                :min="0"
                                clearable
                                :disabled="dialogIsView"
                                style="width: 100%"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="待维修数量"
                        align="center"
                        prop="serviceNum"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.number="scope.row.serviceNum"
                                placeholder="请输入待维修数量"
                                type="number"
                                :min="0"
                                clearable
                                :disabled="dialogIsView"
                                style="width: 100%"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="待报废数量"
                        align="center"
                        prop="scrapNum"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.number="scope.row.scrapNum"
                                placeholder="请输入待报废数量"
                                type="number"
                                :min="0"
                                clearable
                                :disabled="dialogIsView"
                                style="width: 100%"
                            />
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <div v-else>
                <div style="margin-left: 40px; margin-bottom: 10px"
                    >退料数量：{{ returnNum }}</div
                >
                <el-row :gutter="10" class="mb8" v-if="!dialogIsView">
                    <el-col :span="1.5">
                        <el-button
                            type="primary"
                            size="mini"
                            v-if="returnNum > 0"
                            @click="saveNumReturn(dialogData.typeMange)"
                        >
                            保存
                        </el-button>
                    </el-col>
                </el-row>
                <el-table v-loading="loading" :data="numList" height="500">
                    <el-table-column label="序号" align="center" type="index" />
                    <el-table-column
                        label="类型名称"
                        align="center"
                        prop="typeName"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="规格型号"
                        align="center"
                        prop="typeCode"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="退料数量"
                        align="center"
                        prop="backNum"
                        :show-overflow-tooltip="true"
                    />
                    <el-table-column
                        label="合格数量"
                        align="center"
                        prop="qualifiedNum"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.number="scope.row.qualifiedNum"
                                placeholder="请输入合格数量"
                                type="number"
                                :min="0"
                                clearable
                                :disabled="dialogIsView"
                                style="width: 100%"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="待维修数量"
                        align="center"
                        prop="serviceNum"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.number="scope.row.serviceNum"
                                placeholder="请输入待维修数量"
                                type="number"
                                :min="0"
                                clearable
                                :disabled="dialogIsView"
                                style="width: 100%"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="待报废数量"
                        align="center"
                        prop="scrapNum"
                    >
                        <template slot-scope="scope">
                            <el-input
                                v-model.number="scope.row.scrapNum"
                                placeholder="请输入待报废数量"
                                type="number"
                                :min="0"
                                clearable
                                :disabled="dialogIsView"
                                style="width: 100%"
                            />
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="报废信息" align="center" prop="" :show-overflow-tooltip="true" /> -->
                </el-table>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    receiveView,
    getBackMachine,
    backReceiveRecordWeb,
    backReceiveRecordWebPt,
    setNumBack,
    setCodeBack,
    getRecord,
    endBack,
    revoke,
} from '@/api/claimAndRefund/return'
import { getTypeList } from '@/api/store/warehousing'
import { equipmentTypeTree } from '@/api/store/tools'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { getInfo } from '@/api/login'
export default {
    // name: "returnInDetail",
    components: { Treeselect },
    data() {
        return {
            createBy: '', //用户名
            // 遮罩层
            loading: false,
            // 选中数组
            codeSelectList: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            deviceList: [],
            // 弹出层标题
            title: '',
            isView: false, //是否为查看
            isPlural: false,
            dialogIsView: false, //弹窗是否为查看
            openIn: false,
            // 日期范围
            dateRange: [],
            //搜索下拉数据
            typeList: [],
            equipmentTypeList: [],
            dialogData: {
                maTypeDetails: [],
                typeMange: undefined,
                typeName: undefined,
                typeCode: undefined,
            },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,

                unitId: undefined,
                modelId: undefined,
                typeId: undefined,
                agreementId: '',
            },
            openCode: false, //编码弹窗开关
            openNum: false, //数量弹窗开关
            returnNum: 0, //退料数量
            codeQuery: {
                //编码出库-搜索条件
                pageNum: 1,
                pageSize: 20,
                maCode: undefined,
            },
            codeTotal: 0, //编码弹窗分页-总数
            codeList: [], //编码弹窗表格数据
            numList: [], //数量弹窗表格数据
        }
    },
    mounted() {
        if (this.$route.query.isView) {
            this.isView = this.$route.query.isView
        } else {
            this.isView = false
        }
        if (this.$route.query.Id) {
            this.queryParams.id = this.$route.query.Id
        }
        if (this.$route.query.taskId) {
            this.queryParams.taskId = this.$route.query.taskId
        }
        this.getTypeList()

        this.equipmentType()

        this.getList()

        getInfo().then((res) => {
            this.createBy = res.user.userId
        })
    },
    methods: {
        //机具类型
        getTypeList() {
            getTypeList({ level: '3' }).then((response) => {
                this.typeList = response.data
            })
        },
        //规格型号
        equipmentType() {
            equipmentTypeTree().then((response) => {
                this.equipmentTypeList = response.data
                this.equipmentTypeList.forEach((item, index) => {
                    if (item.children && item.children.length > 0) {
                        item.children.forEach((item2, index2) => {
                            if (item2.children && item2.children.length > 0) {
                                item2.children.forEach((item3) => {
                                    if (
                                        item3.children &&
                                        item3.children.length > 0
                                    ) {
                                        item3.children.forEach((item4) => {
                                            item4.machineTypeName =
                                                item3.typeName
                                            item4.specificationType =
                                                item4.typeName
                                            this.$set(item4, 'purchasePrice', 0)
                                            this.$set(item4, 'purchaseNum', 1)
                                        })
                                    }
                                })
                            }
                        })
                    }
                })
            })
        },
        /** 查询列表 */
        getList() {
            this.loading = true
            receiveView(this.queryParams)
                .then((response) => {
                    this.deviceList = response.data.rows
                    console.log(this.deviceList, '  this.deviceList')

                    // this.deviceList.forEach(item => {
                    //   console.log(item.num, 'item.num')
                    //   if (item.manageType == '0' || item.manageType == '1') {
                    //     item.partNum = item.num
                    //   } else {
                    //     item.partNum = item.partNum
                    //   }
                    //   if (item.maTypeDetails.length > 0) {
                    //     this.isPlural = true
                    //   }
                    // })

                    console.log(this.deviceList, 'deviceList')

                    this.total = response.data.total
                    this.loading = false
                })
                .catch(() => {
                    this.loading = false
                })
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        //返回
        back() {
            this.$tab.closeOpenPage({ path: '/claimAndRefund/returnIn' })
        },
        //完成退料接收
        async completeBack() {
            let param1 = {
                parentId: this.queryParams.id,
            }
            const res1 = await getRecord(param1)
            if (res1.code == 200) {
                let param = {
                    createBy: this.createBy,
                    parentId: this.queryParams.id,
                    taskId: this.queryParams.taskId,
                }
                endBack(param).then((response) => {
                    if (response.code == 200) {
                        this.$modal.msgSuccess('操作成功')
                        this.back()
                    }
                })
            }
        },
        //查看数据
        handleViewCode(row) {
            if (row.manageType == '1') {
                let param = {
                    parentId: row.id,
                    typeId: row.modelId,
                }
                backReceiveRecordWeb(param).then((res) => {
                    if (res.code == 200) {
                        this.title = '查看1'
                        this.openNum = true
                        this.dialogIsView = true
                        this.numList = res.data.rows
                        this.numList.forEach((item) => {
                            item.qualifiedNum = item.hgNum
                            item.serviceNum = item.wxNum
                            item.scrapNum = item.bfNum
                            // if (item.backStatus == '1') {
                            //   item.qualifiedNum = item.backNum
                            //   item.serviceNum = 0
                            //   item.scrapNum = 0
                            // }
                            // if (item.backStatus == '2') {
                            //   item.serviceNum = item.backNum
                            //   item.qualifiedNum = 0
                            //   item.scrapNum = 0
                            // }
                            // if (item.backStatus == '3') {
                            //   item.scrapNum = item.backNum
                            //   item.qualifiedNum = 0
                            //   item.serviceNum = 0
                            // }
                        })
                    }
                })
            } else if (row.manageType == '0') {
                this.title = '查看'
                this.openCode = true
                this.resetForm('codeQuery')
                this.codeQuery.parentId = row.id
                this.codeQuery.typeId = row.modelId
                this.dialogIsView = true
                this.returnNum = Number(row.num)
                this.handleCodeQuery()
            } else if (row.manageType == '2') {
                this.handleNumReturn(row)
                const modelIdsJoined = row.maTypeDetails
                    .map((item) => item.modelId)
                    .join(',')
                let param = {
                    parentId: row.id,
                    typeId: modelIdsJoined,
                    agreementId: row.agreementId,
                }
                backReceiveRecordWeb(param).then((res) => {
                    if (res.code == 200) {
                        this.title = '查看'
                        this.openNum = true
                        this.dialogIsView = true
                        this.dialogData.maTypeDetails.map((itemA) => {
                            const matchingItemB = res.data.rows.find(
                                (itemB) => itemA.modelId === itemB.modelId,
                            )
                            if (matchingItemB) {
                                // switch (matchingItemB.backStatus) {
                                //   case '1':
                                itemA.qualifiedNum = matchingItemB.hgNum
                                //   break
                                // case '2':
                                itemA.serviceNum = matchingItemB.wxNum
                                //   break
                                // case '3':
                                itemA.scrapNum = matchingItemB.bfNum
                                //   break
                                // default:
                                // }
                            }
                        })
                    }
                })
            }
        },
        //编码退料
        handleCodeReturn(row) {
            this.title = '编码退料'
            this.openCode = true
            this.resetForm('codeQuery')
            this.codeQuery.parentId = row.id
            this.codeQuery.taskId = row.taskId
            this.codeQuery.typeId = row.modelId
            this.codeQuery.agreementId = row.agreementId
            this.dialogIsView = false
            this.returnNum = Number(row.num)
            this.handleCodeQuery()
        },
        //数量退料
        handleNumReturn(row) {
            this.title = '数量退料'
            this.openNum = true
            this.dialogIsView = false
            this.$set(row, 'backNum', row.num) //退料数量
            this.returnNum = Number(row.num)
            this.dialogData.typeMange = row.manageType
            this.dialogData.typeName = row.typeName
            this.dialogData.typeCode = row.typeCode
            if (row.maTypeDetails) {
                row.maTypeDetails.forEach((item) => {
                    this.$set(item, 'qualifiedNum', undefined)
                    this.$set(item, 'serviceNum', undefined)
                    this.$set(item, 'scrapNum', undefined)
                })
                this.dialogData.maTypeDetails = row.maTypeDetails
            }
            this.numList = [row]
            // this.getNumList()
        },
        //
        handleBackup(row) {
            // console.log(row)
            let param = {
                parentId: row.id,
                typeId: row.modelId,
                manageType: row.manageType,
                createBy: this.createBy,
            }
            if (row.maTypeDetails && row.maTypeDetails.length > 0) {
                param.typeIds = row.maTypeDetails.map((item) => {
                    return item.modelId
                })
            }
            console.log(param)
            revoke(param).then((response) => {
                if (response.code == 200) {
                    this.$modal.msgSuccess('操作成功')
                    this.handleQuery()
                }
            })
        },
        //编码退料查询
        handleCodeQuery() {
            this.codeQuery.pageNum = 1
            if (this.dialogIsView) {
                this.getCodeViewList()
            } else {
                this.getCodeList()
            }
        },
        //编码退料重置
        resetCodeQuery() {
            this.resetForm('codeQuery')
            this.handleCodeQuery()
        },
        //获取退料编码列表
        getCodeList() {
            getBackMachine(this.codeQuery).then((response) => {
                this.codeList = response.data.rows
                this.codeTotal = response.data.total
            })
        },
        //获取退料编码查看列表
        getCodeViewList() {
            backReceiveRecordWebPt(this.codeQuery).then((response) => {
                this.codeList = response.data.rows
                // this.codeList
                this.codeList.forEach((item) => {
                    item.typeModelName = item.typeCode
                    if (item.backStatus == '1') {
                        item.maStatus = '合格'
                    }
                    if (item.backStatus == '2') {
                        item.maStatus = '维修'
                    }
                    if (item.backStatus == '3') {
                        item.maStatus = '报废'
                    }
                })
                this.codeTotal = response.data.total
            })
        },

        /** 数量退料按钮 */
        saveNumReturn(type) {
            let data
            const parentId = this.numList[0].id
            const taskId = this.numList[0].taskId
            if (type == 1) {
                data = this.numList
            } else {
                data = this.dialogData.maTypeDetails
            }
            let arr = []
            for (const item of data) {
                let sumUnmber =
                    (item.qualifiedNum || 0) +
                    (item.serviceNum || 0) +
                    (item.scrapNum || 0)
                if (sumUnmber > item.partNum && sumUnmber > item.num) {
                    this.$modal.msgError('退料总量已大于待退料数量！')
                    return
                } else {
                    if (item.qualifiedNum > 0) {
                        let obj = {
                            parentId: parentId,
                            typeId: item.modelId,
                            manageType: 1,
                            createBy: this.createBy,
                            backStatus: 1,
                            backNum: item.qualifiedNum,
                        }
                        arr.push(obj)
                    }
                    if (item.serviceNum > 0) {
                        let obj = {
                            parentId: parentId,
                            typeId: item.modelId,
                            manageType: 1,
                            createBy: this.createBy,
                            backStatus: 2,
                            backNum: item.serviceNum,
                        }
                        arr.push(obj)
                    }
                    if (item.scrapNum > 0) {
                        let obj = {
                            parentId: parentId,
                            typeId: item.modelId,
                            manageType: 1,
                            createBy: this.createBy,
                            backStatus: 3,
                            backNum: item.scrapNum,
                        }
                        arr.push(obj)
                    }
                }
            }
            let param = {
                taskId: taskId,
                arr: arr,
            }
            setNumBack(param).then((response) => {
                if (response.code == 200) {
                    this.$modal.msgSuccess('操作成功')
                    this.openNum = false
                    this.handleQuery()
                }
            })
        },
        /** 编码退料按钮 */
        saveCodeBack(row, type) {
            if (this.returnNum == 0) {
                this.$modal.msgError('退料数量为零，无法操作！')
            } else {
                let arr = [
                    {
                        parentId: this.codeQuery.parentId,
                        typeId: row.typeId,
                        backNum: 1,
                        backStatus: type,
                        manageType: 0,
                        createBy: this.createBy,
                        maId: row.maId,
                    },
                ]
                let param = {
                    taskId: this.codeQuery.taskId,
                    arr: arr,
                }
                setCodeBack(param).then((response) => {
                    if (response.code == 200) {
                        this.$modal.msgSuccess('操作成功')
                        // this.openCode = false;
                        this.returnNum = Number(this.returnNum) - 1
                        this.handleCodeQuery()
                        this.handleQuery()
                    }
                })
            }
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.codeSelectList = selection
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 编码退料 - 批量操作 */
        saveCodeBackBatch(type) {
            if (this.codeSelectList.length > this.returnNum) {
                this.$modal.msgError('退料总量已大于待退料数量！')
            } else {
                let arr = []
                arr = this.codeSelectList.map((item) => {
                    return {
                        parentId: this.codeQuery.parentId,
                        typeId: item.typeId,
                        backNum: 1,
                        backStatus: type,
                        manageType: 0,
                        createBy: this.createBy,
                        maId: item.maId,
                    }
                })
                let param = {
                    taskId: this.codeQuery.taskId,
                    arr: arr,
                }
                console.log(param)
                setCodeBack(param).then((response) => {
                    if (response.code == 200) {
                        this.$modal.msgSuccess('操作成功')
                        // this.openCode = false;
                        this.returnNum =
                            Number(this.returnNum) - this.codeSelectList.length
                        this.handleCodeQuery()
                        this.handleQuery()
                    }
                })
            }
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep.el-table .fixed-width .el-button--mini {
    width: 60px !important;
    margin-bottom: 10px;
}

.clickText {
    color: #02a7f0;
    cursor: pointer;
}
</style>
