<template>
    <!-- 弹出框表单 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
                                            <Field label="站点Id" prop="siteId" v-model="form.siteId" placeholder="请输入站点Id"/>
                            <Field label="站点名称" prop="siteName" v-model="form.siteName" type="textarea" placeholder="请输入站点名称"/>
                            <Field label="是否启用该服务" prop="used" v-model="form.used" type="select" :enumData="dict.used" placeholder="请选择是否启用该服务"/>
                            <Field label="周一是否监测" prop="monday" v-model="form.monday" type="select" :enumData="dict.monday" placeholder="请选择周一是否监测"/>
                            <Field label="周二是否监测" prop="tuesday" v-model="form.tuesday" type="select" :enumData="dict.tuesday" placeholder="请选择周二是否监测"/>
                            <Field label="周三是否监测" prop="wednesday" v-model="form.wednesday" type="select" :enumData="dict.wednesday" placeholder="请选择周三是否监测"/>
                            <Field label="周四是否监测" prop="thursday" v-model="form.thursday" type="select" :enumData="dict.thursday" placeholder="请选择周四是否监测"/>
                            <Field label="周五是否监测" prop="friday" v-model="form.friday" type="select" :enumData="dict.friday" placeholder="请选择周五是否监测"/>
                            <Field label="周六是否监测" prop="saturday" v-model="form.saturday" type="select" :enumData="dict.saturday" placeholder="请选择周六是否监测"/>
                            <Field label="周日是否监测" prop="sunday" v-model="form.sunday" type="select" :enumData="dict.sunday" placeholder="请选择周日是否监测"/>
                            <Field label="单日服务最大人数设置" prop="maxServicePersonNum" v-model="form.maxServicePersonNum" placeholder="请输入单日服务最大人数设置"/>
                            <Field label="服务阈值设置，单位分钟" prop="serviceThreshold" v-model="form.serviceThreshold" placeholder="请输入服务阈值设置，单位分钟"/>
                            <Field label="累计服务频次预警" prop="alertServiceNum" v-model="form.alertServiceNum" placeholder="请输入累计服务频次预警"/>
                            <Field label="消息发送对象" prop="msgRecipients" v-model="form.msgRecipients" type="radio" :enumData="dict.msgRecipients" />
                            <Field label="备注" prop="remark" v-model="form.remark" type="textarea" placeholder="请输入备注"/>

            </el-row>
                <el-divider content-position="center">主动关怀时间段信息</el-divider>
                <br/>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCareConfigTimes">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCareConfigTimes">删除</el-button>
                    </el-col>
                </el-row>

                <el-table :data="careConfigTimesList" :row-class-name="rowCareConfigTimesIndex" @selection-change="handleCareConfigTimesSelectionChange" ref="careConfigTimes">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                            <el-table-column label="时间段时候开始时间" prop="serviceTimeStart">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.serviceTimeStart" placeholder="请输入时间段时候开始时间" />
                                </template>
                            </el-table-column>
                            <el-table-column label="时间段结束时间" prop="serviceTimeEnd">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.serviceTimeEnd" placeholder="请输入时间段结束时间" />
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" prop="remark">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.remark" placeholder="请输入备注" />
                                </template>
                            </el-table-column>
                            <el-table-column label="创建用户" prop="createUserId">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.createUserId" placeholder="请输入创建用户" />
                                </template>
                            </el-table-column>
                            <el-table-column label="创建时间" prop="createTime">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.createTime" placeholder="请输入创建时间" />
                                </template>
                            </el-table-column>
                            <el-table-column label="更新用户" prop="updateUserId">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.updateUserId" placeholder="请输入更新用户" />
                                </template>
                            </el-table-column>
                            <el-table-column label="更新时间" prop="updateTime">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.updateTime" placeholder="请输入更新时间" />
                                </template>
                            </el-table-column>
                </el-table>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" v-if="pageInfo.type !== 'view'" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    import dialogShow from "./dialogshow";
    export default {
        mixins: [form],
        components: {
            dialogShow ,
        },
        data() {
            return {
                // 子表选中数据
                checkedCareConfigTimes: [],
                // 主动关怀时间段表格数据
                careConfigTimesList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "主动关怀配置",
                // 是否显示弹出层
                open: false,
                toString:[
                    "used",
                    "monday",
                    "tuesday",
                    "wednesday",
                    "thursday",
                    "friday",
                    "saturday",
                    "sunday",
                ],
                // 表单校验
                rules: {
                    msgRecipients: [
                        {required: true,message: "请输入消息发送对象", trigger: "blur" },
                        {max: 255,message: "最多只能录入255个字符",trigger: "blur",},
                    ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {
            /** 主动关怀时间段序号 */
            rowCareConfigTimesIndex({ row, rowIndex }) {
                row.index = rowIndex + 1;
            },
            /** 主动关怀时间段添加按钮操作 */
            handleAddCareConfigTimes() {
                let obj = {};
                obj.serviceTimeStart = "";
                obj.serviceTimeEnd = "";
                obj.remark = "";
                obj.createUserId = "";
                obj.createTime = "";
                obj.updateUserId = "";
                obj.updateTime = "";
                this.careConfigTimesList.push(obj);
            },
            /** 主动关怀时间段删除按钮操作 */
            handleDeleteCareConfigTimes() {
                if (this.checkedCareConfigTimes.length == 0) {
                    this.$alert("请先选择要删除的主动关怀时间段数据", "提示", { confirmButtonText: "确定", });
                } else {
                    this.careConfigTimesList.splice(this.checkedCareConfigTimes[0].index - 1, 1);
                }
            },
            /** 单选框选中数据 */
            handleCareConfigTimesSelectionChange(selection) {
                if (selection.length > 1) {
                    this.$refs.careConfigTimes.clearSelection();
                    this.$refs.careConfigTimes.toggleRowSelection(selection.pop());
                } else {
                    this.checkedCareConfigTimes = selection;
                }
            },
            // 渲染前置处理
            beforeRender(data) {
                if(data.entity.careConfigTimesList) {
                    this.careConfigTimesList = data.entity.careConfigTimesList;
                }
                return data
            },
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="care/config/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改主动关怀配置";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "care/config/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增主动关怀配置";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="care/config/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "主动关怀配置详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    siteId : null,
                    siteName : "",
                    used : 0,
                    monday : 0,
                    tuesday : 0,
                    wednesday : 0,
                    thursday : 0,
                    friday : 0,
                    saturday : 0,
                    sunday : 0,
                    maxServicePersonNum : 0,
                    serviceThreshold : 0,
                    alertServiceNum : null,
                    msgRecipients : "",
                    remark : "",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
