<template>
    <div class="ruleItem">
        <el-dialog title="规则详情" v-model="editEvaluaVisible" width="70vw" :close-on-click-modal="false">
            <div class="ruleItemAll">
                <div class="ruleItemBtn">
                    <div class="collectSeach">
                        <div class="exceed">
                            <span class="indicatorSpan">指标：</span>
                            <el-input v-model="indicItem" placeholder="请输入指标名称"></el-input>
                        </div>
                        <seachs @handleSeach="getSeachData" @handleRest="resetSeach"></seachs>
                    </div>
                    <div class="PeriodBtn">
                        <el-row :gutter="10" style="padding-left: 10px;">
                            <el-col :span="1.5" v-if="$utils.havePurview('evalStart:twoLevel:add')">
                                <el-button
                                type="primary"
                                size="mini"
                                @click="handleAdd"
                                >
                                <img :src="addW" style="margin-right: 10px">
                                <span>新增</span>
                                </el-button>
                            </el-col>
                            <el-col :span="1.5" v-if="$utils.havePurview('evalStart:twoLevel:save')">
                                <el-button
                                type="success"
                                size="mini"
                                @click="handleEdit"
                                :disabled="changeDateSelect.length === 0 || changeDateSelect.length>1"
                                >
                                <img :src="editW" style="margin-right: 10px">
                                <span>修改</span>
                                </el-button>
                            </el-col>
                            <el-col :span="1.5" v-if="$utils.havePurview('evalStart:twoLevel:delete')">
                                <el-button
                                type="danger"
                                size="mini"
                                :disabled="changeDateSelect.length === 0"
                                @click="handleDelete"
                                >
                                <img :src="deleteW" style="margin-right: 10px">
                                <span>删除</span>
                                </el-button>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="ruleItemTableData">
                        <el-table :data="ruleDetailData" style="width: 100%" @select="rowClick" @select-all="rowClick">
                            <el-table-column type="selection" label="操作" align="center"></el-table-column>
                            <el-table-column type="index" label="序号" align="center"></el-table-column>
                            <el-table-column label="指标名称" prop="indicatorName" />
                            <el-table-column label="创建者" prop="createName" />
                            <el-table-column label="创建时间" prop="createTime" />
                            <el-table-column label="描述">
                                <template #default="scope">
                                    <span>{{scope.row.des}}</span>
                                </template>
                            </el-table-column>
                        </el-table>
                        <!-- <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="page.currentPage"
                            :page-size="page.pagesize"
                            layout="total, prev, pager, next, jumper"
                            :total="page.total">
                        </el-pagination> -->
                    </div>
                    <div class="onlyDialog">
                        <el-dialog :title="title" v-model="dialogVisible" width="600px" :close-on-click-modal="false">
                            <div class="periodFrom">
                                <el-form :model="itemForm" :rules="rules" ref="itemForm" label-width="100px" :validate-on-rule-change="false">
                                    <el-form-item label="指标名称" prop="indicator">
                                        <el-select v-model="itemForm.indicator" placeholder="请选择指标名称" filterable>
                                            <el-option
                                            v-for="item in indicItemoptions"
                                            :key="item.id"
                                            :label="item.indicatorName"
                                            :value="item.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="最低分" prop="minScore">
                                        <el-input-number v-model="itemForm.minScore" :precision="2" :step="0.1" :min="0" />
                                    </el-form-item>
                                    <el-form-item label="最高分" prop="maxScore">
                                        <el-input-number v-model="itemForm.maxScore" :precision="2" :step="0.1" :min="0" />
                                    </el-form-item>
                                    <el-form-item label="基准分" prop="standardScore">
                                        <el-input-number v-model="itemForm.standardScore" :precision="2" :step="0.1" :min="0" />
                                    </el-form-item>
                                    <el-form-item label="指标顺序" prop="orderNum">
                                        <el-input-number v-model="itemForm.orderNum" :min="1" />
                                    </el-form-item>
                                    <el-form-item label="描述">
                                        <el-input v-model="itemForm.desc" :rows="5" type="textarea" placeholder="请输入描述"></el-input>
                                    </el-form-item>
                                </el-form>
                            </div>
                            <template #footer>
                                <span class="dialog-footer">
                                    <el-button @click="dialogVisible = false">取 消</el-button>
                                    <el-button type="primary" @click="saveruleItemMsg('itemForm')">确 定</el-button>
                                </span>
                            </template>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import addW from '../../assets/btnIcon/addW.png'
