<template>
    <div class="achievement">
        <div class="achievementBtn">
            <div class="collectSeach" :style="$utils.collectSeachSty()">
                <div class="exceed">
                    <span class="exceedSpan">业务编码：</span>
                    <el-input v-model="coefficientStr" placeholder="请输入业务编码"></el-input>
                </div>
                <div class="exceed">
                    <span class="exceedSpan" style="width: 80px">考评时间：</span>
                    <el-date-picker v-model="resAnnual"  value-format="YYYY" type="year" placeholder="选择时间" />
                </div>
                <seachs @handleSeach="getSeachData" @handleRest="resetSeach"></seachs>
            </div>
            <div class="PeriodBtn" :style="$utils.PeriodBtnSty()">
                <btns
                :showImport="false"
                :showExport="false"
                :showAdd="$utils.havePurview('achieveK:oneLevel:add')"
                :showSave="$utils.havePurview('achieveK:oneLevel:save')"
                :showDelete="$utils.havePurview('achieveK: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="achievementTableData">
                <el-table :data="achievementData" style="width: 100%" :class="achievementTableSty()"
                 @select="rowClick" @select-all="rowClick" @row-dblclick="detailWatch">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="序号" type="index" />
                    <el-table-column label="业务编码" prop="coefficientCode" />
                    <el-table-column label="单位名称" prop="createOrgName" width="400" />
                    <el-table-column label="业务简述" prop="des" width="400"/>
                    <el-table-column label="年度" prop="year" />
                    <!-- <el-table-column label="单据状态" prop="state" /> -->
                    <el-table-column label="创建人" prop="createName" />
                    <el-table-column label="修改时间" prop="updateTime" width="200" />
                    <!-- <el-table-column label="操作">
                        <template #default="scope">
                            <p class="indicitem" @click="detailWatch(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="95vw" :close-on-click-modal="false">
                    <div class="startDetail">
                        <p class="starttitleSty">基础信息：</p>                        
                            <el-row class="danjuMsg">
                                <el-col :span="6">
                                    <div class="danjuMsg_data">
                                        <span class="data_tit data_tit_1">单位名称:</span>
                                        <el-select v-model="achObj.organizationName" placeholder="请选择部门" @change="changeRule">
                                            <el-option
                                            v-for="item in departmentDataAll"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                            </el-option>
                                        </el-select>
                                    </div>
                                </el-col>
                                <el-col :span="6">
                                    <div class="danjuMsg_data">
                                        <span class="data_tit data_tit_2">年度:</span>
                                        <el-date-picker
                                            v-model="achObj.year"
                                            type="year"
                                            value-format="YYYY"
                                            placeholder="请选择年份"
                                            />
                                    </div>
                                </el-col>
                                <el-col :span="24">
                                    <div class="danjuMsg_data">
                                        <span class="data_tit data_tit_1">业务简述:</span>
                                        <el-input v-model="achObj.desc" :rows="2" type="textarea" placeholder="请输入描述"></el-input>
                                    </div>
                                </el-col>
                            </el-row>
                        <div class="detaTableBtns">
                            <div class="tableBtn add" @click="addDetail">
                                <img :src="addIcon" alt="">
                                <span>新增</span>
                            </div>
                            <!-- <div class="tableBtn save" @click="saveDetail">
                                <img :src="saveIcon" alt="">
                                <span>保存</span>
                            </div> -->
                            <!-- <div class="tableBtn edit">
                                <img :src="editIcon" alt="">
                                <span>新增</span>
                            </div> -->
                            <div class="tableBtn delete" @click="deleteTableData">
                                <img :src="deleteIcon" alt="">
                                <span>删除</span>
                            </div>
                        </div>
                        <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                            <el-tab-pane label="权重分配" name="first">
                                <el-table :data="weightAllocation" style="width: 100%" @select="rowClickTable" @select-all="rowClickTable">
                                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                                    <el-table-column type="index" label="序号" width="80" />
                                    <el-table-column label="考评单位">
                                        <template #default="scope">
                                            <el-select v-model="scope.row.organizationId" placeholder="请选择考评单位" @change="(val)=>chooseOrg(val, scope.row)">
                                                <el-option
                                                v-for="item in companyDatas"
                                                :key="item.id"
                                                :label="item.name"
                                                :value="item.id">
                                                </el-option>
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="业务属性">
                                        <template #default="scope">
                                            <el-select v-model="scope.row.binSection" placeholder="请选择业务属性">
                                                <el-option
                                                v-for="item in moduleData"
                                                :key="item.id"
                                                :label="item.sectionName"
                                                :value="item.id">
                                                </el-option>
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="资产管理系数" align="center">
                                        <el-table-column label="资产总额">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.zzc" :min="0" />
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="K1">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.k1" :precision="2" :step="0.1" :min="0" :max="1" />
                                            </template>
                                        </el-table-column>
                                    </el-table-column>
                                    <el-table-column label="幅度管理系数" align="center">
                                        <el-table-column label="机组">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.jzs" :min="0" />
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="K2">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.k2" :precision="2" :step="0.1" :min="0" :max="1" />
                                            </template>
                                        </el-table-column>
                                    </el-table-column>
                                    <el-table-column label="效益贡献系数" align="center">
                                        <el-table-column label="账面利润总额">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.zmlrze" :min="0" />                                                
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="发电量">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.fdl" :min="0" />
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="度电利润">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.ddlr" :min="0" />
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="K3">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.k3" :precision="2" :step="0.1" :min="0" :max="1" />
                                            </template>
                                        </el-table-column>
                                    </el-table-column>
                                    <el-table-column label="定员管理系数" align="center">
                                        <el-table-column label="定员">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.dy" :min="0" />
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="K4">
                                            <template #default="scope">
                                                <el-input-number v-model="scope.row.k4" :precision="2" :step="0.1" :min="0" :max="1" />
                                            </template>
                                        </el-table-column>
                                    </el-table-column>
                                    <el-table-column label="K">
                                        <template #default="scope">
                                            <el-input-number v-model="scope.row.k" :precision="2" :step="0.1" :min="0" :max="1" />
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="填报人" prop="createName">
                                        <!-- <template #default="scope">
                                            <el-input v-model="scope.row.createBy" placeholder="请输入填报人"></el-input>
                                        </template> -->
                                    </el-table-column>
                                    <el-table-column label="填报日期" prop="createTime">
                                        <!-- <template #default="scope">
                                            <el-date-picker
                                                v-model="scope.row.createTime"
                                                type="date"
                                                value-format="YYYY-MM-DD"
                                                :size="size"
                                            />
                                        </template> -->
                                    </el-table-column>
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>                        
                    </div>
                    <template #footer>
                        <span class="dialog-footer">
                            <el-button @click="dialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="saveachievementMsg()">确 定</el-button>
                        </span>
                    </template>
                </el-dialog>
            </div>
            <indicator-item ref="indicatorItem"></indicator-item>
        </div>
        <achievement-detail ref="achievementDetail"></achievement-detail>
    </div>
