<template>
    <div class="evaluationRule" v-loading="loadingImport">
        <div class="evaluationRuleBtn">
            <p class="titleSty">考评规则管理</p>
            <div class="collectSeach">
                <div class="exceed">
                    <span class="exceedSpan" style="margin-top: 3px;">模块名称：</span>
                    <el-input v-model="moduleStr" 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 style="display:flex;justify-content: space-between;padding: 10px;">
                <el-row :gutter="10" class="PeriodBtn">
                    <el-col :span="1.5">
                        <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        size="mini"
                        @click="handleAdd"
                        >新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="success"
                        plain
                        icon="Edit"
                        size="mini"
                        @click="handleEdit"
                        :disabled="changeDateSelect.length === 0 || changeDateSelect.length>1"
                        >修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        size="mini"
                        :disabled="changeDateSelect.length === 0"
                        @click="handleDelete"
                        >批量删除</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="info"
                        plain
                        icon="Upload"
                        size="mini"
                        disabled
                        @click="handleImport"
                        >导入</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            type="warning"
                            plain
                            icon="Download"
                            size="mini"
                            disabled
                        >导出</el-button>
                    </el-col>
                </el-row>
            </div>
            <div class="evaluationRuleTableData">
                <el-table :data="evaluationRuleData" border style="width: 100%" @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="业务板块" prop="binSection" />
                    <el-table-column label="业务阶段" prop="binStage" />
                    <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-column label="操作">
                        <template #default="scope">
                            <p class="indicitem" @click="editRuleDetail(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>
            <div class="indexdialog">
                <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="binSection">
                                <el-select v-model="ruleForm.binSection" placeholder="请选择业务模块">
                                    <el-option
                                    v-for="item in moduleData"
                                    :key="item.keyValue"
                                    :label="item.keyName"
                                    :value="item.keyName">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="业务阶段" prop="binStage">
                                <el-select v-model="ruleForm.binStage" placeholder="请选择业务阶段">
                                    <el-option
                                    v-for="item in stageData"
                                    :key="item.keyValue"
                                    :label="item.keyName"
                                    :value="item.keyName">
                                    </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="saveevaluationRuleMsg('ruleForm')">确 定</el-button>
                        </span>
                    </template>
                </el-dialog>
            </div>
            <rule-detail-page ref="ruleDetail"></rule-detail-page>
        </div>
        <import-dailog ref="importPage" @successImport="successImport" @importLoading="importLoading"></import-dailog>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import ruleDetailPage from './ruleDetailPage.vue'
