<template>
    <div class="evaluationDepartment" v-loading="loadingImport">
        <div class="evaluationDepartmentBtn">
            <div class="collectSeach">
                <div class="exceed">
                    <span class="exceedSpan">部门名称：</span>
                    <el-input v-model="departmentS" 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">
                <btns
                :showImport="false"
                :showExport="false"
                :showAdd="$utils.havePurview('evalDepartment:oneLevel:add')"
                :showSave="$utils.havePurview('evalDepartment:oneLevel:save')"
                :showDelete="$utils.havePurview('evalDepartment:oneLevel:delete')"
                :disSave="changeDateSelect.length === 0 || changeDateSelect.length>1"
                :disDelete="changeDateSelect.length === 0"
                :disImport="true"
                :disExport="true"
                @handleAdd="handleAdd"
                @handleEdit="handleEdit"
                @handleDelete="handleDelete"
                @handleImport="handleImport"
                @handleExport="handleExport"
                ></btns>
            </div>
            <div class="evaluationDepartmentTableData">
                <el-table :data="evaluationDepartmentData" style="width: 100%" @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="部门名称" prop="organizationName" />
                    <el-table-column label="考评类别" prop="organizationType" />
                    <el-table-column label="考评周期" prop="evaluationCycle" />
                    <el-table-column label="考评规则" prop="evaluateRuleName" />
                    <el-table-column label="业务阶段" prop="binStageName" />
                    <el-table-column label="业务属性" prop="binSectionName" />
                    <!-- <el-table-column label="年" prop="year" />
                    <el-table-column label="月" prop="month" /> -->
                    <el-table-column label="创建时间" prop="createTime" />
                </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="organizationName">
                                <el-select v-model="ruleForm.organizationName" placeholder="请选择部门" @change="changeRule">
                                    <el-option
                                    v-for="item in departmentDataAll"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="考评规则" prop="evaluateRule">
                                <el-select v-model="ruleForm.evaluateRule" placeholder="请选择考评规则" @change="changeRule">
                                    <el-option
                                    v-for="item in ruleDataAll"
                                    :key="item.id"
                                    :label="item.ruleName"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="业务阶段">
                                <el-select v-model="ruleForm.binStage" placeholder="请选择业务阶段" disabled>
                                    <el-option
                                    v-for="item in stageData"
                                    :key="item.id"
                                    :label="item.stageName"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="业务属性">
                                <el-select v-model="ruleForm.binSection" placeholder="请选择业务属性" disabled>
                                    <el-option
                                    v-for="item in moduleData"
                                    :key="item.id"
                                    :label="item.sectionName"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="考评周期" prop="evaluationCycle">
                                <el-select v-model="ruleForm.evaluationCycle" placeholder="请选择业务阶段">
                                    <el-option
                                    v-for="item in periodData"
                                    :key="item.keyValue"
                                    :label="item.keyName"
                                    :value="item.keyValue">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="指标顺序" prop="orderNum">
                                <el-input-number v-model="ruleForm.orderNum" :min="1" />
                            </el-form-item>
                        </el-form>
                    </div>
                    <template #footer>
                        <span class="dialog-footer">
                            <el-button @click="dialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="saveevaluationDepartmentMsg('ruleForm')">确 定</el-button>
                        </span>
                    </template>
                </el-dialog>
            </div>
            <indicator-item ref="indicatorItem"></indicator-item>
        </div>
        <import-dailog ref="importPage" @successImport="successImport" @importLoading="importLoading"></import-dailog>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import btns from '../elbuttonS.vue'
