<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                <el-button class="btn-check" size="medium" type="primary" @click="dialogCheck(1)">添加
                </el-button>
            </div>

            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                    tooltip-effect="dark" size="small" border style="width: 100%" :row-class-name="tableRowClassName">
                    <el-table-column prop="ruleName" label="规则名称">
                    </el-table-column>
                    <el-table-column prop="preCata1" label="1级路径">
                    </el-table-column>
                    <el-table-column prop="preCata2" label="2级路径">
                    </el-table-column>
                    <el-table-column prop="preCata3" label="3级路径">
                    </el-table-column>
                    <el-table-column prop="preCata4" label="4级路径">
                    </el-table-column>
                    <el-table-column prop="preCata5" label="5级路径">
                    </el-table-column>
                    <el-table-column prop="preCata6" label="6级路径">
                    </el-table-column>
                    <el-table-column prop="preCata7" label="7级路径">
                    </el-table-column>
                    <el-table-column prop="keyWords" label="投诉关键字" width="100">
                    </el-table-column>
                    <el-table-column prop="compalineCnt" label="投诉数量">
                    </el-table-column>
                    <el-table-column prop="opNo" label="配置工号">
                    </el-table-column>
                    <el-table-column prop="opName" label="配置姓名">
                        <template slot-scope="scope">
                            <span>{{$desensitization(scope.row.opName,1)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="opTime" label="配置时间" width="140">
                    </el-table-column>
                    <el-table-column prop="overTimeFlag" label="是否生效">
                        <template slot-scope="scope">
                            <span>{{scope.row.overTimeFlag == '1'?'是':'否'}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="alarmNum" label="已预警次数" width="100">
                    </el-table-column>
                    <el-table-column label="操作" width="160px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">查看</el-button>
                            <el-button size="mini" type="primary" @click="dialogCheck(3,scope.row)">修改</el-button>
                            <el-button size="mini" type="danger" @click="delLine(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                    :total="total">
                </el-pagination>
            </div>

            <el-dialog :title="titname + '规则配置'" :visible.sync="dialogStatus" width="70%" :close-on-press-escape="false"
                :before-close="closedia" :show-close="true" :destroy-on-close="true" :modal-append-to-body="false"
                :close-on-click-modal="false" :fullscreen="true">

                <el-form :model="infolist" ref="infolist" :rules="rules"
                    style="height: calc(100vh - 180px);overflow-y: scroll;">
                    <div class="info-line" v-if="disableStatus">
                        <el-form-item>
                            <span>配置工号</span>
                            <el-input v-model="infolist.opNo" placeholder="配置工号" disabled></el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>配置姓名</span>
                            <el-input v-model="infolist.opName" placeholder="" disabled></el-input>
                        </el-form-item>
                    </div>
                    <div class="info-line" v-if="disableStatus">
                        <el-form-item>
                            <span>配置时间</span>
                            <el-input v-model="infolist.opTime" placeholder="配置时间" disabled></el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>规则状态</span>
                            <el-input v-model="infolist.stsDesc" placeholder="规则状态" disabled></el-input>
                        </el-form-item>
                    </div>
                    <div class="info-line">
                        <el-form-item prop="ruleName">
                            <span>规则名称</span>
                            <el-input v-model="infolist.ruleName" placeholder="规则名称" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>有效期</span>
                            <el-date-picker v-model="infolist.validTime" type="date" placeholder="有效期"
                                :disabled="disableStatus" value-format="yyyy-MM-dd hh:mm:ss">
                            </el-date-picker>
                        </el-form-item>
                    </div>

                    <el-form-item class="info-line online">
                        <span>接收人</span>
                        <deptTree v-if="!disableStatus" :closeList="closeList" class="tree" @treeCheck="treeCheck">
                        </deptTree>
                        <div :class="!disableStatus?'tree treeUser':'tree treeUserb'">
                            <p v-for="(item,index) in treeList" :key="index">{{item.receiveName}}
                                <i @click="deletes(item,index)" class="el-icon-error"></i>
                            </p>
                        </div>
                    </el-form-item>

                    <div class="info-line">
                        <el-form-item>
                            <span>预警周期</span>
                            <el-select v-model="infolist.cycleCode" placeholder="预警周期" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in weekopt" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>1级路径</span>
                            <el-select v-model="infolist.preCata1" placeholder="1级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt1" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>2级路径</span>
                            <el-select v-model="infolist.preCata2" placeholder="2级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt2" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>3级路径</span>
                            <el-select v-model="infolist.preCata3" placeholder="3级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt3" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>4级路径</span>
                            <el-select v-model="infolist.preCata4" placeholder="4级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt4" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>5级路径</span>
                            <el-select v-model="infolist.preCata5" placeholder="5级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt5" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>6级路径</span>
                            <el-select v-model="infolist.preCata6" placeholder="6级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt6" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>7级路径</span>
                            <el-select v-model="infolist.preCata7" placeholder="7级路径" clearable
                                :disabled="disableStatus">
                                <el-option v-for="item in preCataopt7" :key="item.dataCode" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>投诉关键字</span>
                            <el-input v-model="infolist.keyWords" placeholder="投诉关键字请用 , 分隔" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>投诉数量</span>
                            <el-input v-model="infolist.compalineCnt" placeholder="投诉数量" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <!--   <el-form-item>
                            <span><i class="el-icon-question" @click="userbook = !userbook"></i></span>
                        </el-form-item> -->

                        <div v-if="" class="userbook">
                            <span>1.接收人按部门选择,可选择多个人</span>
                            <span>2.规则添加有效期，过期后不按照该规则进行稽核</span>
                            <span>3.投诉关键字可填写多个 , 分隔</span>
                        </div>
                    </div>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogCli(1)" type="primary" v-if="btnsts !== 2">确 定</el-button>
                    <el-button @click="dialogCli(2)" v-if="btnsts === 2">确 定</el-button>
                    <el-button @click="dialogCli(2)">取 消</el-button>
                </div>
            </el-dialog>

        </div>
    </fullscreen>
</template>
<script>
    import mySearch from '../../../components/search'
    import toolList from '../../../components/toolList'
    import deptTree from '../../../components/deptTreeUser.vue'

    export default {
        components: {
            mySearch,
            toolList,
            deptTree
        },
        data() {
            const ruleName = (rule, value, callback) => {
                if (!this.infolist.ruleName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    ruleName: [{
                        required: true,
                        trigger: 'blur',
                        validator: ruleName
                    }],
                },
                tooltit: '预警规则配置',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                infolist: {},
                typeOptions: [],
                titname: '',
                disableStatus: false,
                visiblec: [],
                visibleList: [],
                userInfo: {},
                searchList: [{
                        type: 'input',
                        tit: '规则名称',
                        value: '',
                        width: '48%'
                    },
                    {
                        type: 'input',
                        tit: '配置工号',
                        value: '',
                        width: '48%',
                    },
                ],
                params: {},
                btnsts: 1,
                options2: [],
                weekopt: [],
                preCataopt1: [],
                preCataopt2: [],
                preCataopt3: [],
                preCataopt4: [],
                preCataopt5: [],
                preCataopt6: [],
                preCataopt7: [],
                userbook: false,
                treeList: [],
                closeList: []
            }
        },
        methods: {
            tableRowClassName({
                row,
                rowIndex
            }) {
                if (row.overTimeFlag == 0) {
                    return 'warning-row';
                }else{
                    return 'success-row';
                }
            },
            deletes(val, index) {
                this.treeList.splice(index, 1)
                this.closeList = this.treeList
            },
            treeCheck(v) {
                this.treeList = v;
            },
            closedia() {
                this.infolist = {};
                this.treeList = [];
                this.dialogStatus = false;
            },
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.ruleName = v[0] : '';
                v[1] ? this.params.opName = v[1] : '';
                this.getList(this.params, this.pageSize);
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.$http({
                    url: "/market/calarm/queryPage",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: v,
                }).then((res) => {
                    this.tableData = res.data.data;
                    this.total = res.data.totalRecord;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //查看 修改 新增
            dialogCheck(v, n) {
                this.dialogStatus = true;
                this.disableStatus = false;
                this.btnsts = v;
                this.infolist = {};
                if (v === 1) {
                    this.titname = '新建';
                    this.getUser();
                    return
                } else if (v === 2) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 3) {
                    this.titname = '修改';
                }
                this.infolist = Object.assign({}, n); //拷贝
                this.infolist.opName = this.$desensitization(this.infolist.opName, 1);
                this.$http({
                    url: "/market/calarm/query",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                        "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                    },
                    data: {
                        id: n.id
                    },
                }).then((res) => {
                    this.infolist = res.data;
                    this.treeList = res.data.sQaAlarmRuleReceiveList;
                });
            },
            dialogCli(v) {
                if (v === 2) {
                    this.infolist = {};
                    this.treeList = [];
                    this.dialogStatus = false;
                    return
                }
                // this.infolist.validTime = this.$formatDate(new Date(), "YYYY-MM-DD HH:mm:ss");
                this.infolist.opTime = this.$formatDate(new Date(), "YYYY-MM-DD HH:mm:ss");
                this.infolist.opName = this.userInfo.loginName;
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.receiveNo = 'admi';
                this.infolist.sts = '0';
                this.infolist.stsDesc = '待审核';
                this.infolist.sQaAlarmRuleReceiveList = this.treeList;
                if (this.titname === '新建') {
                    this.submitInfo("/market/calarm/add", v);
                } else if (this.titname === '修改') {
                    this.submitInfo("/market/calarm/update", v);
                }
            },
            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: this.infolist
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.infolist = {};
                                _this.treeList = [];
                                _this.dialogStatus = false;
                                _this.getList(this.params, this.pageSize);
                            }

                        });
                    }
                })
            },
            //删除
            delLine(v) {
                let _this = this;
                this.$confirm('即将删除此条数据, 是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    _this.$http({
                        url: "/market/calarm/del",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            id: v.id
                        }
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            _this.getList(this.params, this.pageSize);
                        }
                    });
                })
            },
            //处理所需数据
            verifall(v) {
                for (let i = 0; i < this.options2.length; i++) {
                    if (this.options2[i].dataCode === v) {
                        this.infolist.regionCode = v;
                        this.infolist.regionName = this.options2[i].dataName;
                    }
                }
            },
            //功能栏
            iconCli(v) {
                if (v === 1) {
                    this.getList(this.params, this.pageSize);
                }
                if (v === 2) {
                    this.fullscreen = !this.fullscreen
                }
            },
            //获取部门
            getDepot() {
                this.$http({
                    url: "/sysmgr/sysdept/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {},
                }).then((res) => {
                    this.typeOptions = res.data;
                });
            },
            getListsf() {
                this.$http({
                    url: "/market/cfgDataDict/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        dictCode: 'region'
                    },
                }).then((res) => {
                    this.options2 = res.data;
                });
            },
            getUser() {
                this.userInfo = JSON.parse(window.sessionStorage.userInfo);
                this.infolist = {
                    opNo: '',
                    opName: '',
                };
                this.infolist.opNo = this.userInfo.loginNo;
                this.infolist.opName = this.userInfo.loginName;
            },
            getlj() {
                this.$http({
                    url: "/sysmgr/cfgDataDicts/queryMap",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        dictCodePks: 'pre_cata_1,pre_cata_2,pre_cata_3,pre_cata_4,pre_cata_5,pre_cata_6,pre_cata_7,alarmCycle'
                    },
                }).then((res) => {
                    this.weekopt = res.data.body.alarmCycle;
                    this.preCataopt1 = res.data.body.pre_cata_1;
                    this.preCataopt2 = res.data.body.pre_cata_2;
                    this.preCataopt3 = res.data.body.pre_cata_3;
                    this.preCataopt4 = res.data.body.pre_cata_4;
                    this.preCataopt5 = res.data.body.pre_cata_5;
                    this.preCataopt6 = res.data.body.pre_cata_6;
                    this.preCataopt7 = res.data.body.pre_cata_7;
                });
            },
        },
        mounted() {
            this.getList(this.params, this.pageSize);
            this.getUser();
            this.getListsf();
            this.getlj();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .userbook {
        border: 2px dotted #00A0E9;
        width: 100% !important;
        padding: 0 10px;
        border-radius: 5px;

        span {
            display: inline-block;
            width: 100% !important;
            margin: 5px 0;
        }
    }

    .el-icon-question {
        font-size: 20px;
        color: #ddd !important;
        cursor: pointer;
    }

    .dialog-footer {
        padding-right: 20px;
        margin-top: 10px;
    }

    .tabbox {
        margin-top: 16px;
    }

    .pageBox {
        text-align: right;
        margin-top: 10px;
    }

    .info-line {
        width: 100%;
        display: block;
        padding-left: 20px;

        div {
            width: 50%;
            display: inline-block;
        }

        span {
            width: 80px;
            display: inline-block;
            text-align: left;

            i {
                color: red;
                display: inline-block;
                padding-right: 5px;
            }
        }

        .el-select,
        .el-input {
            width: calc(100% - 100px);
        }
    }

    .online {
        width: 100%;

        .el-select {
            width: calc(100% - 100px);
        }

        span {
            vertical-align: top;
        }

        .el-textarea {
            width: calc(100% - 100px);
        }

        .tree {
            width: calc(50% - 60px);
            display: inline-block;
            margin-right: 20px;
            height: 300px;
            overflow-y: scroll;

            .el-icon-error {
                float: right;
                font-size: 20px;
                margin-top: 9px;
                cursor: pointer;
            }
        }

        .treeUser {
            margin: 0;
            border: 1px solid #ddd;

            p {
                background: #f4f4f4;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }

        .treeUserb {
            width: calc(100% - 100px);
            border: 1px solid #ddd;
            background: #f4f4f4;
            border-radius: 3px;
            height: auto;
            overflow: hidden;

            p {
                display: inline-block;
                padding: 0 20px;
                margin-bottom: 5px;
            }
        }
    }
</style>