import {apiGetevaluateRuleList, apiGetevaluateRuleSaveList, apiGetevaluateRuledeleteList, apiGetdatadictionaryList} from '../../api/api'
export default {
    components: { importDailog, ruleDetailPage },
    data() {
        return {
            dialogVisible: false,
            title: '',
            isSave: false,
            changeDateSelect: [],
            moduleData: [],
            stageData: [],
            moduleStr: '',
            evaluationRuleData:[],
            ruleForm: {
                binSection: '',
                binStage: '',
                desc: ''
            },
            rules: {
                binSection: [
                    { required: true, message: '请输入业务模块', trigger: 'blur' }
                ],
                binStage: [
                    { required: true, message: '请输入业务阶段', trigger: 'blur' }
                ],
                desc: [
                    { required: true, message: '请输入描述', trigger: 'blur' }
                ]
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            loadingImport: false,
        }
    },
    created() {
        this.getEvalRuleData()
        this.getDataDictionary('MK0001') //模块
        this.getDataDictionary('JD0001') //阶段
    },
    methods:{
        // 查询指标数据
        getEvalRuleData(type) {
            let that = this
            let params = {
                pageNum: this.page.currentPage,
                pageSize: this.page.pagesize,
            }
            if (type) {
                params.binSection = that.moduleStr
            }
            apiGetevaluateRuleList(params).then(datas =>{
                if (datas && datas.data) {
                    that.evaluationRuleData = datas.data.records
                    that.page.total = datas.data.total
                }
            })
        },
        // 查询规则模块和阶段数据
        getDataDictionary(val) {
            let that = this
            let params = {
                superKey: val
            }
            apiGetdatadictionaryList(params).then(datas =>{
                if (datas && datas.data) {
                    if (val === 'MK0001') {
                        that.moduleData = datas.data
                    } else {
                        that.stageData = datas.data
                    }
                }
            })
        },
        // 新增区域信息
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增考评规则'
            this.$nextTick(() =>{
                // this.$refs['ruleForm'].resetFields()
                this.ruleForm = {
                    binSection: '',
                    binStage: '',
                    desc: ''
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改考评规则'
            this.evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                binSection: this.evalradio.binSection,
                binStage: this.evalradio.binStage,
                desc: this.evalradio.des
            }
        },
        saveevaluationRuleMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditRuleData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditRuleData() {
            let that = this
            let params = {
                binSection: that.ruleForm.binSection,
                binStage: that.ruleForm.binStage,
                des: that.ruleForm.desc
            }
            if (that.isSave) {
                params.id = this.evalradio.id
            }
            apiGetevaluateRuleSaveList(params).then(datas =>{
                if (!that.isSave) {
                    that.$message({
                        message: '指标新增成功',
                        type: 'success'
                    });
                } else {
                    that.$message({
                        message: '指标修改成功',
                        type: 'success'
                    });
                }
                that.dialogVisible = false
                that.changeDateSelect = []
                that.getEvalRuleData()
            })
        },
        //删除指标数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                await apiGetevaluateRuledeleteList(paramsArr.join(',')).then(datas =>{})
                that.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                that.getEvalRuleData()
            })
        },        
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getEvalRuleData('seach')
        },
        resetSeach() {
            this.page.currentPage = 1
            this.moduleStr = ''
            this.getEvalRuleData()
        },
        successImport(val) {
            this.loadingImport = false
            this.getEvalRuleData()
        },
        importLoading(val) {
            this.loadingImport = true
        },
        handleSizeChange(val){
            this.page.pagesize = val
        },
        handleCurrentChange(val){
            this.page.currentPage =val
        },
        handleImport() {
            this.$refs.importPage.upload.title = "考评指标管理信息导入"
            this.$refs.importPage.upload.open = true
        },

        //指标项配置
        editRuleDetail(row) {
            this.$refs.ruleDetail.init(row)
        }
    }
}
</script>

<style lang="less">
.evaluationRule{
  .evaluationRuleBtn{
    //   background: #fff;
        padding: 10px 20px 0 20px;
        .collectSeach{
            display: flex;
            margin: 20px 20px 10px;
                .exceed{
                    display: flex;
                    .exceedSpan{
                        margin-top: 3px;
                        width: 100px;
                        display: inline-block;
                    }
                    .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;
                        }
                    }
                }
        }
        .titleSty{
            border-left: 4px solid #007aab;
            padding-left: 5px;
            font-size: 16px;
            font-weight: bold;
            font-family: '微软雅黑';
            margin-left:20px;
        }
        span{
            font-size:14px;
        }
        .PeriodBtn{
            padding-left: 10px;
        }
        .el-button{
            height: 30px;
            // width:100px;
            padding: 0 30px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
        .indexdialog{
            .el-overlay{
                .el-dialog{
                    .el-dialog__body{
                        padding: 30px 60px 30px 20px !important;
                        .periodFrom{
                            .el-select, .el-input{
                                width: 100%;
                            }
                        }
                    }
                }
            }
        }
    }
  .evaluationRuleTableData{
    padding: 10px 20px;
    .el-table{
        .el-table__body-wrapper{
            height: 64vh !important;
        }
        .el-input__inner{
            height: 30px !important;
        }
        .el-radio__label{
            display: none;
        }
        
        .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;
    }
    }
    
}
</style>