<template>
    <div class="scoringRules" v-loading="loadingImport">
        <div class="scoringRulesBtn">
            <div class="collectSeach">
                <div class="exceed">
                    <span class="exceedSpan">业务属性：</span>
                    <el-select v-model="businessStr" placeholder="请选择业务属性" clearable >
                        <el-option
                        v-for="item in binSectionoptions"
                        :key="item.id"
                        :label="item.sectionName"
                        :value="item.id">
                        </el-option>
                    </el-select>
                </div>
                <div class="exceed">
                    <span class="exceedSpan" style="width:100px">规则名称：</span>
                    <el-input v-model="indicatorStr" placeholder="请输入规则名称"></el-input>
                </div>
                <seachs @handleSeach="getSeachData" @handleRest="reset"></seachs>
            </div>
            <div class="PeriodBtn">
                <btns
                :showImport="false"
                :showExport="false"
                :showAdd="$utils.havePurview('scoringRules:oneLevel:add')"
                :showSave="$utils.havePurview('scoringRules:oneLevel:save')"
                :showDelete="$utils.havePurview('scoringRules:oneLevel:delete')"
                :disSave="changeDateSelect.length === 0 || changeDateSelect.length>1"
                :disDelete="changeDateSelect.length === 0"
                :disImport="true"
                :disExport="true"
                @handleAdd="handleAdd"
                @handleEdit="handleEdit"
                @handleDelete="handleDelete"
                ></btns>
            </div>
            <div class="scoringRulesTableData">
                <el-table :data="scoringRulesData" style="width: 100%" @select="rowClick" @select-all="allSelect" @row-dblclick="scoringRuleDetail">
                    <el-table-column type="selection" label="操作" align="center" />
                    <el-table-column label="规则名称" prop="scoreRuleName" />
                    <el-table-column label="业务属性" prop="businessName"  width="200" />
                    <el-table-column label="指标名称" width="300">
                        <template #default="scope">
                            <span>{{scope.row.indicatorNames.join(',')}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="描述">
                        <template #default="scope">
                            <el-tooltip
                                class="box-item"
                                effect="customized"
                                :content="scope.row.describe"
                                placement="top"
                            >
                                <span class="tooltipCC">{{scope.row.describe}}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100">
                        <template #default="scope">
                            <p class="indicitem" @click="scoringRuleDetail(scope.row)">详情</p>
                        </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>
            <el-dialog :title="title" v-model="dialogVisible" width="600px" :close-on-click-modal="false">
                <div class="periodFrom">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" :validate-on-rule-change="false">
                        <el-form-item label="规则名称" prop="ruleName">
                            <el-input v-model="ruleForm.ruleName" placeholder="请输入规则名称"></el-input>
                        </el-form-item>
                        <el-form-item label="业务属性" prop="binSection">
                            <el-select v-model="ruleForm.binSection" placeholder="请选择业务属性" :disabled="isSave" @change="changeBinstation">
                                <el-option
                                v-for="item in binSectionoptions"
                                :key="item.id"
                                :label="item.sectionName"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="指标名称" prop="indicatorId">
                             <!-- multiple collapse-tags -->
                            <el-select v-model="ruleForm.indicatorId" placeholder="请选择指标名称" :disabled="isSave">
                                <el-option
                                v-for="item in indicItemoptions"
                                :key="item.indicatorCode"
                                :label="item.indicatorName"
                                :value="item.indicatorCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="描述" prop="desc">
                            <el-input v-model="ruleForm.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="saveevaluscoringruleMsg('ruleForm')">确 定</el-button>
                    </span>
                </template>
            </el-dialog>
            <scoring-rules-detail-page ref="rulesDetail"></scoring-rules-detail-page>
        </div>
    </div>
</template>

<script>
import scoringRulesDetailPage from './scoringRulesDetailPage.vue'
import btns from '../elbuttonS.vue'
import seachs from '../seachGroup.vue'
import {apiGetevaluationscoringruleList, apiGetindicatorListAll, apiGetbinsectionList, apiGetevaluationscoringSaveList, apiGetevaluationruleRemove} from '../../api/api'
export default {
    // 
  components: { scoringRulesDetailPage,btns,seachs },
    data() {
        return {
            dialogVisible: false,
            title: '',
            indicItemoptions: [],
            binSectionoptions: [],
            indicatorItems: [],
            indicatorStr: '',
            businessStr: '',
            rowdataMsg: {},
            changeDateSelect: [],
            isSave: false,
            ruleForm: {
                ruleName: '',
                binSection: '',
                indicatorId: '',
                desc: ''
            },
            rules: {
                ruleName: [
                    { required: true, message: '请输入规则名称', trigger: 'blur' }
                ],
                binSection: [
                    { required: true, message: '请选择业务属性', trigger: 'change' }
                ],
                indicatorId: [
                    { required: true, message: '请选择指标名称', trigger: 'change' }
                ],
                desc: [
                    { required: true, message: '请输入描述', trigger: 'blur' }
                ]
            },
            loadingImport: false,
            scoringRulesData:[],
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            }
        }
    },
    created() {
        this.getEvalscoringData()
        this.getDataDictionary()
        this.getEvaluationData()
    },
    methods:{
        // 查询得分规则数据
        getEvalscoringData() {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize,
                scoreRuleName: that.indicatorStr,
                businessPhase: that.businessStr
            }
            apiGetevaluationscoringruleList(params).then(datas =>{
                if (datas && datas.data) {
                    that.scoringRulesData = datas.data.records
                    that.page.total = datas.data.total
                }
            })
        },
        // 查询模块
        getDataDictionary() {
            let that = this
            apiGetbinsectionList().then(datas =>{
                if (datas && datas.data) {
                    that.binSectionoptions = datas.data
                }
            })
        },
        changeBinstation(val) {
            this.getEvaluationData(val)
        },
        // 查询指标数据
        getEvaluationData(val) {
            let that = this
            let params = {
                binSection: val
            }
            apiGetindicatorListAll(params).then(datas =>{
                if (datas && datas.data) {
                    that.indicItemoptions = datas.data
                }
            })
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增考评得分规则'
            this.$nextTick(() =>{
                this.ruleForm = {
                    ruleName: '',
                    binSection: '',
                    indicatorId: '',
                    desc: ''
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改考评得分规则'
            let evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                ruleName: evalradio.scoreRuleName,
                binSection: evalradio.businessPhase,
                indicatorId: evalradio.indicatorId,
                desc: evalradio.describe
            }
            this.getEvaluationData(evalradio.businessPhase)
        },
        saveevaluscoringruleMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let params = {
                scoreRuleName: that.ruleForm.ruleName,
                businessPhase: that.ruleForm.binSection,
                indicatorId: that.ruleForm.indicatorId,
                describe: that.ruleForm.desc
            }
            if (that.isSave) {
                let evalradio = this.changeDateSelect[0]
                params.id = evalradio.id
            }
            apiGetevaluationscoringSaveList(params).then(datas =>{
                if (!that.isSave) {
                    that.$message({
                        message: '考评得分规则新增成功',
                        type: 'success'
                    });
                } else {
                    that.$message({
                        message: '考评得分规则修改成功',
                        type: 'success'
                    });
                }
                that.dialogVisible = false
                that.changeDateSelect = []
                that.getEvalscoringData()
            })
        },
        //删除指标数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiGetevaluationruleRemove(paramsArr.join(',')).then(datas =>{
                    if (datas) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        that.changeDateSelect = []
                        that.getEvalscoringData()
                    }
                })
            })
        },  
        showCounter() {
            this.$refs.counter.init(this.ruleForm.optionName)
        },
        counterStr(val) {
            this.ruleForm.optionName = val
        },
        scoringRuleDetail(row) {
            this.$refs.rulesDetail.init(row)
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getEvalscoringData()
        },
        reset() {
            this.page.currentPage = 1
            this.indicatorStr = ''
            this.businessStr = ''
            this.getEvalscoringData()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getEvalscoringData()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getEvalscoringData()
        },
        rowClick(selection, row) {
            this.changeDateSelect = selection
        }
    }
}
</script>

