<template>
    <div class="evaluationPerson" v-loading="loadingImport">
        <div class="evaluationPersonBtn">
            <div class="collectSeach" :style="$utils.collectSeachSty()">
                <div class="exceed">
                    <span class="exceedSpan">人员名称：</span>
                    <el-input v-model="companyS" placeholder="请输入人员名称"></el-input>
                </div>
                <seachs @handleSeach="getSeachData" @handleRest="resetSeach"></seachs>
            </div>
            <div class="PeriodBtn" :style="$utils.PeriodBtnSty()">
                <btns
                :showImport="false"
                :showExport="false"
                :showAdd="$utils.havePurview('evalPerson:oneLevel:add')"
                :showSave="$utils.havePurview('evalPerson:oneLevel:save')"
                :showDelete="$utils.havePurview('evalPerson: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="evaluationPersonTableData">
                <el-table :data="evaluationPersonData" style="width: 100%" :class="personTableSty()"
                 @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="所属公司" prop="companyName" />
                    <el-table-column label="所属部门" prop="deptName" />
                    <el-table-column label="人员名称" prop="personnelName" />
                    <el-table-column label="人员岗位" prop="position" />
                    <el-table-column label="人员职称" prop="rank" />
                    <el-table-column label="考评周期" prop="evaluationCycle" />
                    <el-table-column label="考评规则ID" prop="evaluateRuleId" />
                    <el-table-column label="是否考核" prop="isCheck" />
                    <el-table-column label="考核级别" prop="evaluationLevel" />
                    <el-table-column label="年份" prop="year" />
                    <el-table-column label="月份" prop="month" />
                </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="companyName">
                                <el-select v-model="ruleForm.companyName" placeholder="请选择所属公司" @change="changeCompany">
                                    <el-option
                                    v-for="item in companyDatas"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="所属部门" prop="deptName">
                                <el-select v-model="ruleForm.deptName" placeholder="请选择所属部门" @change="changeDepartment">
                                    <el-option
                                    v-for="item in departmentDatas"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="人员名称" prop="personnelName">
                                <el-select v-model="ruleForm.personnelName" placeholder="请选择人员名称" @change="changePerson">
                                    <el-option
                                    v-for="item in personDatas"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="人员岗位" prop="position">
                                <el-input v-model="ruleForm.position" placeholder="请输入人员岗位"></el-input>
                            </el-form-item>
                            <el-form-item label="人员职称" prop="rank">
                                <el-input v-model="ruleForm.rank" placeholder="请输入人员职称"></el-input>
                            </el-form-item>
                            <el-form-item label="是否考核">
                                <el-radio-group v-model="ruleForm.isCheck">
                                    <el-radio border label="是" />
                                    <el-radio border label="否" />
                                </el-radio-group>
                            </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="考评周期" 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="evaluationLevel">
                                <el-select v-model="ruleForm.evaluationLevel" placeholder="请选择考评级别">
                                    <el-option
                                    v-for="item in levelDatas"
                                    :key="item.keyValue"
                                    :label="item.keyName"
                                    :value="item.keyValue">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="年份" prop="year">
                                <el-date-picker
                                    v-model="ruleForm.year"
                                    type="year"
                                    value-format="YYYY"
                                    placeholder="请选择年份"
                                    />
                            </el-form-item>
                            <el-form-item label="月份">
                                <el-input-number v-model="ruleForm.month" :min="1" :max="12" />
                            </el-form-item>
                        </el-form>
                    </div>
                    <template #footer>
                        <span class="dialog-footer">
                            <el-button @click="dialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="saveevaluationPersonMsg('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 seachs from '../seachGroup.vue'
import {apiGetpersonnelList, apiGetpersonnelSaveList, apiGetpersonneldeleteList,
 apiGetevaluateListAll, apiGetorganizationgetTree, apiGetdatadictionaryList, apiGetuserListAll} from '../../api/api'
export default {
  components: { importDailog, btns, seachs },
    data() {
        return {
            dialogVisible: false,
            title: '',
            isSave: false,
            changeDateSelect: [],
            indicatoroptions: [],
            companyS: '',
            evaluationPersonData:[],
            ruleDataAll: [],
            periodData: [],
            levelDatas: [],
            companyDatas: [],
            departmentDatas: [],
            personDatas: [],
            ruleForm: {
                companyName: '',
                deptName: '',
                personnelName: '',
                position: '',
                rank: '',
                isCheck: '否',
                evaluateRule: '',
                evaluationCycle: '',
                evaluationLevel: '',
                year: '',
                month: 1
            },
            rules: {
                companyName: [
                    { required: true, message: '请输入所属公司', trigger: 'blur' }
                ],
                deptName: [
                    { required: true, message: '请输入所属部门', trigger: 'blur' }
                ],
                personnelName: [
                    { required: true, message: '请输入人员名称', trigger: 'blur' }
                ],
                position: [
                    { required: true, message: '请输入人员岗位', trigger: 'blur' }
                ],
                rank: [
                    { required: true, message: '请输入人员职称', trigger: 'blur' }
                ],
                evaluateRule: [
                    { required: true, message: '请选择考评规则', trigger: 'change' }
                ],
                evaluationCycle: [
                    { required: true, message: '请选择考评周期', trigger: 'change' }
                ],
                evaluationLevel: [
                    { required: true, message: '请输入考评级别', trigger: 'blur' }
                ],
                year: [
                    { required: true, message: '请选择年份', trigger: 'change' }
                ]
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            loadingImport: false,
            winPix: window.devicePixelRatio,
            innerWidth: window.innerWidth
        }
    },
    created() {
        this.getEvalpersonData()
        this.getRuleallData()
        this.getOrganizetionData(23031000, 2, 'GS')
        this.getPeriodData()
        this.getevalLevel()
    },
    methods:{
        // 查询人员配置数据
        getEvalpersonData() {
            let that = this
            let params = {
                pageNum: this.page.currentPage,
                pageSize: this.page.pagesize,
                organizationType: 'DWKP',
                personnelName: that.companyS
            }
            apiGetpersonnelList(params).then(datas =>{
                if (datas && datas.data) {
                    that.evaluationPersonData = datas.data.records
                    that.page.total = datas.data.total
                }
            })
        },
        //查询组织数据
        getOrganizetionData(id, num, type) {
            let that = this
            let params = {
                id: id,
                num: num
            }
            apiGetorganizationgetTree(params).then(datas =>{
                if (datas && datas.data && datas.data[0].children.length>0) {
                    if (type === 'GS') {
                        that.companyDatas = datas.data[0].children
                    } else {
                        that.departmentDatas = datas.data[0].children
                    }
                }
            })
        },
        // 查询人员
        getDepartMentPerson(id) {
            let that = this
            let params = {
                orgId: id
            }
            apiGetuserListAll(params).then(datas =>{
                if (datas && datas.data) {
                    that.personDatas = datas.data
                }
            })
        },
        //考评周期
        getPeriodData() {
            let that = this
            let params = {
                superKey: 'KPZQ0001'
            }
            apiGetdatadictionaryList(params).then(datas =>{
                if (datas && datas.data) {
                    that.periodData = datas.data
                }
            })
        },
        // 考评级别
        getevalLevel() {
            let that = this
            let params = {
                superKey: 'KHJB0001'
            }
            apiGetdatadictionaryList(params).then(datas =>{
                if (datas && datas.data) {
                    that.levelDatas = datas.data
                }
            })
        },
        getRuleallData() {
            let that = this
            apiGetevaluateListAll().then(datas =>{
                if (datas && datas.data) {
                    that.ruleDataAll = datas.data
                }
            })
        },
        changeRule(val) {
            this.ruleDataAll.forEach(item =>{
                if (item.id === val) {
                    this.ruleForm.binSection = item.binSection
                    this.ruleForm.binStage = item.binStage
                }
            })
        },
        changeCompany(val) {
            this.getOrganizetionData(val, 3, 'BM')
            this.ruleForm.deptName = ''
            this.ruleForm.personnelName = ''
        },
        changeDepartment(val) {
            this.getDepartMentPerson(val)
            this.ruleForm.personnelName = ''
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增考评人员'
            this.$nextTick(() =>{
                // this.$refs['ruleForm'].resetFields()
                this.ruleForm = {
                    companyName: '',
                    deptName: '',
                    personnelName: '',
                    position: '',
                    rank: '',
                    isCheck: '否',
                    evaluateRule: '',
                    evaluationCycle: '',
                    evaluationLevel: '',
                    year: '',
                    month: 1
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改考评人员'
            this.evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                companyName: this.evalradio.companyId,
                deptName: this.evalradio.deptId,
                personnelName: this.evalradio.personnelId,
                position: this.evalradio.position,
                rank: this.evalradio.rank,
                isCheck: this.evalradio.isCheck,
                evaluateRule: this.evalradio.evaluateRuleId,
                evaluationCycle: this.evalradio.evaluationCycle,
                evaluationLevel: this.evalradio.evaluationLevel,
                year: this.evalradio.year,
                month: this.evalradio.month
            }
            this.getOrganizetionData(this.evalradio.companyId, 3, 'BM')
            this.getDepartMentPerson(this.evalradio.deptId)
        },
        saveevaluationPersonMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let comName = ''
            let departName = ''
            let personName = ''
            this.companyDatas.forEach(it =>{
                if (it.id === that.ruleForm.companyName) {
                    comName = it.name
                }
            })
            this.departmentDatas.forEach(it =>{
                if (it.id === that.ruleForm.deptName) {
                    departName = it.name
                }
            })
            this.personDatas.forEach(it =>{
                if (it.id === that.ruleForm.personnelName) {
                    personName = it.name
                }
            })
            let params = {
                companyId: that.ruleForm.companyName,
                companyName: comName,
                deptId: that.ruleForm.deptName,
                deptName: departName,
                personnelId: that.ruleForm.personnelName,
                personnelName: personName,
                position: that.ruleForm.position,
                rank: that.ruleForm.rank,
                isCheck: that.ruleForm.isCheck,
                evaluateRuleId: that.ruleForm.evaluateRule,
                evaluationCycle: that.ruleForm.evaluationCycle,
                evaluationLevel: that.ruleForm.evaluationLevel,
                year: that.ruleForm.year,
                month: that.ruleForm.month
            }
            if (that.isSave) {
                params.id = this.evalradio.id
            }
            apiGetpersonnelSaveList(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.getEvalpersonData()
                }
            })
        },
        //删除指标数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiGetpersonneldeleteList(paramsArr.join(',')).then(datas =>{
                    if (datas) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        that.changeDateSelect = []
                        that.getEvalpersonData()
                    }
                })
            })
        },        
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getEvalpersonData()
        },
        resetSeach() {
            this.page.currentPage = 1
            this.companyS = ''
            this.getEvalpersonData()
        },
        successImport(val) {
            this.loadingImport = false
            this.getEvalpersonData()
        },
        importLoading(val) {
            this.loadingImport = true
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getEvalpersonData()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getEvalpersonData()
        },
        handleImport() {
            this.$refs.importPage.upload.title = "考评指标管理信息导入"
            this.$refs.importPage.upload.open = true
        },
        personTableSty() {
            if (this.winPix === 1.25 || this.innerWidth < 1800) {
                return 'person125Table'
            } else {
                return 'person100Table'
            }
        }
    }
}
</script>

<style lang="less">
.evaluationPerson{
  .evaluationPersonBtn{
        .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: 14px;
                    }
                    .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;
            // 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%;
                            }
                            .el-input{
                                height: 30px;
                            }
                        }
                    }
                }
            }
        }
    }
  .evaluationPersonTableData{
    .person125Table{
        .el-table__body-wrapper{
            height: 53vh !important;
        }
    }
    .person100Table{
        .el-table__body-wrapper{
            height: 60vh !important;
        }
    }
    .el-table{
        .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>