<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="10">
                    <el-button type="primary" @click="add">新增档案</el-button>暂时别删，要测试删除的话，先自己新增几条数据，别删已经存在的，这关乎到系谱信息档案
                </el-col>
                <el-col :span="4">
                    <el-button-group>
                        <el-button :type="sheepType==0?'primary':''" @click="shiftSheepType(0)">种母羊</el-button>
                        <el-button :type="sheepType==1?'primary':''" @click="shiftSheepType(1)">种公羊</el-button>
                    </el-button-group>
                </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="种羊编码" width="100"></el-table-column>
                    <el-table-column label="入场时间" width="200">
                        <template v-slot="scope">
                            <span>{{ scope.row['entryTime'].substr(0,10) }}</span>
                        </template>
                    </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="饲养员">
                        <template v-slot="scope">
                            <div v-for="item in $store.state['workerList']" :key="item.value">
                                <span
                                    v-if="item.id == scope.row['feeder']"
                                >{{ `${item.name}(${item.position})` }}</span>
                            </div>
                        </template>
                    </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.id"></el-input>
                        </el-form-item>

                        <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-select v-model="formData.position" filterable placeholder="请选择">
                                <el-option
                                    v-for="item in $store.state['areaList']"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.name"
                                ></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item v-if="sheepType==0" label="当前状态" >
                            <el-select v-model="formData.status" filterable 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 v-if="sheepType==1" label="当前状态">
                            <el-select v-model="formData.status" filterable placeholder="请选择">
                                <el-option
                                    v-for="item in $store.state['seedRamSheedStatus']"
                                    :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" 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 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: "",
            sheepType: 0,
            tableData: [],
            totalPages: 0,
            isShow: false,
            rules,
            formData: {
                id: '',
                dayOfAge: "222",
                source: 1,
                position: "",
                status: null,
                cost: 3000,
                weight: 48,
                feeder: this.$store.state["workerList"][0].id,
                level: 1
            }
        };
    },
    created() {
        this.getSeedSheepList();
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 新增种羊档案
                    this.formData.gender = this.sheepType
                    reqAddSeedSheepFile(this.formData)
                        .then(res => {
                            // 获取种羊档案列表
                            this.getSeedSheepList();
                            // this.$message.success("新增种羊档案成功！");
                            if (res.errCode) {
                                this.$message.error(res.errMsg);
                            } else {
                                this.$message.success("成功");
                            }
                        })
                        .catch(err => {
                            console.log(err);
                            this.$message.error(err);
                        });
                } else {
                    return false;
                }
            });
        },
        // 获取种羊档案列表
        getSeedSheepList() {
            reqSeedSheepList({
                pageNum: this.page,
                pageSize,
                gender: this.sheepType,
                searchStr: this.searchStr
            })
                .then(res => {
                    this.tableData = res.content;
                    this.totalPages = res.totalPages;
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 羊类型切换
        shiftSheepType(sheepType) {
            if(sheepType == this.sheepType) return
            this.sheepType = sheepType
            this.getSeedSheepList()
        },

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

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