<template>
    <section>
        <!--列表-->
        <el-table :data="list" ref="multipleTable" v-if="showTable" highlight-current-row v-loading="listLoading" :height="tableHeight" style="width: 100%;"
             :default-expand-all="defaultExpandAllFlg" >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column type="expand" :label="''">
                <!-- <template slot-scope="props">
                    <el-timeline>
                    
                    </el-timeline>
                </template> -->
            </el-table-column>

            <el-table-column prop="projectName" :label="'项目名称'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span>
                            {{scope.row.projectName}}
                        </span>
                    </div>
                </template>
            </el-table-column>

            <el-table-column prop="groupName" :label="'分组名称'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span>
                            {{scope.row.groupName}}
                        </span>
                    </div>
                </template>
            </el-table-column>
            
            <el-table-column prop="creator" :label="'提交人'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span v-if="user.userNameNeedTranslate == '1'">
                            <ww-open-data type='userName' :openid='scope.row.creator'></ww-open-data>
                        </span>
                        <span v-else>
                            {{scope.row.creator}}
                        </span>
                    </div>
                </template>
            </el-table-column>

            <el-table-column prop="createTime" :label="'提交时间'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span>
                            {{scope.row.createTime}}
                        </span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="oldManDay" :label="'变更前预估工时'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span>
                            {{scope.row.oldManDay}}
                        </span>
                    </div>
                </template>
            </el-table-column>

            <el-table-column prop="changeManDay" :label="'预估工时变更'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span>
                            {{scope.row.changeManDay}}
                        </span>
                    </div>
                </template>
            </el-table-column>

            <el-table-column prop="nowManDay" :label="'变更后预估工时'" sortable>
                <template slot-scope="scope">
                    <div>
                        <span>
                            {{scope.row.nowManDay}}
                        </span>
                    </div>
                </template>
            </el-table-column>
            
            <el-table-column prop="status" :label="$t('state.states')" sortable>
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 0" style="color:#DAA520;">{{ '待审核'}}</span>
                    <span v-else-if="scope.row.status == 1" style="color:#32CD32;">{{ $t('state.alreadyPassed') }}</span>
                    <span v-else-if="scope.row.status == 2" style="color:#FF0000;">{{ $t('state.rejected') }}</span>
                </template>
            </el-table-column>
            <el-table-column :label="$t('operation')" width="220">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.status==0"  type="primary" :loading="logining" size="small" @click="review(scope.row.id,1)">{{ $t('btn.through') }}</el-button>
                    <el-button v-if="scope.row.status==0"  type="danger" :loading="logining" size="small" @click="review(scope.row.id,2)">{{ $t('btn.rejected') }}</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <!-- <el-col v-if="search.value != -1" :span="24" class="toolbar">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[20 , 50 , 80 , 100]"
                :page-size="20"
                layout="total, sizes, prev, pager, next"
                :total="total"
                style="float:right;"
            ></el-pagination>
        </el-col> -->
    </section>
</template>

