<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container" style="margin: 0;width: 100%;">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div style="overflow: hidden;">
                <div class="search" style="float: left;">
                    <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                </div>
                <div style="margin-top: 20px;float: right;" class="bigbtns">
                    <el-button style="margin-left: 20px;" class="btn-check" size="medium" type="primary"
                               @click="addNew()">新建
                    </el-button>
                </div>
            </div>
            <div class="tabbox">
                <el-table height="calc(100% - 40px)" class="com-table" ref="multipleTable" :data="tableData"
                          tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                    <el-table-column prop="appendixName" label="名称" align="center">
                    </el-table-column>
                    <el-table-column prop="opTime" label="发起时间" align="center" width="90">
                    </el-table-column>
                    <el-table-column prop="deptName" label="发起部门" align="center">
                    </el-table-column>
                    <el-table-column prop="userName" label="发起人" align="center">
                    </el-table-column>
                    <el-table-column prop="endTime" label="截止时间" align="center">
                    </el-table-column>
                    <el-table-column prop="state" label="状态" align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.state == 0">待办</span>
                            <span v-if="scope.row.state == 1">已办</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="220px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="openAppendixInfo(scope.row)" v-if="scope.row.type == '1'">
                                回复详情
                            </el-button>
                            <el-button size="mini" @click="processAppendixInfo(scope.row,1)"
                                       v-if="scope.row.type == '1'">
                                查看
                            </el-button>
                            <el-button size="mini" @click="processAppendixInfo(scope.row,1)"
                                       v-if="scope.row.type == '2'">
                                查看
                            </el-button>
                            <el-button size="mini" @click="processAppendixInfo(scope.row,2)"
                                       v-if="scope.row.type == '2' && scope.row.state =='0'">处理
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination class="pageBox" @current-change="currchange" layout="prev, pager, next" background
                               :total="total">
                </el-pagination>
            </div>
            <el-dialog title="添加" :visible.sync="addDialogStatus" width="40%" :destroy-on-close="true"
                       :modal-append-to-body="false" :close-on-click-modal="false" :fullscreen="false">
                <el-form :model="addDialogParam" ref="addInfoList">
                    <div class="info-line">
                        <el-form-item prop="testInfo">
                            <span>名称</span>
                            <el-input v-model="addDialogParam.name" placeholder="名称" v-if="!addDisabled"></el-input>
                            <label v-else>{{addDialogParam.name}}</label>
                        </el-form-item>
                    </div>
                    <div class="info-line">
                        <el-form-item prop="testInfo">
                            <span>截止日期</span>
                            <el-date-picker type="date" placeholder="截止日期" v-model="addDialogParam.endTime"
                                            :picker-options="pickerOptions" value-format="yyyy-MM-dd"
                                            v-if="!addDisabled"></el-date-picker>
                            <label v-else>{{addDialogParam.endTime}}</label>
                        </el-form-item>
                    </div>
                    <div class="info-line">
                        <el-form-item prop="testInfo">
                            <span>材料说明</span>
                            <el-input v-model="addDialogParam.remark" placeholder="材料说明"
                                      v-if="!addDisabled"></el-input>
                            <label v-else>{{addDialogParam.remark}}</label>
                        </el-form-item>
                    </div>
                    <div class="info-line" v-if="addDialogParam.type == '1'">
                        <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo"
                                  :fileList="fileInfo.fileList"></myUpload>
                    </div>
                    <div class="info-line" v-if="addDialogParam.type == '2'">
                        <span>附件材料</span><br/>
                        <uploadDown :datalist="uploadUserFileList" :dialogStatus="uploadstatus"></uploadDown>
                    </div>
                    <div class="info-line" v-if="addDialogParam.type == '2'">
                        <br/>
                        <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo"
                                  :fileList="fileInfo.fileList"></myUpload>
                    </div>
                </el-form>
                <div slot="footer" class="dialog-footer myfooter">
                    <el-button @click="addDialogStatus = false">取消</el-button>
                    <el-button @click="checkAddForm()" type="primary" v-if="addDialogParam.type == '1'">确定</el-button>
                    <el-button @click="processUserAdd()" type="primary" v-if="addDialogParam.type == '2'">确定
                    </el-button>
                </div>
            </el-dialog>

            <el-dialog title="查看" :visible.sync="viewDialogStatus" width="40%" :destroy-on-close="true"
                       :modal-append-to-body="false" :close-on-click-modal="false" :fullscreen="false">
                <el-form :model="addDialogParam" ref="addInfoList">
                    <div class="info-line">
                        <el-form-item prop="testInfo">
                            <span>名称</span>
                            {{addDialogParam.name}}
                        </el-form-item>
                    </div>
                    <div class="info-line">
                        <el-form-item prop="testInfo">
                            <span>截止日期</span>
                            {{addDialogParam.endTime}}
                        </el-form-item>
                    </div>
                    <div class="info-line">
                        <el-form-item prop="testInfo">
                            <span>材料说明</span>
                            {{addDialogParam.remark}}
                        </el-form-item>
                    </div>
                    <div class="info-line">
                        <span>附件材料</span><br/>
                        <uploadDown :datalist="uploadUserFileList" :dialogStatus="uploadstatus"></uploadDown>
                    </div>
                    <div class="info-line" v-if="this.userFileList.attList.length > 0">
                        <br/>
                        <span>回复附件</span><br/>
                        <uploadDown :datalist="userFileList" :dialogStatus="uploadstatus"></uploadDown>
                    </div>
                </el-form>
                <div slot="footer" class="dialog-footer myfooter">
                    <el-button @click="viewDialogStatus = false">关闭</el-button>
                </div>
            </el-dialog>

            <el-dialog title="选择接收人" :visible.sync="appendixUserDialog" width="50%" :destroy-on-close="true"
                       :modal-append-to-body="false" :close-on-click-modal="false">
                <div>
                    <el-form :model="exaList" ref="addInfoList">
                        <el-form-item prop="processUserId" class="info-line online">
                            <span style="width: 80px;">接收人</span>
                            <deptTreeUserNew class="tree" @treeCheck="treeCheckonly" :defaultList="defaultList"
                                             :type="depttype" :closeList="closeList"></deptTreeUserNew>
                            <div class="tree treeUser">
                                <p v-for="(item,index) in treeList" :key="index">{{ item.receiveName }}
                                    <i @click="deletes(item,index)" class="el-icon-error"></i>
                                </p>
                            </div>
                            <el-form-item style="margin-bottom:0;display:none;">
                                <el-input></el-input>
                            </el-form-item>
                        </el-form-item>
                    </el-form>

                    <div slot="footer" style="text-align: right;padding-bottom: 20px">
                        <el-button @click="appendixUserDialog = false">取 消</el-button>
                        <el-button type="primary" @click="checkUserList()">确 定</el-button>
                    </div>
                </div>
            </el-dialog>

            <el-dialog title="回复详情" :visible.sync="appendixInfoStatus" width="70%" :close-on-press-escape="false"
                       :show-close="true" :before-close="closeAppendixInfo" :destroy-on-close="true"
                       :modal-append-to-body="false"
                       :close-on-click-modal="false" :fullscreen="true">
                <div class="tabbox" style="height: 500px">
                    <el-table height="calc(100% - 50px)" class="com-table" ref="multipleTable"
                              :data="appindixInfoData"
                              tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                        <el-table-column
                            type="index"
                            width="50">
                        </el-table-column>
                        <el-table-column prop="deptName" label="回复部门">
                        </el-table-column>
                        <el-table-column prop="userName" label="回复人">
                        </el-table-column>
                        <el-table-column prop="state" label="回复情况">
                            <template slot-scope="scope">
                                <span v-if="scope.row.state == 0">未回复</span>
                                <span v-if="scope.row.state == 1">已回复</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="isOverTime" label="是否超时">
                            <template slot-scope="scope">
                                <span v-if="scope.row.isOverTime == 0">否</span>
                                <span v-if="scope.row.isOverTime == 1">是</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="回复内容">
                            <template slot-scope="scope">
                                <div v-for="item in scope.row.filesList">
                                    <span style="color: #0b82ff;cursor: pointer"
                                          @click="downAppendixFile(item.id,item.fileName)">{{
                                            item.fileName
                                        }}&nbsp;</span>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination class="pageBox" @current-change="appendixInfoChange"
                                   layout="prev, pager, next" background
                                   :total="appendixInfoTotal">
                    </el-pagination>
                </div>
                <div class="dialog-footer myfooter">
                    <el-button @click="exportZip()" type="primary">导出</el-button>
                    <el-button @click="appendixInfoStatus = false">关闭</el-button>
                </div>
            </el-dialog>
        </div>
    </fullscreen>
