<template>
    <div>
        <div>
            <div style="color:red;margin-right:20px">低级错误分为加分项和减分项，当实际值为负值时，属于加分项，当实际值为正值时，属于减分项</div>

        </div>
        <avue-crud :option="option" :table-loading="loading" :data="data" v-model="form" v-model:page="page" ref="crud"
            @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
            @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
            @refresh-change="refreshChange" @on-load="onLoad">
            <template #menu-left>
                <el-button type="primary" @click="maintenanceClick">维护</el-button>
                <el-button type="primary" @click="proofreadClick">校对</el-button>
            </template>
            <template #menu-right>

            </template>
            <template #menu="scope">

            </template>

            <template #qualityStatus="scope">
                <el-tag v-if="scope.row.qualityStatus == 1">未维护</el-tag>
                <el-tag v-else type="success">待校对</el-tag>
            </template>
            
            <template #hgxTarget-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #hgxTarget="{ row }">
                <el-input :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).hgxTarget === null || maintenanceArr.find(item => item.id == row.id).hgxTarget === '') && 'error'" v-model="row.hgxTarget"></el-input>
            </template>
            <template #hgxReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #hgxReality="{ row }">
                <el-input :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).hgxReality === null || maintenanceArr.find(item => item.id == row.id).hgxReality === '') && 'error'" v-model="row.hgxReality"></el-input>
            </template>
            <template #dppmTarget-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #dppmTarget="{ row }">
                <el-input :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).dppmTarget === null || maintenanceArr.find(item => item.id == row.id).dppmTarget === '') && 'error'" v-model="row.dppmTarget"></el-input>
            </template>
            <template #dppmReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #dppmReality="{ row }">
                <el-input :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).dppmReality === null || maintenanceArr.find(item => item.id == row.id).dppmReality === '') && 'error'" v-model="row.dppmReality"></el-input>
            </template>
            <template #djcwReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #djcwReality="{ row }">
                <el-input :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).djcwReality === null || maintenanceArr.find(item => item.id == row.id).djcwReality === '') && 'error'" v-model="row.djcwReality"></el-input>
            </template>
            <template #qualityTsReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #qualityTsReality="{ row }">
                <el-input-number controls-position="right" style="width:90%;" :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).qualityTsReality === null || maintenanceArr.find(item => item.id == row.id).qualityTsReality === '') && 'error'" v-model="row.qualityTsReality"></el-input-number>
            </template>
            <template #pcxReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #pcxReality="{ row }">
                <el-input-number controls-position="right" style="width:90%;" :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).pcxReality === null || maintenanceArr.find(item => item.id == row.id).pcxReality === '') && 'error'" v-model="row.pcxReality"></el-input-number>
            </template>
            <template #dywReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #dywReality="{ row }">
                <el-input-number controls-position="right" style="width:90%;" :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).dywReality === null || maintenanceArr.find(item => item.id == row.id).dywReality === '') && 'error'" v-model="row.dywReality"></el-input-number>
            </template>
            <template #qlwtReality-header="{ column }">
                <span style="color: red;margin-right:5px;">*</span>{{column.label}}
            </template>
            <template #qlwtReality="{ row }">
                <el-input-number controls-position="right" style="width:90%;" :class="maintenanceArr.length > 0 
                    && maintenanceArr.find(item => item.id == row.id) 
                    && (maintenanceArr.find(item => item.id == row.id).qlwtReality === null || maintenanceArr.find(item => item.id == row.id).qlwtReality === '') && 'error'" v-model="row.qlwtReality"></el-input-number>
            </template>
            <!-- <template #hgxReality-header="{ column }">
                <i style="color:red">*</i>{{ (column || {}).label }}
            </template> -->
        </avue-crud>
    </div>
