<template>
    <div class="evaluationDepartment" v-loading="loadingImport">
        <div class="evaluationDepartmentBtn">
            <div class="collectSeach">
                <div class="exceed">
                    <span class="exceedSpan">部门名称：</span>
                    <el-input v-model="departmentS" placeholder="请输入部门名称"></el-input>
                </div>
                <seachs @handleSeach="getSeachData" @handleRest="resetSeach"></seachs>
            </div>
            <div class="PeriodBtn">
                <!-- $utils.havePurview('evalDepartment:oneLevel:add')
                $utils.havePurview('evalDepartment:oneLevel:save')
                $utils.havePurview('evalDepartment:oneLevel:delete') -->
                <btns
                :showImport="false"
                :showExport="false"
                :showAdd="true"
                :showSave="true"
                :showDelete="true"
                :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="evaluationDepartmentTableData">
                <el-table :data="evaluationDepartmentData" style="width: 100%" @select="rowClick" @select-all="rowClick">
                    <el-table-column type="selection" label="操作" align="center"></el-table-column>
                    <el-table-column label="部门编码" prop="shortDeptName" />
                    <el-table-column label="部门名称" prop="deptName"  width="300" />
                    <!-- <el-table-column label="部门简称" prop="shortDeptName" /> -->
                    <el-table-column label="考评类型" >
                        <template #default="scope">
                                <span>{{scope.row.evaluationCategory === 'BMKP'?'部门考评':''}}</span>
                            </template>
                        </el-table-column>
                    <el-table-column label="指标名称" prop="indicatorDatasAll" width="400" />
                    <el-table-column label="年" prop="annual" />
                    <el-table-column label="描述" prop="des" />
                    <el-table-column label="创建时间" prop="createTime" />
                </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="70vw" :close-on-click-modal="false">
                    <el-row class="periodFrom">
                        <el-col :span="8">
                            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="DruleForm" :validate-on-rule-change="false">
                                <el-form-item label="考评部门" prop="department">
                                    <el-select v-model="ruleForm.department" placeholder="请选择所属部门" :disabled="isSave">
                                        <el-option
                                        v-for="item in departData"
                                        :key="item.id"
                                        :label="item.keyName"
                                        :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="考评类别" prop="indicatorType">
                                    <el-select v-model="ruleForm.indicatorType" placeholder="请选择指标类别" disabled>
                                        <el-option
                                        v-for="item in indicatorTypeData"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                        </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="指标顺序" prop="orderNum">
                                    <el-input-number v-model="ruleForm.orderNum" :min="1" />
                                </el-form-item> -->
                                <el-form-item label="描述" prop="desc">
                                    <el-input v-model="ruleForm.desc" :rows="5" type="textarea" placeholder="请输入描述"></el-input>
                                </el-form-item>
                            </el-form>
                        </el-col>
                        <el-col :span="16" class="indicatorItemBtn">
                            <div class="PeriodBtnDia">
                                <p class="indItemC">指标项配置管理</p>
                                <el-button
                                type="primary"
                                size="mini"
                                @click="handleAddChild"
                                >
                                <img :src="addW" style="margin-right: 10px">
                                <span>新增</span>
                                </el-button>
                            </div>
                            <div class="indicatorItemTableData">
                                <div class="datasMsg" v-if="indicatoroptions.length>0" >
                                    <el-collapse accordion>
                                        <el-collapse-item v-for="item in indicatoroptions" :key="item" :name="item.id">
                                            <template #title>
                                                <div style="display:flex;height:30px">
                                                    <div style="display:flex;height:30px">
                                                        <span class="nameTit">指标名称：</span>
                                                        <el-input v-model="item.indicatorName" placeholder="请输入子指标项名称"></el-input>
                                                    </div>
                                                    <div style="display:flex">
                                                        <span class="nameTit">指标编码：</span>
                                                        <el-input v-model="item.indicatorCode" placeholder="请输入子指标项编码"></el-input>
                                                    </div>
                                                </div>
                                            </template>
                                            <div style="padding:10px 20px;max-height: 50vh;overflow-y: auto;">
                                                <div class="tableBtn add" @click="handleAddTableChild(item)">
                                                    <img :src="addIcon" alt="">
                                                    <span>新增</span>
                                                </div>
                                                <el-table :data="item.evaluationDeptIndicatorItemList">
                                                    <el-table-column label="序号" type="index"  align="center" />
                                                    <el-table-column label="指标项名称" prop="indicatorItemName">
                                                        <template #default="scope">
                                                            <!-- <el-select v-model="scope.row.indicatorItemName" filterable placeholder="请选择指标项名称" @change="(val)=>changeOptionName(val,scope.row)">
                                                                <el-option
                                                                    v-for="item in indicItemoptions"
                                                                    :key="item.keyValue"
                                                                    :label="item.keyName"
                                                                    :value="item.keyName">
                                                                </el-option>
                                                            </el-select> -->
                                                            <el-input v-model="scope.row.indicatorItemName" placeholder="请输入指标项名称"></el-input>
                                                        </template>
                                                    </el-table-column>
                                                    <el-table-column label="指标项编码" prop="indicatorItemCode">
                                                        <template #default="scope">
                                                            <!-- <span>{{scope.row.indicatorItemCode}}</span> -->
                                                            <el-input v-model="scope.row.indicatorItemCode" placeholder="请输入指标项编码"></el-input>
                                                        </template>
                                                    </el-table-column>
                                                    <el-table-column label="指标项类别" prop="indicatorCategory">
                                                        <template #default="scope">
                                                            <el-select v-model="scope.row.indicatorCategory" placeholder="请选择指标项类别">
                                                                <el-option
                                                                    v-for="item in indicatorCategorys"
                                                                    :key="item.value"
                                                                    :label="item.label"
                                                                    :value="item.value">
                                                                </el-option>
                                                            </el-select>
                                                        </template>
                                                    </el-table-column>
                                                    <el-table-column label="描述">
                                                        <template #default="scope">
                                                            <el-input v-model="scope.row.des" :rows="1" type="textarea" placeholder="请输入描述"></el-input>
                                                        </template>
                                                    </el-table-column>
                                                    <el-table-column label="操作" width="100">
                                                        <template #default="scope">
                                                            <p class="indicitem" @click="handleDeleteTableChild(item, scope.row)">删除</p>
                                                        </template>
                                                    </el-table-column>
                                                </el-table>
                                            </div>
                                        </el-collapse-item>
                                    </el-collapse>
                                </div>
                                <div v-else>
                                    <span class="emptyData">暂无数据</span>
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                    <template #footer>
                        <span class="dialog-footer">
                            <el-button @click="dialogVisible = false">取 消</el-button>
                            <el-button type="primary" @click="saveevaluationDepartmentMsg('ruleForm')">确 定</el-button>
                        </span>
                    </template>
                </el-dialog>
            </div>
        </div>
        <import-dailog ref="importPage" @successImport="successImport" @importLoading="importLoading"></import-dailog>
    </div>
