<template>
    <div class="evaluationUnitRating">
        <div class="evaluationUnitRatingBtn">
            <div class="collectSeach" :style="$utils.collectSeachSty()">
                <div class="exceed">
                    <span class="exceedSpan">单位名称：</span>
                    <el-input v-model="resCompanyName" placeholder="请输入单位名称"></el-input>
                </div>
                <div class="exceed">
                    <span class="exceedSpan">业务属性：</span>
                    <el-select v-model="businessStr" placeholder="请选择业务属性" clearable >
                        <el-option
                        v-for="item in binSectionoptions"
                        :key="item.sectionCode"
                        :label="item.sectionName"
                        :value="item.sectionCode">
                        </el-option>
                    </el-select>
                </div>
                <div class="exceed">
                    <span class="exceedSpan" style="width: 80px">考评年度：</span>
                    <el-date-picker v-model="resAnnual" type="year" value-format="YYYY" placeholder="选择年份" />
                </div>
                <seachs @handleSeach="getSeachData" @handleRest="resetSeach"></seachs>
            </div>
            <div class="PeriodBtn" :style="$utils.PeriodBtnSty()">
                <btns
                :showImport="$utils.havePurview('evaluationUnitRate:oneLevel:import')"
                :showExport="$utils.havePurview('evaluationUnitRate:oneLevel:export')"
                :showSave="$utils.havePurview('evaluationUnitRate:oneLevel:save')"
                :showAdd="$utils.havePurview('evaluationUnitRate:oneLevel:add')"
                :showDelete="$utils.havePurview('evaluationUnitRate:oneLevel:delete')"
                :disSave="changeDateSelect.length === 0 || changeDateSelect.length>1"
                :disDelete="changeDateSelect.length === 0"
                :disImport="false"
                :disExport="false"
                @handleAdd="handleAdd"
                @handleEdit="handleEdit"
                @handleDelete="handleDelete"
                @handleImport="handleImport"
                @handleExport="handleExport"
                ></btns>
            </div>
            <div class="evaluationUnitRatingTableData">
                 <!-- @row-click="rowClickToggle" -->
                <el-table :data="evaluationUnitRatingData" style="width: 100%" ref="tableSS" :class="unitRatingTableSty()"
                 @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column type="index" label="序号" align="center"></el-table-column>
                    <el-table-column label="考评单位" width="200">
                        <template #default="scope">
                            <el-tooltip
                                class="box-item"
                                effect="customized"
                                :content="scope.row.organizationShortName"
                                placement="right"
                            >
                                <span class="tooltipCC">{{scope.row.organizationShortName}}</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <el-table-column label="业务板块" prop="binSectionName" />
                    <el-table-column label="考评年度" prop="year" />
                    <el-table-column label="生成经营评级" prop="scjyRate" />
                    <el-table-column label="前期评级" prop="qqRate" />
                    <el-table-column label="基建评级" prop="jjRate" />
                    <el-table-column label="年度评级" prop="yearRate" />
                </el-table>
            </div>
            <el-dialog :title="title" custom-class="startToDia" 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="companyRuleF">
                            <el-select v-model="ruleForm.companyRuleF" placeholder="请选择考评单位" :disabled="isSave">
                                <el-option
                                v-for="item in companyData"
                                :key="item.organizationId"
                                :label="item.organizationShortName"
                                :value="item.organizationId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="业务属性" prop="binSection">
                            <el-select v-model="ruleForm.binSection" placeholder="请选择业务属性">
                                <el-option
                                v-for="item in binSectionoptions"
                                :key="item.sectionCode"
                                :label="item.sectionName"
                                :value="item.sectionCode">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="年份" prop="year">
                            <el-date-picker
                                v-model="ruleForm.year"
                                :disabled="isSave"
                                type="year"
                                value-format="YYYY"
                                placeholder="请选择年份"
                                />
                        </el-form-item>
                        <el-form-item label="生成经营评级">
                            <el-select v-model="ruleForm.scjyRate" placeholder="请选择评级">
                                <el-option
                                v-for="item in ratingArr"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="前期评级">
                            <el-select v-model="ruleForm.qqRate" placeholder="请选择评级">
                                <el-option
                                v-for="item in ratingArr"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="基建评级">
                            <el-select v-model="ruleForm.jjRate" placeholder="请选择评级">
                                <el-option
                                v-for="item in ratingArr"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="年度评级">
                            <el-select v-model="ruleForm.yearRate" placeholder="请选择评级">
                                <el-option
                                v-for="item in ratingArr"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="描述" prop="remark">
                            <el-input v-model="ruleForm.remark" :rows="5" type="textarea" placeholder="请输入描述"></el-input>
                        </el-form-item>
                    </el-form>
                </div>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="saveevaluaStart('ruleForm')">确 定</el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
        <start-from-list ref="startFromDetail" ></start-from-list>
        <import-dailog ref="importPage" @successImport="successImport"></import-dailog>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import startFromList from './evaluationDeptStartFrom.vue'