</template>
<script>
import { getList,maintenanceData,checkData } from "@/api/outsourcingManagement/performanceCalculate"
export default {
    components: {

    },
    data() {
        return {
            selectionList: [],
            loading:false,
            query: {},
            maintenanceArr:[],
            otherData:[],
            option: {
                height: 'auto',
                calcHeight: 32,
                tip: false,
                // size: 'medium',
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                searchIcon: true,
                searchIndex: 3,
                tree: false,
                border: true,
                index: true,
                selection: false,
                viewBtn: false,
                delBtn: false,
                addBtn: false,
                editBtn: false,
                addBtnText: '申请',
                editBtnText: '修改',
                viewBtnText: '详情',
                labelWidth: 120,
                menuWidth: 180,
                dialogWidth: 640,
                dialogClickModal: false,
                searchEnter: true,
                excelBtn: false,
                filterBtn: true,
                searchShowBtn: false,
                columnSort: true,
                excelBtn: true,
                columnSort: true,
                showOverflowTooltip: true,
                menu: false,
                searchLabelPosition: 'left',
                searchLabelPosition: 'left',
                searchGutter: 24,
                searchSpan: 6,
                menuAlign: 'left',
                gridBtn: false,
                searchMenuPosition: 'right',
                addBtnIcon: ' ',
                viewBtnIcon: ' ',
                delBtnIcon: ' ',
                editBtnIcon: ' ',
                align: 'center',
                column: [
                    {
                        label: '工艺能力',
                        prop: 'caName',
                        sortable: true,
                        filter: true,
                        span: 24,
                        search: false,
                        width: 130,
                    },
                    {
                        label: '厂家代码',
                        prop: 'oemCode',
                        search: false,
                        sortable: true,
                        filter: true,
                        span: 24,
                        width: 120,
                    },
                    {
                        label: '厂家名称',
                        prop: 'oemName',
                        // bind: 'bsOemCustomer.ocName',
                        search: false,
                        sortable: true,
                        filter: true,
                        span: 24,
                        width: 120,
                    },

                    {
                        label: '后工序审理单',
                        children: [
                            {
                                label: '目标值',
                                prop: 'hgxTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },
                            {
                                label: '实际值',
                                prop: 'hgxReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: 'DPPM',
                        children: [
                            {
                                label: '目标值',
                                prop: 'dppmTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },
                            {
                                label: '实际值',
                                prop: 'dppmReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: '低级错误',
                        children: [
                            {
                                label: '目标值',
                                prop: 'djcwTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                            },
                            {
                                label: '实际值',
                                prop: 'djcwReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: '顾客投诉(扣分项)',
                        children: [
                            {
                                label: '目标值',
                                prop: 'qualityTsTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                            },
                            {
                                label: '实际值',
                                prop: 'qualityTsReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                cell: true,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: '批次性(扣分项)',
                        children: [
                            {
                                label: '目标值',
                                prop: 'pcxTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                            },
                            {
                                label: '实际值',
                                prop: 'pcxReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                cell: true,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: '多余物(扣分项)',
                        children: [
                            {
                                label: '目标值',
                                prop: 'dywTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                            },
                            {
                                label: '实际值',
                                prop: 'dywReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                cell: true,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: '清零问题(扣分项)',
                        children: [
                            {
                                label: '目标值',
                                prop: 'qlwtTarget',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                            },
                            {
                                label: '实际值',
                                prop: 'qlwtReality',
                                search: false,
                                sortable: true,
                                filter: true,
                                span: 24,
                                width: 120,
                                cell: true,
                                rules: [
                                    { required: true, message: '请输入姓名', trigger: 'blur' }
                                ]
                            },

                        ]
                    },
                    {
                        label: '状态',
                        prop: 'qualityStatus',
                        search: false,
                        sortable: true,
                        filter: true,
                        span: 24,
                    },
                ]
            },
            form: {

            },
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            omId: null,
            isOpen: false
        }
    },
    methods: {
        calcData(a,b){
            // 需要比较的字段
            const fieldsToCheck = ['djcwReality','qualityTsReality','pcxReality', 'dywReality', 'qlwtReality','dppmTarget',
            'dppmReality','hgxTarget','hgxReality'];

            const c = b.filter((itemB, index) => {
                // 策略 A: 如果两个数组顺序严格一致，直接使用索引匹配 (最快)
                const itemA = a[index]; 
                
                // 策略 B: 如果顺序可能不一致，需要通过 id 查找 (更稳健，但如果有重复 ID 会匹配到第一个)
                // const itemA = a.find(item => item.id === itemB.id);

                if (!itemA) return false; // a 中找不到对应的项，视情况决定是否保留，这里暂不保留

                // 检查指定字段是否有任何一个不相等
                const isDifferent = fieldsToCheck.some(key => {
                return itemA[key] !== itemB[key];
                });

                return isDifferent;
            });
            return c
        },
        maintenanceClick(){
            this.maintenanceArr = this.calcData(this.otherData,this.data)
            console.log('main------------',this.maintenanceArr)

            let djcwError = this.maintenanceArr.find(item =>  item.djcwReality === null)
            let qualityError = this.maintenanceArr.find(item => item.qualityTsReality === null)
            let pcxError = this.maintenanceArr.find(item =>  item.pcxReality === null)
            let dywError = this.maintenanceArr.find(item =>  item.dywReality === null)
            let qlwtError = this.maintenanceArr.find(item =>  item.qlwtReality === null)
            let dppmTargetError = this.maintenanceArr.find(item =>  item.dppmTarget === null)
            let dppmRealityError = this.maintenanceArr.find(item =>  item.dppmReality === null)
            let hgxTargetError = this.maintenanceArr.find(item =>  item.hgxTarget === null)
            let hgxRealityError = this.maintenanceArr.find(item =>  item.hgxReality === null)
            
            if(djcwError || qualityError || pcxError || dywError || qlwtError || dppmTargetError || dppmRealityError || hgxTargetError || hgxRealityError){
                this.$message.error('请填写表格中红框数据')
                return
            }
            maintenanceData({type:"quality",oemMeritsEntities:this.maintenanceArr}).then(res =>{
                if(res.data.code === 200){
                    this.$message.success('维护成功')
                    this.onLoad()
                }
            })
        },
        proofreadClick(){
            let checkArr = this.data.filter(item => item.qualityStatus == 2)
            checkData({
                oemMeritsEntities:checkArr,
                type:'quality'
            }).then(res =>{
                if(res.data.code === 200){
                this.$message.success('校对成功')
                this.onLoad()
                }
            })
        },
        addEdit() {
            this.isOpen = true
        },
        closeDialog() {
            this.isOpen = false
        },
        deleteClick(omId) {
            this.$confirm('是否删除此数据?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    // this.$ajax.get('oemMerits/deleteByKey/' + omId).then((res) => {
                    //     if (this.$ifAjax(res)) {
                    //         this.$message.success(this.$t('global.deleteOk'));
                    //         this.$refs.myTable.load();
                    //     }
                    // });
                })
                .catch(() => {
                    this.$message.info('已取消');
                });
        },
        async editClick(omId) {
            this.omId = omId;
            // const res = await this.$ajax.get('oemMerits/getByKey/' + omId);
            // if (this.$ifAjax(res)) {
            //     this.rowItem = res.data;
            //     this.editOpen = true;
            // }
        },

        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning('请选择至少一条数据');
                return;
            }
            this.$confirm('确定将选择数据删除?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
            })
        },
        // 多选
        selectionChange(list) {
            this.selectionList = list;
        },
        onLoad() {
            this.loading = true
            getList({
                current:this.page.currentPage,
                size:this.page.pageSize,
                qualityStatusList:'1,2',
                statusList:"1,2",
                ...this.query,
            }).then((res) => {
                this.data = res.data.data.records
                this.otherData = JSON.parse(JSON.stringify(res.data.data.records));
                this.page.total = res.data.data.total
                this.loading = false
            })
            // this.data = [
            //     {
            //         $cellEdit: true,
            //         "bsOemCustomer": {
            //             "abbreviation": "L076",
            //             "address": "aaa",
            //             "codeAndName": "L076/L076",
            //             "contactMan": "aaa",
            //             "contactPhone": "aaa",
            //             "craftAbility": "镀银、挂镀银、喷砂、涂色标、铜合金化学镀镍、电镀镍、DL零件包装",
            //             "curStatus": 0,
            //             "curStatusText": "正常供货",
            //             "email": "aaa",
            //             "keyValue": 21,
            //             "limitType": 0,
            //             "ocCode": "L076",
            //             "ocId": 21,
            //             "ocName": "L076",
            //             "oemPassword": null,
            //             "partCodeYesOrNo": false,
            //             "qualification": 10,
            //             "qualificationTitle": null,
            //             "region": "陕西省",
            //             "updateTime": "2023-04-14 18:33:49"
            //         },
            //         "createMan": {
            //             "userId": 541,
            //             "userName": "崔殿龙"
            //         },
            //         "createTime": "2023-11-10 18:06:40",
            //         "keyValue": 1,
            //         "oaaId": 1,
            //         "plate": "222"
            //     },
            //     {
            //         $cellEdit: true,
            //         "bsOemCustomer": {
            //             "abbreviation": "L120",
            //             "address": "aaa",
            //             "codeAndName": "L120/L120",
            //             "contactMan": "aaa",
            //             "contactPhone": "aaa",
            //             "craftAbility": "不锈钢涂福斯润滑剂、DL零件包装、",
            //             "curStatus": 0,
            //             "curStatusText": "正常供货",
            //             "email": "aaa",
            //             "keyValue": 2,
            //             "limitType": 0,
            //             "ocCode": "L120",
            //             "ocId": 2,
            //             "ocName": "L120",
            //             "oemPassword": null,
            //             "partCodeYesOrNo": false,
            //             "qualification": 0,
            //             "qualificationTitle": "民品&普军",
            //             "region": "江苏省",
            //             "updateTime": "2023-04-14 18:33:49"
            //         },
            //         "createMan": {
            //             "userId": 541,
            //             "userName": "崔殿龙"
            //         },
            //         "createTime": "2023-11-10 18:06:40",
            //         "keyValue": 4,
            //         "oaaId": 4,
            //         "plate": "222"
            //     },
            //     {
            //         $cellEdit: true,
            //         "bsOemCustomer": {
            //             "abbreviation": "L070",
            //             "address": "aaa",
            //             "codeAndName": "L070/L070",
            //             "contactMan": "aaa",
            //             "contactPhone": "aaa",
            //             "craftAbility": "滚镀金或振镀金、局部镀厚金、闪镀金、镀铂金、局部退火",
            //             "curStatus": 0,
            //             "curStatusText": "正常供货",
            //             "email": "aaa",
            //             "keyValue": 5,
            //             "limitType": 0,
            //             "ocCode": "L070",
            //             "ocId": 5,
            //             "ocName": "L070",
            //             "oemPassword": null,
            //             "partCodeYesOrNo": false,
            //             "qualification": 10,
            //             "qualificationTitle": null,
            //             "region": "江苏省",
            //             "updateTime": "2023-04-14 18:33:49"
            //         },
            //         "createMan": {
            //             "userId": 541,
            //             "userName": "崔殿龙"
            //         },
            //         "createTime": "2023-12-06 17:24:48",
            //         "keyValue": 21,
            //         "oaaId": 21,
            //         "plate": "222"
            //     }
            // ]
            // this.page.total = this.data.length
            // this.loading = false
            // setTimeout(() => {
            //     this.selectionClear()
            // }, 500)
        }
    }
}
</script>
<style lang="scss" scoped>
.error {
  ::v-deep .el-input__wrapper {
  //   background: red;
  // border:none
    border:1px solid #ff0000 !important;
  }
}
</style>