</template>

<script>
import importDailog from '../importPage/importDailog.vue'
import btns from '../elbuttonS.vue'
import seachs from '../seachGroup.vue'
import addW from '../../assets/btnIcon/addW.png'
import addIcon from '../../assets/btnIcon/add.png'
import {apiGetEvaluationDeptList, apiGetevaluationdeptSave, apiGetevaluationdeptremoveAll, apiGetdataZBXList,
apiGetdatadictionaryList, apiGetevaluationdeptremoveItem} from '../../api/api'
export default {
  components: { importDailog, btns, seachs },
    data() {
        return {
            dialogVisible: false,
            title: '',
            isSave: false,
            changeDateSelect: [],
            indicatoroptions: [],
            departmentS: '',
            evaluationDepartmentData:[],
            departData: [],
            addW: addW,
            addIcon: addIcon,
            indicItemoptions: [],
            indicatorTypeData: [],
            ruleForm: {
                department: '',
                indicatorType: 'BMKP',
                year: '',
                desc: ''
            },
            rules: {
                department: [
                    { required: true, message: '请选择部门名称', trigger: 'change' }
                ],
                indicatorType: [
                    { required: true, message: '请选择考评类别', trigger: 'change' }
                ],
                year: [
                    { required: true, message: '请选择年份', trigger: 'change' }
                ],
                desc: [
                    { required: true, message: '请输入描述', trigger: 'blur' }
                ]
            },
            page:{
                pagesize: 12,
                currentPage: 1,
                total: 0
            },
            evalradio: {},
            loadingImport: false,
        }
    },
    created() {
        this.getEvalCompanyData()
        this.getindicItemData()
        this.getDepartmentFromTreeData()
        this.indicatorCategorys = [
            {
                label: '指标计划',
                value: '指标计划'
            },
            {
                label: '指标结果',
                value: '指标结果'
            }
        ]
        this.indicatorTypeData = [
            {
                label: '部门考评',
                value: 'BMKP'
            }
        ]
    },
    methods:{
        // 查询部门列表数据
        getEvalCompanyData() {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize,
                deptName: that.departmentS
            }
            apiGetEvaluationDeptList(params).then(datas =>{
                if (datas && datas.data) {
                    for(let i =0; i<datas.data.records.length; i++) {
                        let item = datas.data.records[i]
                        let indicatorDatasAll = ''
                        if (item.evaluationDeptIndicatorList.length>0) {
                            for(let j =0; j<item.evaluationDeptIndicatorList.length; j++) {
                                let iten = item.evaluationDeptIndicatorList[j]
                                indicatorDatasAll += iten.indicatorName + '; '
                            }
                        }
                        item.indicatorDatasAll = indicatorDatasAll
                    }
                    that.evaluationDepartmentData = datas.data.records
                    that.page.total = datas.data.total
                }
            })
        },
        // 查询部门
        getDepartmentFromTreeData() {
            let that = this
            let params = {
                superKey: 'BM0001'
            }
            apiGetdatadictionaryList(params).then(datas =>{
                if (datas && datas.data) {
                    that.departData = datas.data
                }
            })
        },
        // 查询指标项条目
        getindicItemData() {
            let that = this
            let params = {
                superKey: 'ZBX0001'
            }
            apiGetdataZBXList(params).then(datas =>{
                if (datas && datas.data) {
                    that.indicItemoptions = datas.data
                }
            })
        },
        handleAdd() {
            this.dialogVisible = true
            this.isSave = false
            this.title = '新增考评部门'
            this.$nextTick(() =>{
                // this.$refs['ruleForm'].resetFields()
                this.ruleForm = {
                    department: '',
                    indicatorType: 'BMKP',
                    year: '',
                    desc: ''
                }
                this.indicatoroptions = []
            })
        },
        //新增子指标
        handleAddChild() {
            this.$nextTick(() =>{
                let itemForm = {
                    indicatorName: '输入子指标名称',
                    indicatorCode: '输入子指标编码',
                    evaluationDeptIndicatorItemList: []
                }
                this.indicatoroptions.push(itemForm)
            })
        },
        changeOptionName(val, row) {
            this.indicItemoptions.forEach(item =>{
                if (item.keyName === val) {
                    row.indicatorItemCode = item.keyValue
                }
            })
        },
        //新增子指标
        handleAddTableChild(row) {
            let childitemForm = {
                indicatorItemName: '',
                indicatorItemCode: ''
            }
            row.evaluationDeptIndicatorItemList.push(childitemForm)
        },
        handleEdit() {
            this.dialogVisible = true
            this.isSave = true
            this.title = '修改考评部门'
            this.evalradio = this.changeDateSelect[0]
            this.ruleForm = {
                department: this.evalradio.deptId,
                indicatorType: this.evalradio.evaluationCategory,
                year: this.evalradio.annual,
                desc: this.evalradio.des
            }
            this.indicatoroptions = this.evalradio.evaluationDeptIndicatorList
        },
        saveevaluationDepartmentMsg(formName) {
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    that.saveAndEditIndicatorData()
                }
            });
        },
        //新增/修改指标数据
        saveAndEditIndicatorData() {
            let that = this
            let orgName = ''
            let shortName = ''
            that.departData.forEach(item =>{
                if (item.id === that.ruleForm.department) {
                    orgName = item.keyName
                    shortName = item.dataKey
                }
            })
            let params = {
                deptName: orgName,
                shortDeptName: shortName,
                deptId: that.ruleForm.department,
                evaluationCategory: 'BMKP',
                annual: that.ruleForm.year,
                monthly: that.ruleForm.month,
                des: that.ruleForm.desc,
                evaluationDeptIndicatorList: that.indicatoroptions
            }
            if (that.isSave) {
                params.id = that.evalradio.id
            }
            apiGetevaluationdeptSave(params).then(datas =>{
                if (!datas.success) {
                    that.$message({
                        message: datas.message,
                        type: 'error'
                    });
                } else {
                    if (!that.isSave) {
                        that.$message({
                            message: '考评部门新增成功',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            message: '考评部门修改成功',
                            type: 'success'
                        });
                    }
                    that.dialogVisible = false
                    that.changeDateSelect = []
                    that.getEvalCompanyData()
                }
            })
        },
        //删除部门数据
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let paramsArr = []
                that.changeDateSelect.forEach(it =>{
                    paramsArr.push(it.id)
                })
                apiGetevaluationdeptremoveAll(paramsArr.join(',')).then(datas =>{
                    if (datas) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        that.changeDateSelect = []
                        that.getEvalCompanyData()
                    }
                })
            })
        },
        //删除指标项数据
        handleDeleteTableChild(data, row) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                if (data.evaluationDeptIndicatorItemList && data.evaluationDeptIndicatorItemList.length>0) {
                    data.evaluationDeptIndicatorItemList.forEach((it,index) =>{
                        if (it.indicatorItemCode === row.indicatorItemCode) {
                            data.evaluationDeptIndicatorItemList.splice(index, 1)
                            if (row.id) {
                                apiGetevaluationdeptremoveItem(row.id).then(datas =>{
                                    that.$message({
                                        type: 'success',
                                        message: '删除成功!'
                                    });
                                })
                            }
                        }
                    })
                }
            })
        }, 
        rowClick(selection, row) {
            this.changeDateSelect = selection
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getEvalCompanyData()
        },
        resetSeach() {
            this.page.currentPage = 1
            this.departmentS = ''
            this.getEvalCompanyData()
        },
        successImport(val) {
            this.loadingImport = false
            this.getEvalCompanyData()
        },
        importLoading(val) {
            this.loadingImport = true
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getEvalCompanyData()
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getEvalCompanyData()
        },
        handleImport() {
            this.$refs.importPage.upload.title = "考评指标管理信息导入"
            this.$refs.importPage.upload.open = true
        },
    }
}
</script>

