<template>
    <div class="ImmunePlan">
        <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="getImmunePlanList">查找</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
                            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="batchNo" label="批次号"></el-table-column>
                    <el-table-column prop="performTime" label="执行时间"></el-table-column>
                    <el-table-column prop="number" label="免疫数量(只)"></el-table-column>
                    <el-table-column prop="medicineName" label="使用药物名"></el-table-column>
                    <el-table-column prop="method" 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 prop="remark" label="备注"></el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <el-button @click="onTimeChange(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="addImmunePlan"
                        :model="formData"
                        :rules="rules"
                        label-width="140px"
                    >
                        <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.batchNo"></el-input>
                        </el-form-item>

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

                        <el-form-item label="免疫数量(只)">
                            <el-input v-model="formData.number"></el-input>
                        </el-form-item>

                        <el-form-item label="使用药物">
                            <el-select v-model="formData.medicineId" filterable placeholder="请选择">
                                <el-option
                                    v-for="item in $store.state['drugBasicsList']"
                                    :key="item.id"
                                    :label="`${item.name}`"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="使用方法">
                            <el-input v-model="formData.method"></el-input>
                        </el-form-item>

                        <el-form-item label="操作人">
                            <el-select v-model="formData.opeUser" 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-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('addImmunePlan')">保 存</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-dialog>

        <el-dialog title="时间变更" :visible.sync="isShowTime" width="40%">
            <el-row type="flex">
                <el-col :span="20">
                    <el-form
                        ref="timeChange"
                        :model="formData"
                        :rules="rules"
                        label-width="140px"
                    >
                        <el-form-item label="执行时间">
                            <el-date-picker
                                v-model="formData.performTime"
                                type="date"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择日期"
                            ></el-date-picker>
                        </el-form-item>

                        <el-form-item>
                            <el-button @click="isShowTime=false">取 消</el-button>
                            <el-button type="primary" @click="submitFormTimeChange('timeChange')">保 存</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-dialog>


    </div>
</template>

<script>
import {
    reqImmunePlanList,
    reqAddOrUpdateImmunePlan,
    reqAdjustImmunePlan
} from "@/api/antiepidemic.js";

const pageSize = 10;
const rules = {};

export default {
    name: "ImmunePlan",
    data() {
        return {
            search: "",
            pageNum: 1,
            tableData: [],
            totalPages: 0,
            isShow: false,
            isShowTime: false,
            target: this.$store.state["sheepType"][2].value,  // 按钮绑定的
            sheepTypeArr: [this.$store.state["sheepType"][1].value],
            formData: {
                target: this.$store.state["sheepType"][0].value,
                batchNo: "1922",
                performTime: this.$moment().format('YYYY-MM-DD HH:mm:ss'),
                number: "200",
                medicineId: 1,
                method: "羊皮下注射",
                opeUser: this.$store.state['workerList'][0].id,
                remark: "备注"
            },
            timeForm: {},
            rules
        };
    },
    created() {
        // 免疫程序列表
        this.getImmunePlanList();
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.formData.target = this.sheepTypeArr.reduce(
                        (total, num) => {
                            return total + num;
                        }
                    );
                    reqAddOrUpdateImmunePlan(this.formData)
                        .then(res => {
                            // 免疫程序列表
                            this.getImmunePlanList();
                            this.$message.success("成功！");
                            // this.isShow = false
                        })
                        .catch(err => {
                            console.log(err);
                            this.$message.error("失败！");
                        });
                } else {
                    return false;
                }
            });
        },
        submitFormTimeChange(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    reqAdjustImmunePlan(this.timeForm)
                        .then(res => {
                            // 免疫程序列表
                            this.getImmunePlanList();
                            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.getImmunePlanList();
        },
        // 下拉选择框改变
        S_change() {
            this.getImmunePlanList();
        },
        // 免疫程序列表
        getImmunePlanList() {
            reqImmunePlanList({
                searchStr: this.search,
                pageSize,
                pageNum: this.pageNum,
                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;
        },
        onTimeChange(row) {
            this.timeForm.id = row.id
            this.timeForm.targetDate = row.performTime
            this.isShowTime = true
        },
        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) {
        //     reqDelImmunePlan(row.id)
        //         .then(res => {
        //             // 免疫程序列表
        //             this.getImmunePlanList();
        //             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.getImmunePlanList();
        }
    }
};
</script>

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