<template>
    <!-- 弹出框表单 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="200px">
            <el-row>
<!--
              <Field :span="20" label="是否启用该服务" prop="used" v-model="form.used" type="radio" :enumData="dict.used" placeholder="请选择是否启用该服务"/>
-->

              <Field :span="20" label="启用该服务">
                <el-switch
                    v-model="form.used"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-value="1"
                    inactive-value="0">
                </el-switch>
              </Field>


              <Field :span="20" label="监测周期配置">
                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.monday"
                >周一</el-checkbox>

                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.tuesday"
                >周二</el-checkbox>

                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.wednesday"
                >周三</el-checkbox>

                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.thursday"
                >周四</el-checkbox>

                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.friday"
                >周五</el-checkbox>
                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.saturday"
                >周六</el-checkbox>

                <el-checkbox
                    true-label="1"
                    false-label="0"
                    v-model="form.sunday"
                >周日</el-checkbox>
              </Field>
                            <Field :span="20" label="单日服务最大人数设置" prop="maxServicePersonNum" v-model="form.maxServicePersonNum" placeholder="请输入单日服务最大人数设置"/>
                            <Field :span="20" label="服务阈值设置，单位分钟" prop="serviceThreshold" v-model="form.serviceThreshold" placeholder="请输入服务阈值设置，单位分钟"/>
                            <Field :span="20" label="累计服务频次预警" prop="alertServiceNum" v-model="form.alertServiceNum" placeholder="请输入累计服务频次预警"/>
                            <Field :span="20" label="消息发送对象" prop="msgRecipients" v-model="form.msgRecipients" type="checkbox" :enumData="dict.msgRecipients" />
                            <Field :span="20" 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-time-select
                          placeholder="起始时间"
                          v-model="scope.row.serviceTimeStart"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          format="HH:mm"
                          :picker-options="{
                                        start: '08:00',
                                        step: '00:30',
                                        end: '18:00'
                                      }">
                      </el-time-select>
                      <!--                                    <el-input v-model="scope.row.serviceTimeStart" placeholder="请输入时间段时候开始时间" />-->
                    </template>
                  </el-table-column>
                  <el-table-column label="结束时间" prop="serviceTimeEnd">
                    <template slot-scope="scope">
                      <el-time-select
                          placeholder="结束时间"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          v-model="scope.row.serviceTimeEnd"
                          :picker-options="{
                                        start: '08:00',
                                        step: '00:30',
                                        end: '18:00'
                                      }">
                      </el-time-select>
                    </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>

        </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";
    import moment from "moment/moment";
    export default {
        mixins: [form],
        components: {
            dialogShow ,
        },
        data() {
            return {
                // 子表选中数据
                checkedCareConfigTimes: [],
                // 主动关怀时间段表格数据
                careConfigTimesList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "主动关怀配置",
                // 是否显示弹出层
                open: false,
               toArrays: ["msgRecipients"],
                toString:[
                    "used",
                    "monday",
                    "tuesday",
                    "wednesday",
                    "thursday",
                    "friday",
                    "saturday",
                    "sunday",
                ],
                // 表单校验
                rules: {

                    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) {
                  data.entity.careConfigTimesList.map(i=>{
                    i.serviceTimeStart = moment(i.serviceTimeStart).format('HH:mm')
                    i.serviceTimeEnd = moment(i.serviceTimeEnd).format('HH:mm')
                  })
                    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;
            },

          beforeSubmit(data) {
            data.careConfigTimesList.map(i=>{
              const serviceTimeStart = moment(i.serviceTimeStart, 'HH:mm');
              const serviceTimeEnd = moment(i.serviceTimeEnd, 'HH:mm');
              i.serviceTimeStart =  moment().set({hour: serviceTimeStart.hour(), minute: serviceTimeStart.minute()}).format('yyyy-MM-DD HH:mm:ss');
              i.serviceTimeEnd = moment().set({hour: serviceTimeEnd.hour(), minute: serviceTimeEnd.minute()}).format('yyyy-MM-DD HH:mm:ss');
            })

            if (Array.isArray(data["msgRecipients"])) {
              data.msgRecipients = data.msgRecipients.filter(Boolean).join();
            }
            return data
          },

            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>
