<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>
                        <el-button type="primary" style="margin-left: 10px;" @click="getSeachData">搜索</el-button>
                        <el-button style="margin-left: 10px;" @click="resetSeach">重置</el-button>
                    </div>
                    <div class="PeriodBtn">
                        <el-row :gutter="10" style="padding-left: 10px;">
                            <el-col :span="1.5">
                                <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">
                                <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">
                                <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 label="指标名称" prop="indicatorName" />
                            <el-table-column label="创建者" prop="createBy" />
                            <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="请选择指标名称">
                                            <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="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 {apiGetevaluateRuleinfoList, apiGetevaluateRuleInfoSaveList, apiGetevaluateRuleinfodeleteList, apiGetindicatorListAll} from '../../api/api'
export default {
    data() {
        return {
            addW: addW,
            editW: editW,
            deleteW: deleteW,
            editEvaluaVisible: false,
            isSave: false,
            changeDateSelect: [],
            ruleDetailData: [],
            indicItemoptions: [],
            indicItem: '',
            evaluationIndexData:[],
            itemForm: {
                indicator: '',
                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, type) {
            let that = this
            let params = {
                evaluateRuleId: row.id
            }
            if (type) {
                params.indicatorId = 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
            }
            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: '',
                    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,
                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 params = {
                indicatorId: that.itemForm.indicator,
                orderNum: that.itemForm.orderNum,
                des: that.itemForm.desc
            }
            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, 'seach')
        },
        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: 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;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>