</template>
<script>
import mySearch from "../../../components/search.vue";
import myUpload from "../../../components/upload";
import toolList from "../../../components/toolList";
import myMessageNew from "../../../components/myMessageNew.vue"
import uploadDown from '../../../components/uploadDown'
import deptTreeUserNew from "../../../components/deptTreeUserNew";

export default {
    components: {
        myUpload,
        mySearch,
        toolList,
        myMessageNew,
        uploadDown,
        deptTreeUserNew
    },
    data() {
        return {
            tableData: [{}],
            total: 0,
            appendixInfoTotal: 0,
            params: {},
            deleteTag: '',
            centerDialogVisible: false,
            loading: false,
            issuedAddStatus: false,
            addDialogParam: {},
            tooltit: '收入附件收集',
            searchList: [{
                type: 'input',
                tit: '名称',
                value: '',
                width: '100%',
            },],
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() < Date.now();
                }
            },
            fullscreen: false,
            addDialogStatus: false,
            fileInfo: {
                limit: 350,
                url: '/market/cIncomeExcelAppendix/upload',
                fileList: []
            },
            attList: [],
            //选择审批人
            checkexa: false,
            //审批人用到的
            treeListonly: {},
            exaList: {},
            defaultList: [],
            treeList: [],
            closeList: false,
            depttype: 0,
            appendixUserDialog: false,
            viewDialogStatus: false,
            //提交人下载文件库
            uploadUserFileList: {
                url: '/market/cIncomeExcelAppendix/downFile',
                type: 1,
                attList: []
            },
            //发起人下载文件框
            userFileList: {
                url: '/market/cIncomeExcelAppendix/downFile',
                type: 1,
                attList: []
            },
            uploadStatus: false,
            addDisabled: false,
            nowUserId: '',
            //回复详情
            appendixInfoStatus: false,
            appindixInfoData: [{}],
            appendixInfoPageSize: 1,
            pageSize: 1,
            appendixInfoParams: {},
        }
    },
    methods: {
        closeAppendixInfo() {
            this.appendixInfoStatus = false;
        },
        openAppendixInfo(row) {
            console.log(row);
            this.appendixInfoParams = {
                excelId: row.appendixId,
                name: row.appendixName
            }
            this.appendixInfoPageSize = 1;
            this.getAppendixInfoPage(this.appendixInfoParams, this.appendixInfoPageSize);
            this.appendixInfoStatus = true;
        },
        processUserAdd() {
            var _this = this;
            if (this.attList.length == 0) {
                this.$message({
                    message: '请上传附件',
                    type: 'error'
                });
                return;
            }
            //执行下发
            this.$http({
                url: '/market/cIncomeExcelAppendix/uploadUserAppendix',
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    appendixUserId: this.nowUserId,
                    fileList: _this.attList
                },
            }).then((res) => {
                this.loading = false;
                if (res.data.result === 1) {
                    _this.$message({
                        message: res.data.desc,
                        type: 'error'
                    });
                } else {
                    _this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    this.addDialogStatus = false;
                    this.getList({}, 1);
                }
            });
        },
        processAppendixInfo(row, type) {
            this.addDialogParam = {
                type: '2'
            };
            this.attList = [];
            this.uploadUserFileList.attList = [];
            this.userFileList.attList = [];
            var _this = this;
            //执行下发
            this.$http({
                url: '/market/cIncomeExcelAppendix/queryAppendixInfo',
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    excelId: row.id
                },
            }).then((res) => {
                this.loading = false;
                var d = res.data.body;
                console.log(d);
                this.addDialogParam = {
                    type: '2',
                    name: d.appendix.name,
                    remark: d.appendix.remark,
                    endTime: d.appendix.endTime
                }
                this.uploadUserFileList.attList = d.fileList[0];
                this.userFileList.attList = d.fileList[1];
                if (type == 1) {
                    this.viewDialogStatus = true;
                } else if (type == 2) {
                    this.addDialogStatus = true;
                    this.addDisabled = true;
                    this.nowUserId = row.id;
                }
            });
        },
        checkAddForm() {
            if (!this.addDialogParam.name) {
                this.$message({
                    message: '名称不能为空',
                    type: 'error'
                });
                return;
            }
            if (!this.addDialogParam.remark) {
                this.$message({
                    message: '材料说明不能为空',
                    type: 'error'
                });
                return;
            }
            if (!this.addDialogParam.endTime) {
                this.$message({
                    message: '截止日期不能为空',
                    type: 'error'
                });
                return;
            }
            if (this.addDialogParam.name.length > 240) {
                this.$message({
                    message: '名称长度不能大于240字',
                    type: 'error'
                });
                return;
            }
            if (this.addDialogParam.remark.length > 300) {
                this.$message({
                    message: '材料说明长度不能大于300字',
                    type: 'error'
                });
                return;
            }
            if (this.attList.length == 0) {
                this.$message({
                    message: '请上传附件',
                    type: 'error'
                });
                return;
            }
            this.appendixUserDialog = true;
        },
        checkUserList() {
            if (this.treeList.length == 0) {
                this.$message({
                    message: '请选择接收人',
                    type: 'error'
                });
            } else {
                this.processAddNew();
            }
        },
        addNew() {
            this.addDialogParam = {
                type: '1'
            };
            this.addDisabled = false;
            this.addDialogStatus = true;
        },
        processAddNew() {
            var _this = this;
            var userList = [];
            this.treeList.map((o) => {
                userList.push({
                    userId: o.receiveNo
                });
            });
            //执行下发
            this.$http({
                url: '/market/cIncomeExcelAppendix/addNewAppendix',
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    appendix: this.addDialogParam,
                    userList: userList,
                    fileList: _this.attList
                },
            }).then((res) => {
                this.loading = false;
                if (res.data.result === 1) {
                    _this.$message({
                        message: res.data.desc,
                        type: 'error'
                    });
                } else {
                    _this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    this.addDialogStatus = false;
                    this.appendixUserDialog = false;
                    this.getList({}, 1);
                }
            });
        },
        //文件返回值
        uploadBack(v) {
            this.attList = v;
        },
        //搜索数据
        searchInfo(v) {
            this.params = {};
            v[0] ? this.params.name = v[0] : '';
            this.getList(this.params, this.pageSize);
        },
        //功能栏
        iconCli(v) {
            if (v === 1) {
                this.getList(this.params, this.pageSize);
            }
            if (v === 2) {
                this.fullscreen = !this.fullscreen
            }
        },
        // 分页
        currchange(v) {
            this.pageSize = v;
            this.getList(this.params, this.pageSize);
        },
        appendixInfoChange(v) {
            this.appendixInfoPageSize = v;
            this.getAppendixInfoPage(this.appendixInfoParams, this.appendixInfoPageSize);
        },
        getList(v, n) {
            this.pageSize = n;
            this.loading = true;
            this.tableData = [];
            let _this = this;
            this.$http({
                url: "/market/cIncomeExcelAppendix/queryAppendixList",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                    "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                },
                data: v,
            }).then((res) => {
                this.tableData = res.data.data;
                this.total = res.data.totalRecord;
                this.loading = false;
            });
        },
        getAppendixInfoPage(v, n) {
            this.appendixInfoPageSize = n;
            this.loading = true;
            this.appindixInfoData = [];
            this.$http({
                url: "/market/cIncomeExcelAppendix/queryAppendixUserList",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                    "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                },
                data: v,
            }).then((res) => {
                this.appindixInfoData = res.data.data;
                this.appendixInfoTotal = res.data.totalRecord;
                this.loading = false;
            });
        },
        //填报人相关
        deletes(val, index) {
            this.treeList.splice(index, 1);
            this.closeList = this.treeList;
        },
        treeCheckonly(v) {
            this.treeList = v;
            this.treeListonly = v;
        },
        treeDelete() {
            this.treeListonly = {};
            this.closeList = !this.closeList;
        },
        downAppendixFile(fileId, fileName) {
            this.$http({
                url: "/market/cIncomeExcelAppendix/downFile",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                responseType: "blob",
                data: {"id": fileId, "fileName": fileName},
            }).then((response) => {
                if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                    let blob = new Blob([response.data], {
                        type: 'application/vnd.ms-excel'
                    });
                    window.navigator.msSaveOrOpenBlob(blob, fileName);
                } else {
                    /* 火狐谷歌的文件下载方式 */
                    var blob = new Blob([response.data])
                    var downloadElement = document.createElement('a')
                    var href = window.URL.createObjectURL(blob);
                    downloadElement.href = href;
                    downloadElement.download = fileName;
                    document.body.appendChild(downloadElement);
                    downloadElement.click();
                    document.body.removeChild(downloadElement);
                    window.URL.revokeObjectURL(href);
                }
            });
        },
        exportZip() {
            let _this = this;
            _this.$http({
                url: '/market/cIncomeExcelAppendix/downloadAppendixZip',
                method: "post",
                responseType: 'blob',
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    excelId: this.appendixInfoParams.excelId
                },
            }).then(res => {
                if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                    let blob = new Blob([res.data], {
                        type: "application/x-msdownload"
                    });
                    window.navigator.msSaveOrOpenBlob(
                        blob,
                        // this.wo.bizName + ".xlsx"
                        this.appendixInfoParams.name + ".zip"
                    );
                } else {
                    /* 火狐谷歌的文件下载方式 */
                    var blob = new Blob([res.data]);
                    var downloadElement = document.createElement("a");
                    var href = window.URL.createObjectURL(blob);
                    downloadElement.href = href;
                    downloadElement.download = this.appendixInfoParams.name + ".zip";
                    document.body.appendChild(downloadElement);
                    downloadElement.click();
                    document.body.removeChild(downloadElement);
                    window.URL.revokeObjectURL(href);
                }
            });
        }
    },
    mounted() {
        this.getList({}, 1);
    },
    created() {

    }
}
</script>
<style scoped lang="scss">
.titbox {
    div {
        float: right;

        i {
            font-size: 22px;
            margin-left: 20px;
            cursor: pointer;
        }
    }
}