<style lang="less">
.scoringRules{
    .scoringRulesBtn{
        .collectSeach{
            display: flex;
            padding: 24px 20px;
            border-bottom: 1px solid#D6DBEA;
                .exceed{
                    display: flex;
                    .exceedSpan{
                        width: 75px;
                        height: 12px;
                        font-size: 14px;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        color: #8991B0;
                        line-height: 12px;
                        margin-top: 14px;
                    }
                    .el-input{
                        margin-right:10px;
                        .el-input__inner{
                            height:40px;
                        }
                        .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;
            // width:100px;
            padding: 0 30px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
    }
    .scoringRulesTableData{
        .el-table{
            .el-table__body-wrapper{
                height: 60vh !important;
            }
            .el-input__inner{
                height: 30px !important;
            }
            .el-radio__label{
                display: none;
            }
            .tooltipCC{
                width: 400px;
                display: inline-block;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            .indicitem{
                color: #409EFF;
                font-size: 12px;
                margin-right: 20px;
                cursor:pointer;
                &:hover{
                    text-decoration: underline;
                }
            }
        }
        .el-pagination{
            margin-top: 20px;
            text-align: end;
            position: relative;
        }
    }
    .el-overlay{
        .el-dialog{
            .el-dialog__body{
                padding: 30px 60px 30px 20px;
                .periodFrom{
                    .counterSx{
                        width: 420px;
                        height: 27px;
                        border: 1px solid #dcdfe6;
                        border-radius: 5px;
                        cursor: pointer;
                    }
                    .el-select, .el-input{
                        width: 100%;
                    }
                }
            }
        }
    }
}
</style>