<style lang="less">
.evaluationDepartment{
  .evaluationDepartmentBtn{
        .collectSeach{
            display: flex;
            padding: 24px 20px;
            border-bottom: 1px solid#D6DBEA;
                .exceed{
                    display: flex;
                    .exceedSpan{
                        width: 100px;
                        height: 12px;
                        font-size: 14px;
                        font-family: Microsoft YaHei;
                        font-weight: 400;
                        color: #8991B0;
                        line-height: 12px;
                        margin-top: 14px;
                    }
                    .el-input{
                        margin-right:10px;
                        .el-input__inner{
                            height:30px;
                        }
                        .el-input__suffix{
                            .el-select__caret{
                                line-height:30px;
                            }
                        }
                    }
                }
                .el-select{
                    margin-right:10px;
                    .el-input__inner{
                        height:30px;
                    }
                    .el-input__suffix{
                        .el-select__caret{
                            line-height:30px;
                        }
                    }
                }
        }
        span{
            font-size:14px;
        }
        .PeriodBtn{
            display: flex;
            justify-content: 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: 10vh;
                    .el-dialog__body{
                        padding: 0px 20px 0px 20px !important;
                        border-top: 1px solid #D6DBEA;
                        border-bottom: 1px solid #D6DBEA;
                        .periodFrom{
                            
                            .DruleForm{
                                padding: 20px 60px 20px 20px;
                                .el-select, .el-input{
                                    width: 100%;
                                }
                                .el-date-editor{
                                    height:30px !important;
                                }
                                .el-select{
                                    .el-input__inner, .is-disabled{
                                        height:30px !important;
                                    }
                                }
                            }
                            .indicatorItemBtn{
                                padding: 10px 20px 0 20px;
                                border-left: 1px solid #D6DBEA;
                                span{
                                    font-size:14px;
                                }
                                .PeriodBtnDia{
                                    display: flex;
                                    justify-content: space-between;
                                    padding: 20px 0;
                                    .indItemC{
                                        position: relative;
                                        // left: -600px;
                                        top: 15px;
                                        font-size: 16px;
                                        font-family: Microsoft YaHei;
                                        font-weight: 600;
                                        color: #3B7AD1;
                                    }
                                    .el-button{
                                        height: 30px;
                                        padding: 0 30px ;
                                        span{
                                            margin:0;
                                        }
                                    }
                                    .is-disabled{
                                        opacity: 0.5;
                                    }
                                }
                                .indicatorItemTableData{
                                    height: 55vh !important;
                                    overflow-y: auto;
                                    .datasMsg{
                                        .tableBtn {
                                            display: flex;
                                            justify-content: end;
                                            margin-right: 30px;
                                            img{
                                                margin-right: 5px;
                                                margin-top: 1px;
                                                width: 18px;
                                                height: 18px;
                                            }
                                            span{
                                                position: relative;
                                                top: -2px;
                                                font-size: 14px;
                                                font-family: Microsoft YaHei;
                                                font-weight: 400;
                                            }
                                        }
                                        .add{
                                            cursor: pointer;
                                            span{
                                                color: #3B7AD1;
                                            }
                                        }
                                        .el-collapse{
                                            .el-collapse-item{
                                                .el-collapse-item__header{
                                                    .nameTit{
                                                        display: inline-block;
                                                        height: 20px;
                                                        font-size: 12px;
                                                        font-weight: bold;
                                                        position: relative;
                                                        top: -8px;
                                                    }
                                                    .el-input{
                                                        width: 150px;
                                                        margin-right: 10px;
                                                    }
                                                }
                                                .el-table{
                                                    .el-table__body-wrapper{
                                                    }
                                                    .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;
                                                }
                                                .el-button{
                                                    height: 26px;
                                                    padding: 0 15px ;
                                                    span{
                                                        margin:0;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    .emptyData{
                                        display: inline-block;
                                        width: 100%;
                                        // border: 1px solid #d9d9d9;
                                        text-align: center;
                                        margin-top: 50px;
                                    }
                                }
                            }
                        }
                    }
                    .el-dialog__footer{
                        .dialog-footer{
                            display: flex;
                            justify-content: center;
                            .el-button{
                                width: 180px !important;
                                height: 40px !important;
                            }
                        }
                    }
                }
            }
        }
    }
  .evaluationDepartmentTableData{
    .el-table{
        .el-table__body-wrapper{
            height: 60vh !important;
        }
        .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>