import btns from '../elbuttonS.vue'
import seachs from '../seachGroup.vue'
import ExcelJS from 'exceljs'
import fileSave from 'file-saver'
import {apiGetorganizationratinglist, apiGetorganizationratingsave,apiGetOrganizationListAll,apiGetbinsectionList,
apiPostorganizationratingDelete} from '../../api/api'
export default {
    components: {
        startFromList,
        importDailog,
        btns,
        seachs
    },
    data() {
        return {
            dialogVisible: false,
            isSave: false,
            title: '',
            evalradio: {},
            changeDateSelect: [],
            evaluationUnitRatingData:[],
            companyData: [],
            binSectionoptions: [],
            ruleForm: {
                companyRuleF: '',
                year: '',
                binSection: '',
                scjyRate: '',
                qqRate: '',
                jjRate: '',
                yearRate: '',
                remark: ''
            },
            rules: {
                companyRuleF: [
                    { required: true, message: '请选择考评单位', trigger: 'change' }
                ],
                binSection: [
                    { required: true, message: '请选择考评单位', trigger: 'change' }
                ],
                year: [
                    { required: true, message: '请选择年份', trigger: 'change' }
                ]
            },
            resCompanyName: '',
            businessStr: '',
            resAnnual: '',
            monthArr: [],
            ratingArr: [],
            winPix: window.devicePixelRatio,
            innerWidth: window.innerWidth
        }
    },
    created() {
        this.ratingArr = [
            {
                label: 'A级',
                value: 'A'
            },
            {
                label: 'B级',
                value: 'B'
            },
            {
                label: 'C级',
                value: 'C'
            }
        ]
        this.resAnnual = new Date().getFullYear().toString()
        this.getevaluRatingList()
        this.getOrganizetionData()
        this.getDataDictionary()
    },
    methods:{
        // 查询列表页面
        getevaluRatingList() {
            let that = this
            let params = {
                pageNum: 1,
                pageSize: 500,
                organizationId: that.resCompanyName,
                binSection: that.businessStr,
                year: that.resAnnual
            }
            apiGetorganizationratinglist(params).then(datas =>{
                if (datas && datas.data.records) {
                    that.evaluationUnitRatingData = datas.data.records
                }
            })
        },
        //查询单位数据
        getOrganizetionData() {
            let that = this
            let params = {
                evaluationCycle: 'NDKP',
                organizationType: 'DWKP'
            }
            apiGetOrganizationListAll(params).then(datas => {
                if (datas && datas.data) {
                    that.companyData = datas.data
                }
            })
        },
        // 查询属性和阶段
        getDataDictionary() {
            let that = this
            let params = {
                type: '1'
            }
            apiGetbinsectionList(params).then(datas =>{
                if (datas && datas.data) {
                    that.binSectionoptions = datas.data
                }
            })
        },
        saveevaluaStart(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改考评启动数据
        saveAndEditIndicatorData() {
            let that = this
            let orgName = ''
            this.companyData.forEach(item =>{
                if (item.organizationId === that.ruleForm.companyRuleF) {
                    orgName = item.organizationShortName
                }
            })
            let params = {
                organizationId: that.ruleForm.companyRuleF,
                organizationShortName: orgName,
                year: that.ruleForm.year,
                binSection: that.ruleForm.binSection,
                scjyRate: that.ruleForm.scjyRate,
                qqRate: that.ruleForm.qqRate,
                jjRate: that.ruleForm.jjRate,
                yearRate: that.ruleForm.yearRate,
                remark: that.ruleForm.remark
            }
            if (that.isSave) {
                params.id = that.evalradio.id
            }
            apiGetorganizationratingsave(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.getevaluRatingList()
                }
            })
        },
        getSeachData() {
            this.getevaluRatingList()
        },
        resetSeach() {
            this.resCompanyName = ''
            this.businessStr = ''
            this.resAnnual = ''
            this.getevaluRatingList()
        },
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        // rowClickToggle(row) {
        //     this.$refs.tableSS.toggleRowSelection(row)
        // },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新建单位绩效考核评级'
            this.$nextTick(() =>{
                this.ruleForm = {
                    companyRuleF: '',
                    year: '',
                    binSection: '',
                    scjyRate: '',
                    qqRate: '',
                    jjRate: '',
                    yearRate: '',
                    remark: ''
                }
            })
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改单位绩效考核评级'
            this.evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                companyRuleF: this.evalradio.organizationId,
                year: this.evalradio.year,
                binSection: this.evalradio.binSection,
                scjyRate: this.evalradio.scjyRate,
                qqRate: this.evalradio.qqRate,
                jjRate: this.evalradio.jjRate,
                yearRate: this.evalradio.yearRate,
                remark: this.evalradio.remark
            }
        },
        //删除
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiPostorganizationratingDelete(paramsArr.join(',')).then(datas =>{
                    that.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    that.getevaluRatingList()
                    that.changeDateSelect = []
                })
            })
        },  
        handleImport() {
            this.$refs.importPage.upload.title = "单位考评评级"
            this.$refs.importPage.upload.open = true
            this.$refs.importPage.upload.url = '/organization-rating/import'
        },
        successImport(val) {
            this.getevaluRatingList()
        },
        handleExport() {
            let data = this.evaluationUnitRatingData  //接口返回数据
            const workbook = new ExcelJS.Workbook()
            const worksheet = workbook.addWorksheet('Sheet1')
            //根据数据自己调整
            let colums = []
            colums = [
                { header: 'ID', key: 'id', width: 26, hidden: true},
                { header: '考评单位', key: 'organizationShortName', width: 26 },
                { header: '考评单位ID', key: 'organizationId', width: 26, hidden: true },
                { header: '业务板块ID', key: 'binSection', width: 26, hidden: true },
                { header: '考评年度', key: 'year', width: 26 },
                { header: '生成经营评级', key: 'scjyRate', width: 26 },
                { header: '前期评级', key: 'qqRate', width: 26 },
                { header: '基建评级', key: 'jjRate', width: 26 },
                { header: '年度评级', key: 'yearRate', width: 26 },
                { header: '备注', key: 'remake', width: 26 },
            ]
            worksheet.columns = colums
            worksheet.getRow(1).font = {
                size: 12,
                bold: true
            }
            worksheet.addRows(data)

            workbook.xlsx.writeBuffer().then(buffer => {
                //这里为type
                const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' })
                fileSave(blob, `单位考评评级数据导出.xlsx`)
            })
        },
        unitRatingTableSty() {
            if (this.winPix === 1.25 || this.innerWidth < 1800) {
                return 'unitRating125Table'
            } else {
                return 'unitRating100Table'
            }
        }
    }
}
</script>

<style lang="less">
.evaluationUnitRating{
    .evaluationUnitRatingBtn{
        .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 20px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
        .evaluationUnitRatingTableData{
            .unitRating125Table{
                .el-table__body-wrapper{
                    height: 63vh !important;
                }
            }
            .unitRating100Table{
                .el-table__body-wrapper{
                    height: 65vh !important;
                }
            }
            .el-table{
                .el-input__inner{
                    height: 30px !important;
                }
                .el-radio__label{
                    display: none;
                }
                .tooltipCC{
                    width: 300px !important;
                    display: inline-block;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
                .tooltipCCDes{
                    width: 50px;
                    display: inline-block;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }
                .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;
            }
        }
        .el-overlay{
            .startToDia{
                .el-dialog__body{
                    padding: 30px 60px 30px 20px !important;
                    .periodFrom{
                        .el-select, .el-input{
                            width: 100%;
                        }
                        .el-input{
                            height: 30px;
                        }
                    }
                    .flowSty{
                        width: 100%;
                        height: 70vh;
                    }
                }
            }
        }
    }
    
}
</style>