<script>
    import util from "../../common/js/util";

    // 引入自定义组件
    import selectCat from "@/components/select.vue"

    // 引入自定义级联组件
    import vueCascader from "@/components/cascader.vue"
    import cascaderOption from "@/components/cascaderOption.vue"

    export default {
        components: {
            selectCat,
            vueCascader,
            cascaderOption
        },
        data() {
            return {
                yuzhongCompId: 3385,
                roleList:[{value: 1,label: 'CRC&LM'},{value: 2,label: 'PM'}],
                batchDenyLoading: false,
                batchDenyDialog: false,
                batchDenyData: {ids:'',reason:''},
                batchApproveLoading: false,
                denyForm:null,
                denyReasonDialog:false,
                isAllSelect:false,
                user: JSON.parse(sessionStorage.getItem("user")),
                permissions: JSON.parse(sessionStorage.getItem("permissions")),
                search: {
                    projectId:null,
                    departmentIdArray: null,
                    departmentId:null,
                    // date: null,
                    startDate: null,
                    endDate: null,
                    state:0,
                    userId: null,
                    userIdArray: []
                },

                users: [],
                option:[],
                tableHeight: 0,
                listLoading: false,
                total: 0,
                page: 1,
                size: 20,
                list: [],
                logining: false,
                multipleSelection: [],
                usersList: [],
                searchUsersList: [],
                dataTime: [],
                recordDialogVisible: false,
                recordLists: [],
                totals: 0,
                pageIndexList: 1,
                pageSizeList: 20,
                undoForm: {
                    // reason: '',
                    // userId: '',
                    // createDate: ''
                },
                undoFormDialog: false,
                detailsDialog: false,
                idx: 0, // 详情索引
                detailsList: [],
                undoFormLoading: false,

                approveinData: null,
                approveinDialog: false,
                isbatch: false,
                defaultExpandAllFlg: false,
                showTable: true
            };
        },
        filters: {
            // 过滤
            amounts(value) {
                var zhi = +value + 0
                return zhi.toFixed(1)
            }
        },
        methods: {
            viewOneReport(r) {
                this.http.post("/report/getAuditWorkflowList", {reportId:r.id},
                    res => {
                        if (res.code == "ok") {
                            this.$set(r,'auditorList', res.data);
                        } 
                    },
                    error => {
                        this.undoFormLoading = false
                        this.$message({
                            message: error,
                            type: "error"
                        });
                    });
            },
            expandChange(row, expandedRows) {
                var reportList = row.data;
                for (var i=0;i<reportList.length; i++) {
                    var r = reportList[i];
                    if (r.auditorList) continue;
                    this.http.post("/report/getAuditWorkflowList", {reportId:r.id},
                    res => {
                        if (res.code == "ok") {
                            this.$set(r,'auditorList', res.data);
                        } 
                    },
                    error => {
                        this.undoFormLoading = false
                        this.$message({
                            message: error,
                            type: "error"
                        });
                    });
                }
            },
            detailsClick(item, i) {
                this.detailsDialog = true
                this.detailsList = item.membdateList
                this.idx = i
            },
            // 审核记录撤销点击确定
            clickCancel() {
                this.undoFormLoading = true
                this.http.post('/report/denyHisReport', this.undoForm,
                res => {
                    this.undoFormLoading = false
                    if (res.code == "ok") {
                        this.$message({
                            message: this.$t('Revocationofsuccess'),
                            type: "success"
                        });
                        this.undoFormDialog = false
                        this.recordList()
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.undoFormLoading = false
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            undoCli(item, i) {
                // console.log(item)
                this.undoFormDialog = true
                this.undoForm = {reason: ''}
                // this.undoForm.reason = ''
                if(i){
                    this.undoForm.hisId = item.id
                }else{
                    this.undoForm.hisId = item.membdateList[0].id
                }
                
                this.ioss = i
                // if(i == 0) {    
                //     this.undoForm.createDate = item.indate.split(' ')[0]
                //     this.undoForm.userId = item.userId
                // } else {
                //     this.undoForm.hisId = item.id
                // }
            },
            // 获取审核记录
            recordList() {
                // this.recordDialogVisible = true
                // return
                this.http.post( '/report-audit-log/getProjectReportAuditLog', {
                    companyId: this.user.companyId,
                    pageIndex: this.pageIndexList,
                    pageSize: this.pageSizeList
                },
                res => {
                    if (res.code == "ok") {
                        for (var i in res.data.records) {
                            res.data.records[i].result.indexOf(this.$t('btn.through')) == '-1' ? res.data.records[i].flg = false : res.data.records[i].flg = true
                        }
                        this.recordLists = res.data.records
                        this.totals = res.data.total
                        if(this.recordLists.length != 0){
                            this.detailsList = this.recordLists[this.idx].membdateList
                        }
                        
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            // 获取部门列表
            getDepartment() {
                this.http.post( this.port.manage.depList, {},
                res => {
                    if (res.code == "ok") {
                        var list1 = JSON.parse(JSON.stringify(res.data));
                        
                        this.option = this.changeArr(list1);
                        console.log(this.option, '部门')
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            batchApprove(isPass) {
                var ids = '';
                for (var i=0;i<this.multipleSelection.length; i++) {
                    var line = this.multipleSelection[i];
                    var array = line.data;
                    if (this.user.company.companyName == '成都明夷电子科技有限公司') {
                        ids += line.reportIds+',';
                    } else {
                        for (var m=0;m<array.length; m++) {
                            ids += array[m].id+',';
                        }
                    }
                }
                if (ids.length > 0) {
                    ids = ids.substring(0, ids.length-1);
                }
                //等待
                if(isPass){
                    this.isbatch = true
                    this.approveinData = {
                        ids: ids
                    }
                    if(this.user.timeType.needEvaluate == 1){
                        this.$set(this.approveinData,'evaluate','')
                        this.approveinDialog = true
                    }else{
                        this.batchApproveLoading = true
                        this.listLoading = true;
                        this.batchApproveinfun()
                    }
                }else{
                    this.batchDenyDialog = true
                    this.batchDenyData.ids = ids
                    this.batchDenyData.reason = ''
                }
            },
            batchApproveinfun(){
                this.http.post('/report/batchApproveReport', this.approveinData,
                    res => {
                        this.batchApproveLoading = false
                        this.listLoading = false;
                        if (res.code == "ok") {
                            this.approveinDialog = false
                            this.getList();
                        } else {
                            this.$message({
                            message: res.msg,
                            type: "error"
                            });
                        }
                    },
                    error => {
                        this.listLoading = false;
                        this.$message({
                            message: error,
                            type: "error"
                        });
                    });
            },
            batchDenyClick(){
                this.batchDenyLoading = true
                    this.listLoading = true;
                    this.http.post('/report/batchDenyReport', this.batchDenyData,
                    res => {
                        this.batchDenyLoading = false;
                        this.batchDenyDialog = false
                        this.listLoading = false;
                        if (res.code == "ok") {
                            this.getList();
                        } else {
                            this.$message({
                            message: res.msg,
                            type: "error"
                            });
                        }
                    },
                    error => {
                        this.listLoading = false;
                        this.$message({
                            message: error,
                            type: "error"
                        });
                    });
            },
            //分页
            handleCurrentChange(val) {
                this.page = val;
                this.getList();
            },

            handleSizeChange(val) {
                this.size = val;
                this.getList();
            },

            //分页
            handleCurrentChangeList(val) {
                this.pageIndexList = val;
                this.recordList();
            },

            handleSizeChangeList(val) {
                this.pageSizeList = val;
                this.recordList();
            },

            test(){
                console.log(this.search.userId);
            },
            searchUserIds(deptId){
                this.searchUsersList = this.usersList.filter(item => deptId == item.departmentId)
            },
            usersSearch(e){
                if(e == false){
                    this.getList()
                }
            },
            //获取待审核的数据列表
            getList(e) {
                this.listLoading = true;
                this.http.post("/group-budget-review/list",{},
                res => {
                    this.listLoading = false;
                    if (res.code == "ok") {
                        this.list = res.data;
                    } else {
                        this.$message({
                        message: res.msg,
                        type: "error"
                        });
                    }
                },
                error => {
                    this.listLoading = false;
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },

            review(id,checkType){
                this.http.post("/group-budget-review/check", {id:id,checkType:checkType},
                res => {
                    if (res.code == "ok") {
                        this.$message({
                            message:"操作成功",
                            type: "success"
                        });
                        this.getList();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },

            approveinfun(){
                this.http.post(this.port.report.approve, this.approveinData,
                res => {
                    this.logining = false;
                    if (res.code == "ok") {
                        this.approveinDialog = false
                        this.$message({
                            message: this.$t('message.Reviewsucceeded'),
                            type: "success"
                        });
                        this.getList();
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.logining = false;
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
// 修改数组
            changeArr(arr) {
                for (var i = 0; i < arr.length; i++) {
                    if(arr[i].id != -1 && arr[i].id != 0) {
                        if (arr[i].children != null && arr[i].children.length>0) {
                            arr[i].children = this.changeArr(arr[i].children);
                        }
                        arr[i].id && (arr[i].value = arr[i].id);
                        delete arr[i].id;
                    }
                }
                for(var i in arr) {
                    if(arr[i].id == -1 || arr[i].id == 0) {
                        arr.splice(i,1)
                    }    
                }
                return arr;
            },
            
            //获取项目列表
            getProjectList() {
                this.http.post( this.port.project.list, {},
                res => {
                    if (res.code == "ok") {
                        this.projectList = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            showDenyDialog(id,i, date, item) {
                this.denyReasonDialog = true;
                var ids = '';
                if (this.user.company.companyName == '成都明夷电子科技有限公司') {
                    ids = item.reportIds;
                } else {
                    var data = item.data;
                    data.forEach(element => {
                        ids +=(element.id+',');
                    });
                }
                
                this.denyForm = {id: id ,i:i, date: date, reportIds: ids, reason:null};
            },

            // 未通过日报
            deny() {
                this.logining = true;
                this.http.post( this.port.report.deny, this.denyForm,
                res => {
                    this.logining = false;
                    if (res.code == "ok") {
                        this.$message({
                            message: this.denyForm.i==0?this.$t('message.rejectedsuccessfully'):this.$t('Revocationofsuccess'),
                            type: "success"
                        });
                        this.getList();
                        this.denyReasonDialog = false;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.logining = false;
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            // 获取所有人员
            getUsers() {
                // this.http.post(this.port.manage.list, {
                //     departmentId: -1,
                //     pageIndex: 1,
                //     // pageSize: 99999
                //     pageSize: -1
                // },
                this.http.post('/user/getSimpleActiveUserList', {},
                res => {
                    if (res.code == "ok") {
                        this.usersList = res.data;
                        this.searchUsersList = this.usersList
                    } else {
                        this.$message({
                        message: res.msg,
                        type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
            // 选择日期后触发
            dataTimes() {
                // console.log(this.dataTime)
                if(this.dataTime){
                    this.search.startDate = this.dataTime[0]
                    this.search.endDate = this.dataTime[1]
                }else{
                    this.search.startDate = null
                    this.search.endDate = null
                }
                this.getList()
            },
            // 自定义部门选择
            vueCasader(obj) {
                console.log(obj, '组件传过来的')
                if(obj.distinction == '1') {
                    if(obj.id != '') {
                        let arr = []
                        arr.push(obj.id)
                        this.search.departmentIdArray = arr
                    } else {
                        this.search.departmentIdArray = []
                    }
                    this.getList(1)
                }
            },
            selectCal(obj) {
                console.log(obj, '过来的数据')
                // search.userIdArray
                let userListId = obj.arrUserList
                let arr = []
                for(var i in userListId) {
                    arr.push(userListId[i].id)
                }
                this.search.userIdArray = arr
                console.log(this.search.userIdArray, '数据看看')
                this.usersSearch(false)
            },
            defaultExpandAllFlgCli() {
                this.defaultExpandAllFlg = !this.defaultExpandAllFlg
                this.list = JSON.parse(JSON.stringify(this.list))
                this.$nextTick(()=>{this.$refs.multipleTable.doLayout()})
                this.showTable = false
                this.$nextTick(() => {
                    this.showTable = true
                })
            }
        },
        created() {
            let height = window.innerHeight;
            this.tableHeight = height - 125;
            const that = this;
            window.onresize = function temp() {
                that.tableHeight = window.innerHeight - 125;
            };
        },
        mounted() {
            this.getList();
            // this.getDepartment();
            // this.getProjectList();
            // this.getUsers()
        }
    };
</script>

<style lang="scss">
.propsbtn {
    display: inline-block;
    padding-left: 20px;
}

</style>