<template>
    <div class="samplesComponent">
        <DoorView title="检验项">
            <div slot="main">
                <el-table v-if="$route.query.status == 1" :data="newTableData = tableData">
                    <el-table-column align="left" prop="number" label="序号">
                        <template slot-scope="scope">
                            {{scope.row.number = scope.$index + 1}}
                        </template>
                    </el-table-column>
                    <el-table-column align="left" prop="itemName" label="检验项名称">
                    </el-table-column>
                    <el-table-column align="left" prop="unit" label="单位">
                    </el-table-column>
                    <el-table-column align="left" prop="isNotice" label="是否通知">
                        <template slot-scope="scope">
                            <el-switch v-model="scope.row.isNotice" @change="updateSampleTemplateInspectionItemIsTotice(scope.row)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120px" align="left" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-popconfirm title="确认删除该条记录？" @onConfirm="deleteSampleTemplateInspectionItem(scope.row.id)">
                                <el-link type="primary" slot="reference">删除</el-link>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <el-table v-else :data="newTableData">
                    <el-table-column align="left" label="序号">
                        <template slot-scope="scope">
                            {{scope.row.number = scope.$index + 1}}
                        </template>
                    </el-table-column>
                    <el-table-column align="left" prop="itemName" label="检验项名称">
                    </el-table-column>
                    <el-table-column align="left" prop="unit" label="单位">
                    </el-table-column>
                    <el-table-column align="left" prop="isNotice" label="是否通知">
                        <template slot-scope="scope">
                            <el-switch v-model="scope.row.isNotice" @change="updateSampleTemplateInspectionItemIsTotice(scope.row)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120px" align="left" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-popconfirm title="确认删除该条记录？" @onConfirm="deleteDetails(scope.row,scope.$index)">
                                <el-link type="primary" slot="reference">删除</el-link>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <el-row class="newConstruction">
                    <div @click="newSampling">
                        <span>+</span>
                        <span>添加检验项</span>
                    </div>
                </el-row>
            </div>
        </DoorView>
        <el-dialog title="添加检验项" :visible.sync="open" width="600px">
            <el-table :data="testData" tooltip-effect="dark" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column align="left" prop="itemName" label="检验项名称">
                </el-table-column>
                <el-table-column align="left" prop="unit" label="单位">
                </el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="open = false">取消</el-button>
                <el-button size="small" type="primary" @click="saveSampleTemplateInspectionItem()">保存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import DoorView from "@/components/YuComponents/DoorView.vue";
export default {
    name: 'samplesComponent',
    props: {
        tableData: {
            type: Array,
            default: () => {
                return []
            }
        },
        //父组件重新渲染方法
        hongxin: {
            type: Function,
            default: () => {
                return function () {}
            }
        }
    },
    data() {
        return {
            newTableData: [], //新建表格数据
            open: false, //新增检验项弹框
            sampleTemplateInspectionItemSaveList: [], //勾选项新增数据
            testData: []
        }
    },

    components: {
        DoorView
    },
    methods: {
        jumpDetails() {

        },
        //新增取样地点
        newSampling() {
            this.open = true
        },
        async updateSampleTemplateInspectionItemIsTotice(record) {
            let query = {
                isTotice:record.isNotice,
                id:record.id
            }
           const res = await this.$api.sampleTemplateManagement.updateSampleTemplateInspectionItemIsTotice(query).then(response => {
               
           })
        },
        handleSelectionChange(v) {
            this.sampleTemplateInspectionItemSaveList = v
        },
        //新增检验数据保存
        // saveTheNewtset() {
        //     if (this.sampleTemplateInspectionItemSaveList && this.sampleTemplateInspectionItemSaveList.length) {
        //         this.sampleTemplateInspectionItemSaveList.forEach(item => {
        //             this.newTableData.push(item)
        //         })
        //         //拿到newTableData的每一项id
        //         const tableData = this.newTableData.map(item => item.id)
        //         // 过滤testData中不包含tableData相同id数据。
        //         this.testData = this.testData.filter(item => !tableData.includes(item.id));
        //     } else {
        //         this.$message.waning('请选择检验数据')
        //     }
        //     this.open = false
        // },
        deleteDetails(record, index) {
            this.newTableData.splice(index, 1)
        },
        //删除
        async deleteSampleTemplateInspectionItem(query) {
            const res = await this.$api.sampleTemplateManagement.deleteSampleTemplateInspectionItem(query).then(response => {
                if (response.code === 200) {
                    this.testData = response.data
                    this.hongxin()
                }
            })
        },
        //查询可以添加的检验项
        async listSampleTemplateInspectionItem() {
            let dataList = this.$route.query.status == 1 ? this.tableData.map(e => {
                return e.id
            }) : this.newTableData.map(e => {
                return e.id
            })
            const keys = dataList && dataList.length ? dataList.map(item => `ids=${item}`).join('&') : null
            let query = {
                keys
            }
            const res = await this.$api.sampleTemplateManagement.listSampleTemplateInspectionItem(query).then(response => {
                if (response.code === 200) {
                    this.testData = response.data
                }
            })
        },
        //样品检验项保存
        async saveSampleTemplateInspectionItem() {
            if (this.sampleTemplateInspectionItemSaveList && this.sampleTemplateInspectionItemSaveList.length) {
                let sampleTemplateInspectionItemSaveList = this.sampleTemplateInspectionItemSaveList.map(item => {
                    item.inspectionItemId = item.id
                    item.sampleId = this.$route.query.id ? this.$route.query.id : 0
                    delete item.id
                    return item
                })
                const res = await this.$api.sampleTemplateManagement.saveSampleTemplateInspectionItem({
                    sampleTemplateInspectionItemSaveList
                }).then(response => {
                    if (response.code === 200) {
                        response.data.forEach(element => {
                            this.newTableData.push(element)
                        })
                        this.open = false
                        this.listSampleTemplateInspectionItem()
                    }
                })
            }
        }
    },
    mounted() {
        this.listSampleTemplateInspectionItem()
    },
    watch: {
        tableData() {
            this.listSampleTemplateInspectionItem()
        }
    }

}
</script>

<style lang="scss">
.samplesComponent {
    .newConstruction {
        width: 100%;
        height: 38px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 20px;
        border: 1px dashed rgba(0, 0, 0, 0.15);
        cursor: pointer;
    }

    .el-dialog__body {
        min-height: 150px !important;
        max-height: 500px !important;
        // height: 400px;
        overflow-y: auto;
    }
}
</style>
