<template>
    <div class="ImmuneProce">
        <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="getImmuneProceList">查找</el-button>
                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click="add">新增</el-button>
                </el-col>
                <el-col :span="4">
                    <el-select v-model="formData.vaccineStyle" @change="S_change" placeholder="请选择">
                        <el-option
                            v-for="item in $store.state['vaccineStyle']"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        ></el-option>
                    </el-select>
                </el-col>
                <el-col :span="4">
                    <el-button-group>
                        <el-button
                            v-for="item in $store.state['sheepType']"
                            :key="item.value"
                            :type="target==item.value?'primary':''"
                            @click="onTargetType(item.value)"
                        >{{ item.label }}</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="80"></el-table-column>
                    <el-table-column prop="vaccineName" label="疫苗名称"></el-table-column>
                    <el-table-column prop="disease" label="疫病种类"></el-table-column>
                    <el-table-column prop="vaccineTime" label="免疫时间"></el-table-column>
                    <el-table-column label="免疫类型">
                        <template v-slot="scope">
                            <div v-for="item in $store.state['vaccineStyle']" :key="item.value">
                                <span
                                    v-if="item.value == scope.row['vaccineStyle']"
                                >{{ item.label }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="免疫剂量">
                        <template v-slot="scope">
                            <div>{{ scope.row['vaccineDosage'] + scope.row['unit']}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="bodyPart" label="注射部位"></el-table-column>
                    <el-table-column prop="remark" label="备注"></el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                            <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="40%">
            <el-row type="flex">
                <el-col :span="20">
                    <el-form
                        ref="addImmuneProce"
                        :model="formData"
                        :rules="rules"
                        label-width="140px"
                    >
                        <el-form-item label="免疫类型">
                            <el-radio-group v-model="formData.vaccineStyle">
                                <el-radio
                                    v-for="item in $store.state['vaccineStyle']"
                                    :key="item.value"
                                    :label="item.value"
                                >{{ item.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>

                        <el-form-item label="羊类型">
                            <el-checkbox-group v-model="sheepTypeArr">
                                <el-checkbox
                                    v-for="item in $store.state['sheepType']"
                                    :key="item.value"
                                    :label="item.value"
                                >{{ item.label }}</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>

                        <el-form-item label="免疫时间">
                            <el-input v-model="formData.vaccineTime"></el-input>
                        </el-form-item>

                        <el-form-item label="免疫剂量及单位">
                            <el-row type="flex" :gutter="20">
                                <el-col :span="10">
                                    <el-input v-model="formData.vaccineDosage"></el-input>
                                </el-col>
                                <el-col :span="10">
                                    <el-input v-model="formData.unit"></el-input>
                                </el-col>
                            </el-row>
                        </el-form-item>

                        <el-form-item label="注射部位">
                            <el-input v-model="formData.bodyPart"></el-input>
                        </el-form-item>

                        <el-form-item label="备注">
                            <el-input type="textarea" v-model="formData.remark"></el-input>
                        </el-form-item>

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

<script>
import {
    reqImmuneProceList,
    reqAddOrUpdateImmuneProce,
    reqDelImmuneProce
} from "@/api/antiepidemic.js";

const pageSize = 10;
const rules = {};

export default {
    name: "ImmuneProce",
    data() {
        return {
            search: "",
            pageNum: 1,
            tableData: [],
            totalPages: 0,
            isShow: false,
            target: this.$store.state["sheepType"][0].value,  // 按钮绑定的
            sheepTypeArr: [this.$store.state["sheepType"][1].value],
            formData: {
                vaccineStyle: this.$store.state["vaccineStyle"][1].value,
                target: this.$store.state["sheepType"][0].value,
                vaccineName: "山羊口疮弱毒细胞冻干苗",
                disease: "山羊口疮",
                vaccineTime: "怀孕母羊分娩前20-30天和10-20天各注射1次",
                vaccineDosage: "2",
                unit: "ml",
                bodyPart: "注射部位",
                remark: "备注"
            },
            rules
        };
    },
    created() {
        // 免疫程序列表
        this.getImmuneProceList();
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.formData.target = this.sheepTypeArr.reduce(
                        (total, num) => {
                            return total + num;
                        }
                    );
                    reqAddOrUpdateImmuneProce(this.formData)
                        .then(res => {
                            // 免疫程序列表
                            this.getImmuneProceList();
                            this.$message.success("成功！");
                            // this.isShow = false
                        })
                        .catch(err => {
                            console.log(err);
                            this.$message.error("失败！");
                        });
                } else {
                    return false;
                }
            });
        },
        // 点击按钮组
        onTargetType(target) {
            this.target = target;
            this.sheepTypeArr = [target];
            this.getImmuneProceList();
        },
        // 下拉选择框改变
        S_change() {
            this.getImmuneProceList();
        },
        // 免疫程序列表
        getImmuneProceList() {
            reqImmuneProceList({
                searchStr: this.search,
                pageSize,
                pageNum: this.pageNum,
                vaccineStyle: this.formData.vaccineStyle,
                target: this.target
            })
                .then(res => {
                    this.tableData = res.content;
                    this.totalPages = res.totalPages;
                })
                .catch(err => {
                    console.log(err);
                });
        },
        add() {
            this.isShow = true;
            this.formData.target = this.target
            this.sheepTypeArr = [this.target];
            // 有id是update 没id是add
            delete this.formData.id;
        },
        edit(row) {
            // 数据回填 
            console.log(row.target);
            this.formData = row;
            this.$store.state["sheepType"].forEach(item => {
                    if((row.target & item.value) == item.value) this.sheepTypeArr.push(item.value)
                }
            );
            this.isShow = true;
        },
        del(row) {
            reqDelImmuneProce(row.id)
                .then(res => {
                    // 免疫程序列表
                    this.getImmuneProceList();
                    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("删除失败！");
                });
        },
        pageChange(p) {
            console.log(p);
            this.pageNum = p;
            // 免疫程序列表
            this.getImmuneProceList();
        }
    }
};
</script>

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