</template>

<script>
import achievementDetail from './achievementKDetail.vue'
import btns from '../elbuttonS.vue'
import seachs from '../seachGroup.vue'
import {apiGetyeargroupcoefficientList, apiGetyeargroupcoefficientsaveGroup, apiDeleteyearcoefficientRemove,apiGetbinsectionList,
 apiGetorganizationstructureFromTree, apiGetorganizationgetTree, apigetyeargroupcoefficientlistById} from '../../api/api'
export default {
  components: { btns, achievementDetail, seachs },
    data() {
        return {
            dialogVisible: false,
            title: '',
            isSave: false,
            changeDateSelect: [],
            changeTableSelect: [],
            coefficientStr: '',
            resAnnual: '',
            achievementData:[],
            departmentDataAll: [],
            weightAllocation: [],
            companyDatas: [],
            moduleData: [],
            achObj: {
                organizationName: '',
                year: '',
                des: ''
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            activeName: 'first',
            winPix: window.devicePixelRatio,
            innerWidth: window.innerWidth
        }
    },
    created() {
        this.resAnnual = new Date().getFullYear().toString()
        this.getachievementData()
        this.getDepartmentFromTreeData()
        this.getOrganizetionData()
        this.getBinSectionS()
    },
    methods:{
        // 查询人员配置数据
        getachievementData() {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize,
                coefficientCode: that.coefficientStr,
                year: that.resAnnual
            }
            apiGetyeargroupcoefficientList(params).then(datas =>{
                if (datas && datas.data) {
                    that.achievementData = 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
                }
            })
        },
        // 查询业务属性
        getBinSectionS() {
            let that = this
            apiGetbinsectionList().then(datas =>{
                if (datas && datas.data) {
                    that.moduleData = datas.data
                }
            })
        },
        //查询组织数据
        getOrganizetionData() {
            let that = this
            let params = {
                id: '23031000',
                num: 1
            }
            apiGetorganizationgetTree(params).then(datas =>{
                if (datas && datas.data && datas.data[0].children.length>0) {
                    that.companyDatas = datas.data[0].children
                }
            })
        },
        addDetail() {
            let userMes = JSON.parse(window.sessionStorage.getItem('user'))
            let obj = {
                id: Math.random(),
                organizationId: '',
                binSection: '',
                zzc: 0,
                k1: 0,
                jzs: 0,
                k2: 0,
                zmlrze: 0,
                fdl: 0,
                ddlr: 0,
                k3: 0,
                dy: 0,
                k4: 0,
                k: 0,
                createBy: userMes.id,
                createName: userMes.name,
                createTime: this.getPowerTime(new Date())
            }
            this.weightAllocation.push(obj)
        },
        deleteTableData() {
            for(let i =0; i<this.changeTableSelect.length;i++) {
                let item = this.changeTableSelect[i]
                for(let j =0; j<this.weightAllocation.length;j++) {
                    let it = this.weightAllocation[j]
                    if (item.id === it.id) {
                        this.weightAllocation.splice(j, 1)
                    }
                }
            }
        },
        //转换时间
        getPowerTime(date){
            var y = date.getFullYear();  
            var m = date.getMonth() + 1;  
            m = m < 10 ? ('0' + m) : m;  
            var d = date.getDate();  
            d = d < 10 ? ('0' + d) : d;  
            var h = date.getHours();  
            h=h < 10 ? ('0' + h) : h;  
            var minute = date.getMinutes();  
            minute = minute < 10 ? ('0' + minute) : minute;
            var second=date.getSeconds();  
            second=second < 10 ? ('0' + second) : second;  
            let timeF = ''
            timeF = y + '-' + m + '-' + d+' '+h+':'+minute+ ':'+second
            return timeF
        },
        //根据id查询权重数据
        getWeightAllocation(id) {
            let that = this
            let params = {
                id: id
            }
            apigetyeargroupcoefficientlistById(params).then(datas =>{
                if (datas && datas.data) {
                    that.achObj = {
                        organizationName: datas.data.createOrgId,
                        year: datas.data.year,
                        desc: datas.data.des
                    }
                    that.weightAllocation = datas.data.coefficientList
                }
            })
        },
        detailWatch(row) {
            this.$refs.achievementDetail.init(row)
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增业绩考核系数'
            this.achObj = {
                organizationName: '',
                year: '',
                des: ''
            }
            this.weightAllocation = []
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改业绩考核系数'
            this.evalradio = this.changeDateSelect[0]
            this.getWeightAllocation(this.evalradio.id)
        },
        saveachievementMsg() {
            let that = this
            if (that.achObj.organizationName === '' || that.achObj.year === '') {
                that.$message({
                    message: '请完善基础信息',
                    type: 'error'
                })
            } else {
                that.saveAndEditIndicatorData()
            }
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let userMes = JSON.parse(window.sessionStorage.getItem('user'))
            let orgName = ''
            this.departmentDataAll.forEach(item =>{
                if (item.id === that.achObj.organizationName) {
                    orgName = item.name
                }
            })
            let params = {
                createOrgId: that.achObj.organizationName,
                createOrgName: orgName,                
                year: that.achObj.year,
                des: that.achObj.desc,
                coefficientList: that.weightAllocation,
                createOrgId: userMes.unitId,
                createOrgName: userMes.unitName,
                createBy: userMes.id,
                createName: userMes.name
            }
            if (that.isSave) {
                params.id = this.evalradio.id
            }
            apiGetyeargroupcoefficientsaveGroup(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.getachievementData()
                }
            })
        },
        //删除指标数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiDeleteyearcoefficientRemove(paramsArr.join(',')).then(datas =>{
                    if (datas) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        that.changeDateSelect = []
                        that.getachievementData()
                    }
                })
            })
        },        
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        rowClickTable(selection, row) {
            this.changeTableSelect = selection
        },
        chooseOrg(val, row) {
            this.companyDatas.forEach(item =>{
                if (val === item.id) {
                    row.organizationName = item.name
                }
            })
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getachievementData()
        },
        resetSeach() {
            this.page.currentPage = 1
            this.coefficientStr = ''
            this.resAnnual = ''
            this.getachievementData()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getachievementData()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getachievementData()
        },
        handleImport() {
            this.$refs.importPage.upload.title = "考评指标管理信息导入"
            this.$refs.importPage.upload.open = true
        },
        achievementTableSty() {
            if (this.winPix === 1.25 || this.innerWidth < 1800) {
                return 'achievement125Table'
            } else {
                return 'achievement100Table'
            }
        }
    }
}
</script>

