<template>
    <div class="taskUnitRatingDetail">
        <el-dialog title="单位考评评级详情" v-model="ratingdialogVisible" :fullscreen="true" :close-on-click-modal="false">
            <div class="taskUnit">
                <p class="starttitleSty">单据信息：</p>
                <el-row class="danjuMsg">
                    <el-col :span="6">
                        <div class="danjuMsg_data">
                            <span class="data_tit">业务编号:</span>
                            <span>{{receiptMsg.code}}</span>
                        </div>
                        <div class="danjuMsg_data">
                            <span class="data_tit">创建日期:</span>
                            <span>{{receiptMsg.createDate}}</span>
                        </div>
                    </el-col>
                    <el-col :span="6">
                        <div class="danjuMsg_data">
                            <span class="data_tit">业务简述:</span>
                            <span>{{receiptMsg.des}}</span>
                        </div>
                        <div class="danjuMsg_data">
                            <span class="data_tit">年度:</span>
                            <span>{{receiptMsg.year}}</span>
                        </div>
                    </el-col>
                    <el-col :span="6">
                        <div class="danjuMsg_data">
                            <span class="data_tit">流程状态:</span>
                            <span>{{receiptMsg.stage}}</span>
                        </div>
                        <div class="danjuMsg_data">
                            <span class="data_tit">单据状态:</span>
                            <span>{{receiptMsg.recStage}}</span>
                        </div>
                    </el-col>
                    <el-col :span="6">
                        <div class="danjuMsg_data">
                            <span class="data_tit">创建人:</span>
                            <span>{{receiptMsg.createName}}</span>
                        </div>
                    </el-col>
                </el-row>
                <div class="evaluationUnitRatingBtn">
                    <div class="headerRight">
                        <div class="seachFor">
                            <el-input v-model="resCompanyName" placeholder="请输入单位名称" clearable
                                @keyup.enter="getevaluRatingDiaList(rowObj, rowkeyId)"></el-input>
                            <el-select v-model="businessStr" placeholder="请选择业务属性" clearable
                                @change="getevaluRatingDiaList(rowObj, rowkeyId)">
                                <el-option v-for="item in binSectionoptions" :key="item.sectionCode"
                                    :label="item.sectionName" :value="item.sectionCode">
                                </el-option>
                            </el-select>
                        </div>
                        <div class="tableBtn add" @click="handleAdd">
                            <img :src="addIcon" alt="">
                            <span>新增</span>
                        </div>
                        <div class="tableBtn save" @click="handleEdit">
                            <img :src="saveIcon" alt="">
                            <span>保存</span>
                        </div>
                        <div class="tableBtn delete" @click="handleDelete">
                            <img :src="deleteIcon" alt="">
                            <span>删除</span>
                        </div>
                        <div class="tableBtn import" @click="handleImport">
                            <img :src="importIcon" alt="">
                            <span>导入</span>
                        </div>
                        <div class="tableBtn export" @click="handleExport">
                            <img :src="exportIcon" alt="">
                            <span>导出</span>
                        </div>
                    </div>
                    <div class="evaluationUnitRatingTableData">
                        <!-- @row-click="rowClickToggle" -->
                        <el-table :data="evaluationRatingDiaData" style="width: 100%" ref="tableSS" @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>
                        <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>
                            <p class="starttitleSty">审批意见：</p>
                            <el-input v-model="descMsg" :rows="3" type="textarea" placeholder="请输入审批意见"></el-input>
                        </div>
                    </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>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="examineAndApprove('agree')">同 意</el-button>
                    <el-button @click="examineAndApprove('reject')">拒 绝</el-button>
                </span>
            </template>
        </el-dialog>
        <import-dailog ref="importPage" @successImport="successImport"></import-dailog>
    </div>
</template>

