<!-- 耗材列表 -->
<template>
    <el-dialog title="选择耗材" :visible.sync="dialogVisible">
        <el-form :model="searchForm" inline>
            <el-form-item>
                <el-input v-model="searchForm.conTypeName" placeholder="耗材类型名称" @input="onInput"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="searchForm.conCode" placeholder="耗材代码" @input="onInput"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="searchForm.conName" placeholder="耗材名称" @input="onInput"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getTableData">查询</el-button>
                <el-button @click="resetHandle">重置</el-button>
            </el-form-item>
        </el-form>
        <div class="head-container">
            <el-table ref="table" :data="tableData.list" v-loading="tableData.loading" @select="firstSelectChange"
                :row-key="getRowKey" @selection-change="onSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="耗材名称" prop="conName"></el-table-column>
                <el-table-column label="价格" prop="price"></el-table-column>
                <el-table-column label="耗材代码" prop="conCode"></el-table-column>
                <el-table-column label="耗材类型名称" prop="conTypeName"></el-table-column>
                <el-table-column label="单位" prop="conUnit"></el-table-column>
                <el-table-column label="最近入库量" prop="lasterInStock"></el-table-column>
                <el-table-column label="库存数量" prop="stockNumber">
                    <template v-slot="scope">
                        {{ formatDecimal(scope.row.stockNumber - scope.row.stockConsume, 2, true) }}
                    </template>
                </el-table-column>
                <!-- <el-table-column label="操作" fixed="right">
                    <template v-slot="scope">
                        <el-button type="primary" size="mini" @click="confirmHandle(scope.row)">选择</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
        </div>
        <el-pagination :total="tableData.total" :current-page="tableData.page + 1" :page-size="tableData.size"
            @current-change="paginationChange" @size-change="sizeChange"
            layout="total, sizes, prev, pager, next, jumper"></el-pagination>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmHandle">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { formatDecimal } from '@/utils'
import { tbConsInfoGet } from "@/api/invoicing";
import _ from 'lodash'

export default {
    data() {
        return {
            formatDecimal,
            dialogVisible: false,
            searchForm: {
                conTypeId: '',
                conTypeName: '',
                conCode: '',
                conName: ''
            },
            resetSearchForm: '',
            tableData: {
                page: 0,
                size: 10,
                total: 0,
                loading: false,
                list: []
            },
            goods: [],
            stayselection: []
        }
    },
    mounted() {
        this.resetSearchForm = { ...this.searchForm }
    },
    methods: {
         reset(){
            this.$refs.table.clearSelection()
        },
        onInput: _.debounce(function (event) { //防抖请求
            this.getTableData()
        }, 500),
        onSelectionChange(selection) {
            this.stayselection = selection
        },
        getRowKey(row) {
            return row.id
        },
        firstSelectChange() {
            // console.log(selection)
            let selection = this.$refs.table.selection
            if (selection.length > 1 && this.isselect) {
                const del_row = selection.shift();
                this.$refs.table.toggleRowSelection(del_row, false);
            }
        },
       
        // 确定选商品
        confirmHandle() {
            // let res = this.$refs.table.selection
            // let res = this.stayselection
            // this.$emit('success', res)
            var uniqueArray = this.stayselection.filter(item1 =>
                !this.goods.some(item2 => item2.productId == item1.id)
            );
            this.$emit('success', uniqueArray)
            this.close()
        },
        // 重置查询
        resetHandle() {
            this.searchForm = { ...this.resetSearchForm }
            this.tableData.page = 0
            this.tableData.size = 10
            this.tableData.list = []
            this.getTableData()
        },
        // 分页大小改变
        sizeChange(e) {
            this.tableData.size = e
            this.getTableData()
        },
        // 分页回调
        paginationChange(e) {
            this.tableData.page = e - 1
            this.getTableData()
        },
        // 耗材列表
        async getTableData() {
            this.tableData.loading = true
            try {
                const res = await tbConsInfoGet({
                    page: this.tableData.page,
                    size: this.tableData.size,
                    ...this.searchForm
                })
                this.tableData.list = res.content
                this.tableData.total = res.totalElements

                if (this.goods.length) {
                    this.$nextTick(() => {
                        this.selection()
                    })
                }
                setTimeout(() => {
                    this.tableData.loading = false
                }, 500);
            } catch (error) {
                console.log(error)
            }
        },
        show(goods) {
            this.dialogVisible = true
            if (goods && goods.length) {
                this.goods = goods
            } else {
                this.goods = []
            }
            this.resetHandle()
        },
        close() {
            this.dialogVisible = false
        },
        selection() {
            this.goods.forEach(row => {
                this.tableData.list.forEach((item, index) => {
                    if (row.id == item.id) {
                        this.$refs.table.toggleRowSelection(this.tableData.list[index]);
                    }
                })
            });
        }
    }
}
</script>

<style scoped lang="scss">
.shop_info {
    display: flex;
    align-items: center;

    span {
        margin-left: 10px;
    }
}
</style>