<template>
    <div class="SlaughterBatch">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">屠宰批次</h2>
        <header id="header">
            <el-row type="flex" :gutter="20">
                <el-col :span="4">
                    <el-input v-model="search" placeholder="请选择"></el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="getSlaughterBatchList">查找</el-button>
                </el-col>
            </el-row>
        </header>
        <section>
            <article class="table">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    tooltip-effect="dark"
                    style="width: 30%"
                    border
                    @selection-change="handleSelectionChange"
                    :row-key="getRowKeys"
                >
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="id" label="序号" width="80" align="center"></el-table-column>
                    <el-table-column prop="sheepId" label="羊只编号" align="center"></el-table-column>
                    <el-table-column prop="weight" label="体重" align="center"></el-table-column>
                </el-table>

                <el-row type="flex" style="margin-top: 20px">
                    <el-col :span="4">
                        <el-button @click="toggleSelection()">取消选择</el-button>
                        <el-button type="primary" plain @click="create">
                            生成批次
                            <i class="el-icon-arrow-right el-icon--right"></i>
                        </el-button>
                    </el-col>
                    <el-col :span="8">
                        <el-pagination
                            @current-change="pageChange"
                            background
                            layout="prev, pager, next"
                            :page-count="Number(totalPages)"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </article>
        </section>
    </div>
</template>

<script>
import { reqSlaughterBatchList, reqCreateBatchList } from "@/api/slaughterManagment.js";

const pageSize = 10;
const rules = {};

export default {
    name: "SlaughterBatch",
    data() {
        return {
            search: "",
            page: 1,
            tableData: [],
            totalPages: 0,
            isShow: false,
            multipleSelection: [],
            getRowKeys(row) {
                return row.id;
            },
            rules
        };
    },
    created() {
        // 屠宰批次列表
        this.getSlaughterBatchList();
    },
    methods: {
        // 屠宰批次列表
        getSlaughterBatchList() {
            reqSlaughterBatchList({
                searchStr: this.search,
                pageSize,
                pageNum: this.page
            })
                .then(res => {
                    this.tableData = res.content;
                    this.totalPages = res.totalPages;
                })
                .catch(err => {
                    console.log(err);
                });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 选项选择
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTableCreateBatch.toggleRowSelection(row);
                });
            } else {
                this.$refs.multipleTableCreateBatch.clearSelection();
            }
        },
        create() {
            let ids = this.multipleSelection.map(item => item.id).toString
            reqCreateBatchList({ids}).then(res => {
                if(res.errCode) {
                    this.$message.error(res.errMsg)
                } else {
                    this.$message.success("成功")
                }
            })
        },
        pageChange(p) {
            console.log(p);
            this.page = p;
            // 屠宰批次列表
            this.getSlaughterBatchList();
        }
    }
};
</script>

<style lang="scss" scoped>
#header {
    margin-bottom: 15px;
}
.table {
    .pagination {
        margin-top: 20px;
    }
}
</style>