<template>
    <div class="seedSheepFile">
        <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="searchStr" placeholder="请选择"></el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="getSeedSheepList">查找</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
                    ref="multipleTable"
                    :data="tableData"
                    tooltip-effect="dark"
                    style="width: 100%"
                >
                    <el-table-column prop="id" label="种母羊编码" width="100"></el-table-column>
                    <el-table-column prop="entryTime" label="入场时间" width="200"></el-table-column>
                    <el-table-column prop="dayOfAge" label="入场日龄"></el-table-column>
                    <el-table-column label="种羊来源">
                        <template v-slot="scope">
                            <div v-for="item in $store.state['seedSheedSource']" :key="item.value">
                                <span v-if="item.value == scope.row['source']">{{ item.label }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="position" label="当前位置"></el-table-column>
                    <el-table-column label="当前状态">
                        <template v-slot="scope">
                            <div v-for="item in $store.state['seedSheedStatus']" :key="item.value">
                                <span v-if="item.value == scope.row['status']">{{ item.label }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="cost" label="成本价格(元)"></el-table-column>
                    <el-table-column prop="weight" label="体重(kg)"></el-table-column>
                    <el-table-column prop="feederName" label="饲养员"></el-table-column>
                    <el-table-column label="等级评级">
                        <template v-slot="scope">
                            <div v-for="item in $store.state['seedSheedLevel']" :key="item.value">
                                <span v-if="item.value == scope.row['level']">{{ item.label }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template v-slot="scope">
                            <el-popconfirm title="是否删除此设备的信息？" @onConfirm="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="45%">
            <el-row type="flex">
                <el-col :span="15">
                    <el-form
                        ref="addSeedSheepForm"
                        :model="formData"
                        :rules="rules"
                        label-width="140px"
                    >
                        <el-form-item label="日龄">
                            <el-input v-model="formData.dayOfAge"></el-input>
                        </el-form-item>

                        <el-form-item label="种羊来源">
                            <el-radio-group v-model="formData.source">
                                <el-radio
                                    v-for="item in $store.state['seedSheedSource']"
                                    :key="item.value"
                                    :label="item.value"
                                >{{ item.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>

                        <el-form-item label="当前位置">
                            <el-input v-model="formData.position"></el-input>
                        </el-form-item>

                        <el-form-item label="当前状态">
                            <el-select v-model="formData.status" placeholder="请选择">
                                <el-option
                                    v-for="item in $store.state['seedSheedStatus']"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="成本价格(元)">
                            <el-input v-model="formData.cost"></el-input>
                        </el-form-item>

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

                        <el-form-item label="饲养员">
                            <el-select v-model="formData.feeder" placeholder="请选择">
                                <el-option label="甲" :value="90003"></el-option>
                                <el-option label="乙" :value="2"></el-option>
                                <el-option label="丙" :value="3"></el-option>
                                <el-option label="丁" :value="4"></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="等级评级">
                            <el-radio-group v-model="formData.level">
                                <el-radio
                                    v-for="item in $store.state['seedSheedLevel']"
                                    :key="item.value"
                                    :label="item.value"
                                >{{ item.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>

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

<script>
import {
    reqSeedSheepList,
    reqAddSeedSheepFile,
    reqDelSeedSheepFile
} from "@/api/breed.js";

const pageSize = 10;
const rules = {};

export default {
    data() {
        return {
            page: 1,
            searchStr: "",
            tableData: [],
            totalPages: 0,
            isShow: false,
            rules,
            formData: {
                dayOfAge: "222",
                source: 1,
                position: "xx牧场",
                status: 1,
                cost: 3000,
                weight: 48,
                feeder: 90003,
                level: 1
            }
        };
    },
    created() {
        this.getSeedSheepList();
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 新增种羊档案
                    reqAddSeedSheepFile(this.formData)
                        .then(res => {
                            // 获取种羊档案列表
                            this.getSeedSheepList();
                            this.$message.success("新增种羊档案成功！");
                        })
                        .catch(err => {
                            console.log(err);
                            this.$message.error("新增种羊档案失败！");
                        });
                } else {
                    return false;
                }
            });
        },
        // 获取种羊档案列表
        getSeedSheepList() {
            reqSeedSheepList({
                pageNum: this.page,
                pageSize,
                searchStr: this.searchStr
            })
                .then(res => {
                    this.tableData = res.content;
                    this.totalPages = res.totalPages;
                })
                .catch(err => {
                    console.log(err);
                });
        },

        add() {
            this.isShow = true;
        },
        del(row) {
            console.log(row);
            reqDelSeedSheepFile({ id: row.id })
                .then(res => {
                    this.getSeedSheepList();
                    this.$message.success("删除种羊档案成功！");
                })
                .catch(err => {
                    console.log(err);
                    this.$message.error("删除种羊档案失败！");
                });
        },
        pageChange(p) {
            console.log(p);
            this.page = p;
            this.getSeedSheepList();
        }
    }
};
</script>

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