<script>
    import importDailog from '../importPage/importDailog.vue'
    import btns from '../elbuttonS.vue'
    import seachs from '../seachGroup.vue'
    import ExcelJS from 'exceljs'
    import fileSave from 'file-saver'
    import addIcon from '../../assets/btnIcon/add.png'
    import saveIcon from '../../assets/btnIcon/save.png'
    import deleteIcon from '../../assets/btnIcon/delete.png'
    import exportIcon from '../../assets/btnIcon/export.png'
    import importIcon from '../../assets/btnIcon/import.png'
    import {
        apiGetorganizationratinglist,
        apiGetorganizationratingsave,
        apiGetOrganizationListAll,
        apiGetbinsectionList,
        apiPostorganizationratingDelete,
        apiPostIsdoAction,
        apiGetorganizationyearratingHeader
    } from '../../api/api'
    export default {
        components: {
            importDailog,
            btns,
            seachs
        },
        data() {
            return {
                dialogVisible: false,
                ratingdialogVisible: false,
                isSave: false,
                title: '',
                descMsg: '',
                evalradio: {},
                addIcon: addIcon,
                saveIcon: saveIcon,
                deleteIcon: deleteIcon,
                exportIcon: exportIcon,
                importIcon: importIcon,
                changeDateSelect: [],
                evaluationRatingDiaData: [],
                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'
                    }]
                },
                page: {
                    pagesize: 12,
                    currentPage: 1,
                    total: 0
                },
                receiptMsg: {
                    code: '',
                    des: '',
                    stage: '',
                    createName: '',
                    createDate: '',
                    year: '',
                    recStage: ''
                },
                resCompanyName: '',
                businessStr: '',
                monthArr: [],
                ratingArr: [],
                winPix: window.devicePixelRatio,
                innerWidth: window.innerWidth,
                rowObj: {},
                instanceChild: {},
                rowkeyId: ''
            }
        },
        created() {
            this.ratingArr = [{
                    label: 'A级',
                    value: 'A'
                },
                {
                    label: 'B级',
                    value: 'B'
                },
                {
                    label: 'C级',
                    value: 'C'
                }
            ]
        },
        methods: {
            init(row, keyId, allData) {
                this.ratingdialogVisible = true
                this.rowObj = row
                this.rowkeyId = keyId
                this.instanceChild = allData
                this.getevaluRatingDiaList(row, keyId)
                this.getUnitRatingHeader(keyId)
                this.getOrganizetionData()
                this.getDataDictionary()
            },
            // 查询头部信息
            getUnitRatingHeader(id) {
                let that = this
                let params = {
                    id: id
                }
                apiGetorganizationyearratingHeader(params).then(datas => {
                    if (datas && datas.data) {
                        let item = datas.data
                        that.receiptMsg = {
                            code: item.yearRatingCode,
                            des: item.des,
                            stage: item.stage,
                            createName: item.createName,
                            createDate: item.createTime,
                            year: item.year,
                            recStage: '有效'
                        }
                    }
                })
            },
            // 查询列表页面
            getevaluRatingDiaList(row, id) {
                let that = this
                let params = {
                    pageNum: that.page.currentPage,
                    pageSize: that.page.pagesize,
                    organizationYearRatingId: id,
                    organizationId: that.resCompanyName,
                    binSection: that.businessStr,
                    year: row.year
                }
                apiGetorganizationratinglist(params).then(datas => {
                    if (datas && datas.data.records) {
                        that.evaluationRatingDiaData = datas.data.records
                        that.page.total = datas.data.total
                    }
                })
            },
            //查询单位数据
            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.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
                    }
                })
            },
            examineAndApprove(type) {
                let that = this
                let actionCS = ''
                if (type === 'agree') {
                    if (that.rowObj.taskType === 'SIGN') {
                        actionCS = 'signAgree'
                    } else if (that.rowObj.taskType === 'NORMAL') {
                        actionCS = 'agree'
                    }
                } else {
                    if (that.rowObj.taskType === 'SIGN') {
                        actionCS = 'signReject'
                    } else if (that.rowObj.taskType === 'NORMAL') {
                        actionCS = 'reject'
                    }
                }
                let params = {
                    taskId: that.rowObj.id, //任务ID
                    nodeId: that.rowObj.nodeId, //节点ID
                    action: actionCS, //固定值
                    instanceId: that.rowObj.instId, //流程实例ID
                    opinion: that.descMsg, //审批意见
                    iamCode: window.localStorage.getItem('code'), //认证后code值
                    taskName: that.rowObj.name,
                    defKey: that.instanceChild.defKey,
                    bizKey: that.instanceChild.bizKey
                }
                apiPostIsdoAction(params).then(datas => {
                    if (datas && datas.data) {
                        if (datas.data.isOk) {
                            that.$message({
                                message: datas.data.data,
                                type: 'success'
                            })
                            that.ratingdialogVisible = false
                            that.$emit('approveMsg', datas.data.isOk)
                        } else {
                            that.$message({
                                message: datas.data.msg,
                                type: 'error'
                            })
                        }
                    }
                })
            },
            getSeachData() {
                this.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
            },
            resetSeach() {
                this.resCompanyName = ''
                this.businessStr = ''
                this.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
            },
            handleSizeChange(val) {
                this.page.pagesize = val
                this.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
            },
            handleCurrentChange(val) {
                this.page.currentPage = val
                this.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
            },
            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.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
                        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.getevaluRatingDiaList(that.rowObj, that.rowkeyId)
            },
            handleExport() {
                let data = this.evaluationRatingDiaData //接口返回数据
                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`)
                })
            },
        }
    }
</script>

<style lang="less">
    .taskUnitRatingDetail {
        .el-overlay {
            .el-dialog {

                // margin-top: 7vh;
                .el-dialog__body {
                    padding: 0 20px !important;

                    .taskUnit {
                        .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;

                                .data_tit {
                                    margin-right: 10px;
                                    font-weight: bold;
                                    font-size: 14px;
                                    font-family: Microsoft YaHei;
                                    color: #8991B0;
                                }

                                .data_tit_wd {
                                    display: inline-block;
                                    width: 90px;
                                }

                                .el-form-item--small {
                                    .el-input {
                                        height: 25px;
                                        width: 160px;
                                    }

                                    margin-bottom: 0;

                                    .el-input-number {
                                        height: 25px;

                                        .el-input-number__decrease,
                                        .el-input-number__increase {
                                            right: -39px;
                                        }
                                    }
                                }
                            }
                        }

                        .evaluationUnitRatingBtn {
                            .headerRight {
                                display: flex;
                                justify-content: flex-end;
                                width: 100%;
                                margin: 15px 0;

                                .seachFor {
                                    display: flex;

                                    .el-input {
                                        height: 30px;
                                        margin-right: 5px;
                                        width: 150px;

                                        .el-input__inner {
                                            height: 30px;
                                        }

                                        // .el-input__suffix{
                                        //     .el-select__caret{
                                        //         line-height:30px;
                                        //     }
                                        // }
                                    }

                                    .el-icon {
                                        margin: 5px 10px 0 10px;
                                        cursor: pointer;
                                    }
                                }

                                .tableBtn {
                                    display: flex;
                                    height: 20px;
                                    margin: 5px 8px 0 8px;
                                    // width: 20%;
                                    width: fit-content;

                                    img {
                                        margin-right: 5px;
                                        margin-top: 1px;
                                    }

                                    span {
                                        display: inline-block;
                                        width: 35px;
                                        font-size: 14px;
                                        font-family: Microsoft YaHei;
                                        font-weight: 400;
                                    }
                                }

                                .add {
                                    cursor: pointer;

                                    span {
                                        color: #3B7AD1;
                                    }
                                }

                                .save {
                                    cursor: pointer;

                                    span {
                                        color: #50C14E;
                                    }
                                }

                                .import {
                                    cursor: pointer;

                                    span {
                                        color: #ce1e78;
                                    }
                                }

                                .export {
                                    cursor: pointer;

                                    span {
                                        color: #2baa8a;
                                    }
                                }
                            }

                            .evaluationUnitRatingTableData {
                                .el-table {
                                    .el-table__body-wrapper {
                                        height: 40vh !important;
                                    }

                                    .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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                .el-dialog__footer {
                    .dialog-footer {
                        display: flex;
                        justify-content: center;

                        .el-button {
                            width: 180px !important;
                            height: 40px !important;
                        }
                    }
                }
            }
        }

    }
</style>