import editW from '../../assets/btnIcon/editW.png'
import deleteW from '../../assets/btnIcon/deleteW.png'
import seachs from '../seachGroup.vue'
import {apiGetevaluateRuleinfoList, apiGetevaluateRuleInfoSaveList, apiGetevaluateRuleinfodeleteList, apiGetindicatorListAll} from '../../api/api'
export default {
    components: {
        seachs
    },
    data() {
        return {
            addW: addW,
            editW: editW,
            deleteW: deleteW,
            editEvaluaVisible: false,
            isSave: false,
            changeDateSelect: [],
            ruleDetailData: [],
            indicItemoptions: [],
            indicItem: '',
            evaluationIndexData:[],
            itemForm: {
                indicator: '',
                minScore: 0,
                maxScore: 0,
                standardScore: 0,
                orderNum: 1,
                desc: ''
            },
            rules: {
                indicator: [
                    { required: true, message: '请选择指标名称', trigger: 'change' }
                ]
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            rowevalradio: {},
            dialogVisible: false
        }
    },
    methods: {
        init(row) {
            this.editEvaluaVisible = true
            this.ruleDetailData = []
            this.indicItemoptions = []
            this.rowevalradio = row
            this.getruleDetailData(row)
            this.getEvaluationData(row)
        },
        // 查询规则详情数据
        getruleDetailData(row) {
            let that = this
            let params = {
                evaluateRuleId: row.id,
                indicatorName: that.indicItem
            }
            apiGetevaluateRuleinfoList(params).then(datas =>{
                if (datas && datas.data) {
                    that.ruleDetailData = datas.data
                    that.changeDateSelect = []
                }
            })
        },
        // 查询指标数据
        getEvaluationData(row) {
            let that = this
            let params = {
                binSection: row.binSection,
                binStage: row.binStage,
                evaluationCycle: that.rowevalradio.checkCycle
            }
            apiGetindicatorListAll(params).then(datas =>{
                if (datas && datas.data) {
                    that.indicItemoptions = datas.data
                }
            })
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增规则详情'
            this.$nextTick(() =>{
                this.itemForm = {
                    indicator: '',
                    minScore: 0,
                    maxScore: 0,
                    standardScore: 0,
                    orderNum: 1,
                    desc: ''
                }
                // this.$refs['itemForm'].resetFields()
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改规则详情'
            this.evalradio = this.changeDateSelect[0]
            this.itemForm = {
                indicator: this.evalradio.indicatorId,
                minScore: this.evalradio.minScore,
                maxScore: this.evalradio.maxScore,
                standardScore: this.evalradio.standardScore,
                orderNum: this.evalradio.orderNum,
                desc: this.evalradio.des
            }
        },
        saveruleItemMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let userMes = JSON.parse(window.sessionStorage.getItem('user'))
            let params = {
                indicatorId: that.itemForm.indicator,
                minScore: that.itemForm.minScore,
                maxScore: that.itemForm.maxScore,
                standardScore: that.itemForm.standardScore,
                orderNum: that.itemForm.orderNum,
                des: that.itemForm.desc,
                createBy: userMes.id,
                createName: userMes.name
            }
            if (that.isSave) {
                params.id = that.evalradio.id
            } else {
                params.evaluateRuleId = that.rowevalradio.id
            }
            apiGetevaluateRuleInfoSaveList(params).then(datas =>{
                if (datas) {
                    if (datas.success) {
                        if (!that.isSave) {
                            that.$message({
                                message: '规则详情新增成功',
                                type: 'success'
                            });
                        } else {
                            that.$message({
                                message: '规则详情修改成功',
                                type: 'success'
                            });
                        }
                        that.dialogVisible = false
                        that.changeDateSelect = []
                        that.getruleDetailData(that.rowevalradio)
                    } else {
                        that.$message({
                            message: datas.message,
                            type: 'error'
                        })
                    }
                }
                
            })
        },
        //删除指标数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiGetevaluateRuleinfodeleteList(paramsArr.join(',')).then(datas =>{
                    that.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    that.changeDateSelect = []
                    that.getruleDetailData(that.rowevalradio)
                })
            })
        },        
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getruleDetailData(this.rowevalradio)
        },
        resetSeach() {
            this.page.currentPage = 1
            this.indicItem = ''
            this.getruleDetailData(this.rowevalradio)
        },
        handleSizeChange(val){
            this.page.pagesize = val
        },
        handleCurrentChange(val){
            this.page.currentPage =val
        },
    }
}
</script>

<style lang="less">
.ruleItem{
    .el-overlay{
        .el-dialog{
            margin-top: 5vh !important;
            .el-dialog__body{
                padding: 0 20px 30px 20px !important;
                .ruleItemAll{
                    .el-select, .el-input{
                        width: 100%;
                    }
                    .ruleItemBtn{
                        padding: 10px 20px 0 20px;
                        .collectSeach{
                            display: flex;
                            padding: 24px 20px;
                            border-bottom: 1px solid#D6DBEA;
                                .exceed{
                                    display: flex;
                                    .indicatorSpan{
                                        width: 100px;
                                        height: 12px;
                                        font-size: 14px;
                                        font-family: Microsoft YaHei;
                                        font-weight: 400;
                                        color: #8991B0;
                                        line-height: 12px;
                                        margin-top: 10px;
                                    }
                                    .el-input{
                                        margin-right:10px;
                                        .el-input__inner{
                                            height:30px;
                                        }
                                        .el-input__suffix{
                                            .el-select__caret{
                                                line-height:30px;
                                            }
                                        }
                                    }
                                }
                                .el-select{
                                    margin-right:10px;
                                    .el-input__inner{
                                        height:30px;
                                    }
                                    .el-input__suffix{
                                        .el-select__caret{
                                            line-height:30px;
                                        }
                                    }
                                }
                        }
                        span{
                            font-size:14px;
                        }
                        .PeriodBtn{
                            display: flex;
                            justify-content: flex-end;
                            padding: 20px 0;
                            .el-button{
                                height: 30px;
                                padding: 0 30px ;
                                span{
                                    margin:0;
                                }
                            }
                            .is-disabled{
                                opacity: 0.5;
                            }
                        }
                    }
                    .ruleItemTableData{
                        .el-table{
                            .el-table__body-wrapper{
                                height: 55vh !important;
                            }
                            .el-input__inner{
                                height: 30px !important;
                            }
                            .el-radio__label{
                                display: none;
                            }
                        }
                        .el-pagination{
                            margin-top: 20px;
                            text-align: end;
                            position: relative;
                        }
                    }
                    .onlyDialog{
                        .el-overlay{
                            .el-dialog{
                                margin-top: 10vh !important;
                                .el-dialog__body{
                                    padding: 30px 60px 30px 20px !important;
                                }
                            }
                        }
                    }
                }
            }
            .el-dialog__footer{
                .dialog-footer{
                    display: flex;
                    justify-content: center;
                    .el-button{
                        width: 180px !important;
                        height: 40px !important;
                    }
                }
            }
        }
    }
}
</style>