.tabbox {
    margin-top: 16px;
}

.pageBox {
    text-align: right;
    margin-top: 10px;
}

.info-line {
    width: 100%;
    display: block;
    padding-left: 20px;

    div {
        width: 90%;
        display: inline-block;
    }

    span {
        width: 100px;
        display: inline-block;
        text-align: left;

        i {
            color: red;
            display: inline-block;
            padding-right: 5px;
        }
    }

    .el-select,
    .el-input {
        width: calc(100% - 150px);
    }
}

.online {
    width: 100%;

    .el-select {
        width: calc(100% - 100px);
    }

    span {
        vertical-align: top;
    }

    .el-textarea {
        width: calc(100% - 100px);
    }

    .tree {
        width: calc(50% - 60px);
        display: inline-block;
        margin-right: 20px;
        height: 300px;
        overflow-y: scroll;

        .el-icon-error {
            float: right;
            font-size: 20px;
            margin-top: 9px;
            cursor: pointer;
        }
    }

    .treeUser {
        margin: 0;
        border: 1px solid #ddd;

        p {
            background: #f4f4f4;
            padding: 0 20px;
            margin-bottom: 5px;
        }
    }

    .treeUserb {
        width: calc(100% - 100px);
        border: 1px solid #ddd;
        background: #f4f4f4;
        border-radius: 3px;
        height: auto;
        overflow: hidden;

        p {
            display: inline-block;
            padding: 0 20px;
            margin-bottom: 5px;
        }
    }
}
</style>