<style lang="less">
.achievement{
  .achievementBtn{
        .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;
                        height: 40px;
                        .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: 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{
                    margin-top: 3vh;
                    .el-dialog__body{
                        padding: 0 20px !important;
                        .startDetail{
                            .starttitleSty{
                                font-size: 18px;
                                font-family: Microsoft YaHei;
                                font-weight: bold;
                                color: #3B7AD1;
                                // line-height: 12px;
                                margin: 20px 0 20px 10px;
                            }
                            .danjuMsg{
                                border: 1px solid #D6DBEA;
                                padding: 10px 20px;
                                border-radius: 10px;
                                margin-bottom: 20px;
                                .danjuMsg_data{
                                    padding: 5px 0 10px 0;
                                    display: flex;
                                    .data_tit{
                                        font-weight: bold;
                                        font-size: 14px;
                                        font-family: Microsoft YaHei;
                                        color: #8991B0;
                                        position: relative;
                                        top: 7px;
                                    }
                                    .data_tit_1{
                                        width: 100px;
                                    }
                                    .data_tit_2{
                                        width: 60px;
                                    }
                                    .el-select, .el-input{
                                        width: 90%;
                                        height: 40px;
                                    }
                                    .el-textarea{
                                        left: -7px;
                                    }
                                }
                            }
                            .detaTableBtns{
                                display: flex;
                                width: 300px;
                                position: relative;
                                top: 32px;
                                left: 87vw;
                                z-index: 11111;
                                .tableBtn {
                                    display: flex;
                                    margin-right: 30px;
                                    width: 13%;
                                    img{
                                        margin-right: 5px;
                                        margin-top: 1px;
                                    }
                                    span{
                                        font-size: 14px;
                                        font-family: Microsoft YaHei;
                                        font-weight: 400;
                                    }
                                }
                                .add{
                                    cursor: pointer;
                                    span{
                                        color: #3B7AD1;
                                    }
                                }
                                .save{
                                    cursor: pointer;
                                    span{
                                        color: #50C14E;
                                    }
                                }
                                .edit{
                                    span{
                                        color: #F5A623;
                                    }
                                }
                                .delete{
                                    cursor: pointer;
                                    span{
                                        color: #F65177;
                                    }
                                }
                            }
                            .el-tabs{
                                .el-tabs__header{
                                    .el-tabs__nav{
                                        .el-tabs__item{
                                            font-size: 18px;
                                            font-family: Microsoft YaHei;
                                            font-weight: bold;
                                            margin: 0 10px;                                    
                                            color: #8991B0;
                                        }
                                        .is-active{
                                            font-size: 18px;
                                            font-family: Microsoft YaHei;
                                            font-weight: bold;
                                            color: #3B7AD1;
                                            margin: 0 10px;
                                        }
                                    }
                                }
                                
                                .el-table{
                                    .el-table__body-wrapper{
                                        height: 33vh;
                                    }
                                    .el-table__row{
                                        .cell{
                                            .el-input{
                                                height: 24px;
                                                .el-input__inner{
                                                    height: 24px;
                                                }
                                            }
                                            .el-input-number--small, .el-date-editor{
                                                width: 100px;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        .achievementTableData{
            .achievement125Table{
                .el-table__body-wrapper{
                    height: 53vh !important;
                }
            }
            .achievement100Table{
                .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>