<template>
    <div class="weaningLog">
        <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="getWeaningList"
                        >查找</el-button
                    >
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="add">新增</el-button>
                </el-col>
            </el-row>
        </header>
        <section>
            <article class="table">
                <el-table
                    :data="tableData"
                    tooltip-effect="dark"
                    style="width: 100%"
                >
                    <el-table-column prop="id" label="序号"></el-table-column>
                    <el-table-column label="断奶时间">
                        <template v-slot="scope">
                            <span>{{
                                scope.row["opeTime"].substr(0, 10)
                            }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="sheepId"
                        label="种母羊编码"
                    ></el-table-column>
                    <el-table-column
                        prop="number"
                        label="羔羊数量(只)"
                    ></el-table-column>
                    <el-table-column
                        prop="weight"
                        label="断奶均重量(kg)"
                    ></el-table-column>
                    <el-table-column
                        prop="cost"
                        label="断奶成本(元)"
                    ></el-table-column>
                    <el-table-column label="操作人">
                        <template v-slot="scope">
                            <div
                                v-for="item in $store.state['workerList']"
                                :key="item.value"
                            >
                                <span v-if="item.id == scope.row['opeUser']">{{
                                    `${item.name}(${item.position})`
                                }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <el-button
                                @click="edit(scope.row, true)"
                                type="text"
                                size="small"
                                >编辑</el-button
                            >
                            <el-popconfirm
                                title="是否删除此设备的信息？"
                                @confirm="del(scope.row)"
                            >
                                <el-button
                                    slot="reference"
                                    type="text"
                                    size="small"
                                    >删除</el-button
                                >
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>

                <el-row type="flex" justify="end">
                    <el-col :span="8" class="pagination">
                        <el-pagination
                            @current-change="pageChange"
                            background
                            layout="prev, pager, next"
                            :page-count="Number(totalPages)"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </article>
        </section>

        <el-dialog title="新增/编辑" :visible.sync="isShow" width="40%">
            <el-row type="flex">
                <el-col :span="20">
                    <el-form
                        ref="addWeaning"
                        :model="formData"
                        :rules="rules"
                        label-width="140px"
                    >
                        <el-form-item label="种母羊编码">
                            <el-input v-model="formData.sheepId"></el-input>
                        </el-form-item>

                        <el-form-item label="断奶时间">
                            <el-date-picker
                                v-model="formData.opeTime"
                                type="date"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择日期"
                            ></el-date-picker>
                        </el-form-item>

                        <el-form-item label="羔羊数量">
                            <el-input-number
                                v-model="formData.number"
                                :min="0"
                                :max="5"
                                label="描述文字"
                            ></el-input-number>
                        </el-form-item>

                        <el-form-item label="断奶均重量(kg)">
                            <el-input v-model="formData.weight"></el-input>
                        </el-form-item>

                        <el-form-item label="断奶成本">
                            <el-input v-model="formData.cost"></el-input>
                        </el-form-item>

                        <el-form-item label="操作人">
                            <el-select
                                v-model="formData.opeUser"
                                filterable
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in $store.state['workerList']"
                                    :key="item.id"
                                    :label="`${item.name}(${item.position})`"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item>
                            <el-button @click="isShow = false">取 消</el-button>
                            <el-button
                                type="primary"
                                @click="submitForm('addWeaning')"
                                >保 存</el-button
                            >
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-dialog>
    </div>
</template>

<script>
import {
    reqWeaningList,
    reqAddOrUpdateWeaning,
    reqDelWeaning,
} from "@/api/breed.js";

const pageSize = 10;
const rules = {};

export default {
    name: "weaningLog",
    data() {
        return {
            search: "",
            pageNum: 1,
            tableData: [],
            totalPages: 0,
            isShow: false,
            formData: {
                opeTime: this.$moment().format("YYYY-MM-DD HH:mm:ss"),
                sheepId: "",
                number: 0,
                weight: "",
                cost: "",
                opeUser: null,
            },
            rules,
        };
    },
    created() {
        // 断奶信息列表
        this.getWeaningList();
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    reqAddOrUpdateWeaning(this.formData)
                        .then((res) => {
                            // 断奶信息列表
                            this.getWeaningList();
                            // this.$message.success("成功！");
                            if (res.errCode) {
                                this.$message.error(res.errMsg);
                            } else {
                                this.$message.success("成功");
                                this.formData = {
                                    opeTime: this.$moment().format(
                                        "YYYY-MM-DD HH:mm:ss"
                                    ),
                                    sheepId: "",
                                    number: 0,
                                    weight: "",
                                    cost: "",
                                    opeUser: null,
                                };
                                this.isShow = false;
                            }
                            // this.isShow = false
                        })
                        .catch((err) => {
                            console.log(err);
                            this.$message.error(err);
                        });
                } else {
                    return false;
                }
            });
        },
        // 断奶信息列表
        getWeaningList() {
            reqWeaningList({
                searchStr: this.search,
                pageSize,
                pageNum: this.pageNum,
            })
                .then((res) => {
                    this.tableData = res.content;
                    this.totalPages = res.totalPages;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        add() {
            this.isShow = true;
            // 有id是update 没id是add
            delete this.formData.id;
        },
        edit(row, bool) {
            this.formData.cost = row.cost;
            this.formData.number = row.number;
            this.formData.opeTime = row.opeTime;
            this.formData.opeUser = row.opeUser;
            this.formData.sheepId = row.sheepId;
            this.formData.weight = row.weight;
            if(bool) {
                this.formData.id = row.id;
            }
            this.isShow = true;
        },
        del(row) {
            reqDelWeaning(row.id)
                .then((res) => {
                    // 断奶信息列表
                    this.getWeaningList();
                    if (res.code == "success") {
                        this.$message.success(res.msg);
                    }
                    if (res.errCode == "delete failed") {
                        this.$message.error(res.errMsg);
                    }
                })
                .catch((err) => {
                    console.log(err);
                    this.$message.error(err);
                });
        },
        pageChange(p) {
            console.log(p);
            this.pageNum = p;
            // 断奶信息列表
            this.getWeaningList();
        },
    },
};
</script>

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