<template>
    <div class="evaluationDepartment">
        <div class="evaluationDepartmentBtn">
            <div class="collectSeach" :style="$utils.collectSeachSty()">
                <div class="exceed">
                    <span class="exceedSpan">部门名称：</span>
                    <el-input v-model="departmentS" placeholder="请输入部门名称"></el-input>
                </div>
                <div class="exceed">
                    <span class="exceedSpan">部门领导：</span>
                    <el-input v-model="leaderS" 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('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"
                ></btns>
            </div>
            <div class="evaluationDepartmentTableData">
                <el-table :data="evaluationDepartmentData" style="width: 100%" :class="departmentTableSty()"
                 @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="序号" prop="serialNumber" width="100" />
                    <el-table-column label="部门名称" prop="deptName" />
                    <el-table-column label="部门领导名称" prop="chargePersonName" />
                    <el-table-column label="部门应用">
                        <template #default="scope">
                            <span v-if="scope.row.yjzbjhType === '1'">业绩指标计划</span>
                            <span v-if="scope.row.yjzbjhType === '1' && scope.row.jxjgkhsbType === '1'">,</span>
                            <span v-if="scope.row.jxjgkhsbType === '1'">绩效考核申报</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>
            <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="department">
                            <el-select v-model="ruleForm.department" placeholder="请选择所属部门" :disabled="isSave"
                             filterable  @change="changedepart2Data">
                                <el-option
                                v-for="item in departData"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="二级部门">
                            <el-select v-model="ruleForm.depart2ment" placeholder="请选择所属部门" :disabled="isSave" >
                                <el-option
                                v-for="item in depart2Data"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="部门领导" prop="departmentLeader">
                            <el-select v-model="ruleForm.departmentLeader" placeholder="请选择部门领导" filterable>
                                <el-option
                                v-for="item in deptLeaderOption"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                <span style="float: left;display:inline-block;width:100px">{{ item.name }}</span>
                                <span style="float: right;color: #909399;font-size: 13px;" >{{ item.no }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="部门应用" prop="departmentIn">
                            <el-checkbox-group v-model="ruleForm.departmentIn">
                                <el-checkbox label="业绩指标计划" name="type" :disabled="ruleForm.depart2ment !== ''" />
                                <el-checkbox label="绩效考核申报" name="type" />
                            </el-checkbox-group>
                        </el-form-item>
                        <el-form-item label="排序" >
                            <el-input-number v-model="ruleForm.serialNumber" :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>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import btns from '../elbuttonS.vue'
import seachs from '../seachGroup.vue'
import addW from '../../assets/btnIcon/addW.png'
import addIcon from '../../assets/btnIcon/add.png'
import {apiGetEvaluationDeptList, apiGetevaluationdeptSave, apiGetevaluationdeptremoveAll, apiGetorganizationstructureFromTree,
apiGetDepartmentallocationLeader,
apievaluationdeptallocationgetDept,apievaluationdeptallocationgetDept2, apievaluationdeptallocationgetUser} from '../../api/api'
export default {
  components: { importDailog, btns, seachs },
    data() {
        return {
            dialogVisible: false,
            title: '',
            isSave: false,
            changeDateSelect: [],
            departmentS: '',
            leaderS: '',
            evaluationDepartmentData:[],
            departData: [],
            depart2Data: [],
            deptLeaderOption: [],
            addW: addW,
            addIcon: addIcon,
            ruleForm: {
                department: '',
                depart2ment: '',
                departmentLeader: '',
                serialNumber: 1,
                departmentIn: []
            },
            rules: {
                department: [
                    { required: true, message: '请选择部门名称', trigger: 'change' }
                ],
                departmentLeader: [
                    { required: true, message: '请选择部门领导', trigger: 'change' }
                ],
                departmentIn: [
                    {
                    type: 'array',
                    required: true,
                    message: '请选择部门应用',
                    trigger: 'change',
                    },
                ]
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            winPix: window.devicePixelRatio,
            innerWidth: window.innerWidth
        }
    },
    created() {
        this.getEvalCompanyData()
        this.getDepartmentFromTreeData()
        this.indicatorCategorys = [
            {
                label: '指标计划',
                value: '指标计划'
            },
            {
                label: '指标结果',
                value: '指标结果'
            }
        ]
    },
    methods:{
        // 查询部门列表数据
        getEvalCompanyData() {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize,
                deptName: that.departmentS,
                chargePersonName: that.leaderS
            }
            apiGetEvaluationDeptList(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.departData = datas.data[0].children
                }
            })
            // 测试数据
            // apievaluationdeptallocationgetDept().then(datas =>{
            //     if (datas && datas.data) {
            //         that.departData = datas.data
            //     }
            // })
        },
        // 二级部门
        changedepart2Data(val) {
            let that = this
            that.ruleForm.depart2ment = ''
            let params = {
                id: val,
                num: 2
            }
            apiGetorganizationstructureFromTree(params).then(datas =>{
                if (datas && datas.data) {
                    that.depart2Data = datas.data[0].children
                        if (that.isSave) {
                            that.ruleForm.depart2ment = ''
                            if (that.evalradio.parentId !== '') {
                                that.ruleForm.depart2ment = that.evalradio.deptId
                            }
                        }
                    that.getuserPageList(val)
                }
            })
            // 测试数据
            // apievaluationdeptallocationgetDept2(val).then(datas =>{
            //     if (datas && datas.data) {
            //         that.depart2Data = datas.data
            //         if (that.isSave) {
            //             that.ruleForm.depart2ment = ''
            //             if (that.evalradio.parentId !== '') {
            //                 that.ruleForm.depart2ment = that.evalradio.deptId
            //             }
            //         }
            //         that.getuserPageList(val)
            //     }
            // })
        },
        // 查询人员
        getuserPageList(val) {
            let that = this
            apiGetDepartmentallocationLeader(val).then(datas =>{
                if (datas && datas.data) {
                    that.deptLeaderOption = datas.data
                    if (that.isSave) {
                        that.ruleForm.departmentLeader = that.evalradio.chargePersonId
                    }
                }
            })
            // 测试数据
            // apievaluationdeptallocationgetUser().then(datas =>{
            //     if (datas && datas.data) {
            //         that.deptLeaderOption = datas.data
            //     }
            // })
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增考评部门'
            this.$nextTick(() =>{
                this.ruleForm = {
                    department: '',
                    departmentLeader: '',
                    serialNumber: 1,
                    departmentIn: []
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改考评部门'
            this.evalradio = this.changeDateSelect[0]
            let departmentInStr = []
            if (this.evalradio.yjzbjhType === '1') {
                departmentInStr.push('业绩指标计划')
            }
            if (this.evalradio.jxjgkhsbType === '1') {
                departmentInStr.push('绩效考核申报')
            }
            this.ruleForm = {
                // departmentLeader: this.evalradio.chargePersonId,
                serialNumber: this.evalradio.serialNumber,
                departmentIn: departmentInStr
            }
            if (this.evalradio.parentId !== '') {
                this.ruleForm.department = this.evalradio.parentId
                this.changedepart2Data(this.evalradio.parentId)
                // this.getuserPageList(this.evalradio.parentId)
            } else {
                this.ruleForm.department = this.evalradio.deptId
                this.changedepart2Data(this.evalradio.deptId)
                // this.getuserPageList(this.evalradio.deptId)
            }
        },
        saveevaluationDepartmentMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let deptNameS = ''
            let dept2NameS = ''
            let deptLeaderNameS = ''
            that.departData.forEach(item =>{
                if (item.id === that.ruleForm.department) {
                    deptNameS = item.name
                }
            })
            that.depart2Data.forEach(item =>{
                if (item.id === that.ruleForm.depart2ment) {
                    dept2NameS = item.name
                }
            })
            that.deptLeaderOption.forEach(item =>{
                if (item.id === that.ruleForm.departmentLeader) {
                    deptLeaderNameS = item.name
                }
            })
            let yjzbjhTypeIn = '0'
            let jxjgkhsbTypeIn = '0'
            that.ruleForm.departmentIn.forEach(it => {
                if (it === '业绩指标计划') {
                    yjzbjhTypeIn = '1'
                } else if (it === '绩效考核申报') {
                    jxjgkhsbTypeIn = '1'
                }
            })
            let params = {
                chargePersonId: that.ruleForm.departmentLeader,
                chargePersonName: deptLeaderNameS,
                serialNumber: that.ruleForm.serialNumber,
                yjzbjhType: yjzbjhTypeIn,
                jxjgkhsbType: jxjgkhsbTypeIn
            }
            if (that.ruleForm.depart2ment === '') {
                params.deptId = that.ruleForm.department
                params.deptName = deptNameS
            } else {
                params.parentId = that.ruleForm.department
                params.deptId = that.ruleForm.depart2ment
                params.deptName = dept2NameS
            }
            if (that.isSave) {
                params.id = that.evalradio.id
            }
            apiGetevaluationdeptSave(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)
                })
                apiGetevaluationdeptremoveAll(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.leaderS = ''
            this.getEvalCompanyData()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getEvalCompanyData()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getEvalCompanyData()
        },
        departmentTableSty() {
            if (this.winPix === 1.25 || this.innerWidth < 1800) {
                return 'department125Table'
            } else {
                return 'department100Table'
            }
        }
    }
}
</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: 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;
            }
        }
        .el-overlay{
            .el-dialog{
                margin-top: 10vh;
                .el-dialog__body{
                    padding: 30px 60px 30px 20px !important;
                    .periodFrom{
                        .el-select, .el-input{
                            width: 100%;
                        }
                        .el-input{
                            height: 30px;
                        }
                    }
                }
                .el-dialog__footer{
                    .dialog-footer{
                        display: flex;
                        justify-content: center;
                        .el-button{
                            width: 180px !important;
                            height: 40px !important;
                        }
                    }
                }
            }
        }
    }
  .evaluationDepartmentTableData{
    .department125Table{
        .el-table__body-wrapper{
            height: 53vh !important;
        }
    }
    .department100Table{
        .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>