import {apiGetOrganizationList, apiGetorganizationSaveList, apiGetorganizationdeleteList, 
apiGetdatadictionaryList,apiGetbinstageList,apiGetbinsectionList, apiGetevaluateListAll, apiGetorganizationstructureFromTree} from '../../api/api'
export default {
  components: { importDailog, btns },
    data() {
        return {
            dialogVisible: false,
            title: '',
            isSave: false,
            changeDateSelect: [],
            indicatoroptions: [],
            departmentS: '',
            evaluationDepartmentData:[],
            moduleData: [],
            stageData: [],
            ruleDataAll: [],
            periodData: [],
            departmentDataAll: [],
            ruleForm: {
                organizationName: '',
                evaluateRule: '',
                binSection: '',
                binStage: '',
                evaluationCycle: '',
                orderNum: 1
            },
            rules: {
                organizationName: [
                    { required: true, message: '请选择部门名称', trigger: 'change' }
                ],
                evaluationCycle: [
                    { required: true, message: '请选择考评周期', trigger: 'change' }
                ],
                evaluateRule: [
                    { required: true, message: '请选择考评规则', trigger: 'change' }
                ],
                binSection: [
                    { required: true, message: '请选择业务属性', trigger: 'change' }
                ],
                binStage: [
                    { required: true, message: '请输入业务阶段', trigger: 'change' }
                ],
                orderNum: [
                    { required: true, message: '请输入指标顺序', trigger: 'blur' }
                ]
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            loadingImport: false,
        }
    },
    created() {
        this.getEvalCompanyData()
        this.getRuleallData()
        this.getDataDictionary() //模块//阶段
        this.getPeriodData()
        this.getDepartmentFromTreeData()
    },
    methods:{
        // 查询部门列表数据
        getEvalCompanyData() {
            let that = this
            let params = {
                pageNum: this.page.currentPage,
                pageSize: this.page.pagesize,
                organizationType: 'BMKP',
                organizationName: that.departmentS
            }
            apiGetOrganizationList(params).then(datas =>{
                if (datas && datas.data) {
                    that.evaluationDepartmentData = datas.data.records
                    that.page.total = datas.data.total
                }
            })
        },
        //根据组织树查询部门
        getDepartmentFromTreeData() {
            let that = this
            let params = {
                id: '23031001',
                num: 1
            }
            apiGetorganizationstructureFromTree(params).then(datas =>{
                if (datas && datas.data) {
                    that.departmentDataAll = datas.data[0].children
                }
            })
        },
        // 查询规则模块和阶段数据
        getDataDictionary() {
            let that = this
            apiGetbinsectionList().then(datas =>{
                if (datas && datas.data) {
                    that.moduleData = datas.data
                }
            })
            apiGetbinstageList().then(datas =>{
                if (datas && datas.data) {
                    that.stageData = datas.data
                }
            })
        },
        getRuleallData() {
            let that = this
            apiGetevaluateListAll().then(datas =>{
                if (datas && datas.data) {
                    that.ruleDataAll = datas.data
                }
            })
        },
        //考评周期
        getPeriodData() {
            let that = this
            let params = {
                superKey: 'KPZQ0001'
            }
            apiGetdatadictionaryList(params).then(datas =>{
                if (datas && datas.data) {
                    that.periodData = datas.data
                }
            })
        },
        changeRule(val) {
            this.ruleDataAll.forEach(item =>{
                if (item.id === val) {
                    this.ruleForm.binSection = item.binSection
                    this.ruleForm.binStage = item.binStage
                }
            })
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增考评部门'
            this.$nextTick(() =>{
                // this.$refs['ruleForm'].resetFields()
                this.ruleForm = {
                    organizationName: '',
                    evaluateRule: '',
                    binSection: '',
                    binStage: '',
                    evaluationCycle: '',
                    orderNum: 1
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改考评部门'
            this.evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                organizationName: this.evalradio.organizationId,
                evaluationCycle: this.evalradio.evaluationCycle,
                evaluateRule: this.evalradio.evaluateRuleId,
                binSection: this.evalradio.binSection,
                binStage: this.evalradio.binStage,
                orderNum: this.evalradio.orderNum
            }
        },
        saveevaluationDepartmentMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let orgName = ''
            that.departmentDataAll.forEach(item =>{
                if (item.id === that.ruleForm.organizationName) {
                    orgName = item.name
                }
            })
            let params = {
                organizationName: orgName,
                evaluationCycle: that.ruleForm.evaluationCycle,
                organizationType: 'BMKP',
                evaluateRuleId: that.ruleForm.evaluateRule,
                binSection: that.ruleForm.binSection,
                binStage: that.ruleForm.binStage,
                orderNum: that.ruleForm.orderNum
            }
            if (that.isSave) {
                params.id = that.evalradio.id
                params.organizationId = that.evalradio.organizationName
            } else {
                params.organizationId = that.ruleForm.organizationName
            }
            apiGetorganizationSaveList(params).then(datas =>{
                if (!datas.success) {
                    that.$message({
                        message: datas.message,
                        type: 'error'
                    });
                } else {
                    if (!that.isSave) {
                        that.$message({
                            message: '考评部门新增成功',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            message: '考评部门修改成功',
                            type: 'success'
                        });
                    }
                    that.dialogVisible = false
                    that.changeDateSelect = []
                    that.getEvalCompanyData()
                }
            })
        },
        //删除指标数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiGetorganizationdeleteList(paramsArr.join(',')).then(datas =>{
                    if (datas) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        that.changeDateSelect = []
                        that.getEvalCompanyData()
                    }
                })
            })
        },        
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getEvalCompanyData()
        },
        resetSeach() {
            this.page.currentPage = 1
            this.departmentS = ''
            this.getEvalCompanyData()
        },
        successImport(val) {
            this.loadingImport = false
            this.getEvalCompanyData()
        },
        importLoading(val) {
            this.loadingImport = true
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getEvalCompanyData()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getEvalCompanyData()
        },
        handleImport() {
            this.$refs.importPage.upload.title = "考评指标管理信息导入"
            this.$refs.importPage.upload.open = true
        },
    }
}
</script>

<style lang="less">
.evaluationDepartment{
  .evaluationDepartmentBtn{
        .collectSeach{
            display: flex;
            padding: 24px 20px;
            border-bottom: 1px solid#D6DBEA;
                .exceed{
                    display: flex;
                    .exceedSpan{
                        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;
            // 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%;
                            }
                        }
                    }
                }
            }
        }
    }
  .evaluationDepartmentTableData{
    .el-table{
        .el-table__body-wrapper{
            height: 60vh !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>