<template>
<!-- 预案文档管理 -->
    <div class="APDocument">
        <div class="box">
            <div class="box-left" :style="listTopicTree.show ? null : { width: '22px' }">
                <div class="box-left-bt" style="overflow: hidden;">
                    <span v-show="listTopicTree.show">目录</span>
                    <div style="float: right;height: 100%;padding-right: 4px;">
                        <span
                            class="el-icon-d-arrow-right"
                            style="cursor: pointer; transition: transform 0.3s ease-out, -webkit-transform 0.3s ease-out;"
                            :style="listTopicTree.show ? { transform: 'rotate(180deg)'} : { transform: 'rotate(0deg)'} "
                            @click="listTopicTree.show = !listTopicTree.show;"
                        ></span>
                    </div>
                </div>
                <div class="box-left-tree">
                    <el-tree
                        v-show="listTopicTree.show"
                        :data="listTopicTree.data"
                        :props="listTopicTree.defaultProps"
                        v-loading="listTopicTree.loading"
                    >
                        <span
                            class="custom-tree-node"
                            :class="{'tree-row-on': filterForm.data.itemcode === data.itemcode}"
                            style="width:100%; font-size:12px;"
                            slot-scope="{ node, data }"
                        >
                            <p
                                style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"
                                :title="node.label"
                                @click="selectByTopicId(data)"
                            >{{ node.label }}</p>
                        </span>
                    </el-tree>
                </div>
            </div>
            <div class="box-right" :style="listTopicTree.show ? null : { 'margin-left': '22px' }">
                <div class="box-top">
                    <div class="box-right-bt">文件</div>
                    <div class="box-top-gjl">
                        <el-button
                            class="button"
                            type="primary"
                            size="mini"
                            icon="el-icon-plus"
                            @click="but_add"
                        >上传</el-button>
                    </div>
                </div>
                <div class="box-bottom">
                    <el-table stripe
                        id="singleTable"
                        ref="singleTable"
                        :data="tableData"
                        v-loading="tableLoading"
                        style="width: 100%;"
                        :height="singleTableHeight"
                        border
                        size="mini"
                        highlight-current-row
                    >
                        <el-table-column
                    sortable
                            prop="parentcode"
                            label="目录"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                    sortable
                            prop="filename"
                            label="文件名称"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                    sortable
                            prop="creator"
                            label="编制人"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                    sortable
                            prop="checkperson"
                            label="审核人"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                    sortable
                            prop="signperson"
                            label="签发人"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                    sortable
                            prop="starttime"
                            label="日期"
                            min-width="100px"
                            :show-overflow-tooltip="true"
                        ></el-table-column>
                        <el-table-column
                            class-name="gn-TableDownloadExcel-none"
                            label="操作"
                            align="center"
                            width="140px"
                            fixed="right"
                        >
                            <template slot-scope="scope">
                                <div>
                                    <el-button
                                        type="primary"
                                        size="mini"
                                        @click="but_look(scope.row)"
                                    >查看</el-button>
                                    <el-button
                                        type="danger"
                                        size="mini"
                                        v-privilege="activeMenu + 'DELETE'"
                                        @click="but_del(scope.row)"
                                    >删除</el-button>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        :page-sizes="[10, 20, 50, 100, 500, 1000]"
                        :page-size="pageSize"
                        :current-page.sync="pageNum"
                        @size-change="tableSizeChange"
                        @current-change="getTableData()"
                        style="text-align: right;margin-top: 10px;"
                    ></el-pagination>
                </div>
            </div>
        </div>
        <el-dialog
            :title="'文件 - ' + (dialog.editorBox.type === 'add' ? '上传' : '查看')"
            :visible.sync="dialog.editorBox.show"
            width="600px"
            :close-on-click-modal="false"
            :show-close="!loading"
        >
            <div>
                <el-form
                    ref="dialog_form"
                    :model="dialog.editorBox.form.data"
                    :rules="dialog.editorBox.form.rules"
                    size="mini"
                    label-width="90px"
                >
                    <el-form-item label="文件" prop="filename">
                        <el-input clearable
                            class="input-but"
                            style="width: 100%;"
                            :value="dialog.editorBox.form.data.filename"
                            readonly
                            :disabled="dialog.editorBox.type === 'look'"
                        >
                            <el-button
                                slot="append"
                                type="primary"
                                @click="selectFile"
                            >{{dialog.editorBox.type === 'add' ? '选择' : '下载'}}</el-button>
                        </el-input>
                        <input
                            ref="licFile"
                            type="file"
                            style="display: none;"
                            @change="tirggerFile($event)"
                        >
                    </el-form-item>
                    <el-form-item label="文件上传目录" prop="parentcode">
                        <el-input clearable
                            class="input-but"
                            style="width: 100%;"
                            :value="dialog.editorBox.form.data.parentcode"
                            readonly
                            :disabled="dialog.editorBox.type === 'look'"
                        >
                            <el-button
                                v-if="dialog.editorBox.type === 'add'"
                                slot="append"
                                type="primary"
                                @click="selectParent"
                            >选择</el-button>
                        </el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="编制人" prop="creator">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.creator"
                                    :disabled="dialog.editorBox.type === 'look'"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="审核人" prop="checkperson">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.checkperson"
                                    :disabled="dialog.editorBox.type === 'look'"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="签发人" prop="signperson">
                                <el-input clearable
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.signperson"
                                    :disabled="dialog.editorBox.type === 'look'"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="上传日期" prop="starttime">
                                <el-date-picker
                                    type="datetime"
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.starttime"
                                    :disabled="dialog.editorBox.type === 'look'"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="文档摘要" prop="describ">
                                <el-input clearable
                                    type="textarea"
                                    style="width: 100%;"
                                    v-model="dialog.editorBox.form.data.describ"
                                    :disabled="dialog.editorBox.type === 'look'"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button @click="dialog.editorBox.show = false" :loading="loading">取 消</el-button>
                <el-button
                    v-if="dialog.editorBox.type === 'add'"
                    type="primary"
                    @click="dataSave"
                >保 存</el-button>
            </span>
        </el-dialog>
        <el-dialog
            :title="'选择文件上传目录'"
            :visible.sync="dialog.parentBox.show"
            width="300px"
            :close-on-click-modal="false"
        >
            <div style="height: 250px;overflow: auto;">
                <el-tree
                    :data="listTopicTree.data"
                    :props="listTopicTree.defaultProps"
                    v-loading="listTopicTree.loading"
                >
                    <span
                        class="custom-tree-node"
                        :class="{'tree-row-on': dialog.parentBox.form.data.parentcode === data.path}"
                        style="width:100%; font-size:12px;"
                        slot-scope="{ node, data }"
                    >
                        <p
                            style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"
                            :title="node.label"
                            @click="selectByParent(data)"
                        >{{ node.label }}</p>
                    </span>
                </el-tree>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button @click="dialog.parentBox.show = false">取 消</el-button>
                <el-button
                    type="primary"
                    @click="parentSave"
                >选 择</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from '@/utils/util.js';
import store from '@/store/index.js';
export default {
    name: 'APDocument',
    data () {
        return {
            store,
            userInfo: {
                data: null
            },
            activeMenu: '',
            filterForm: {
                data: {
                    itemcode: '',
                    path: ''
                },
                rules: {

                }
            },
            listTopicTree: {
                show: true,
                loading: false,
                data: [],
                defaultProps: {
                    children: 'children',
                    label: 'itemcode'
                }
            },
            pageNum: 1,
            pageSize: 20,
            total: 0,
            singleTableHeight: 100,
            tableData: [
                // {
                //     parentcode: '文档/法律法规',
                //     filename: '文件.txt',
                //     creator: '1',
                //     checkperson: '2',
                //     signperson: '3',
                //     starttime: '2019-08-26 15:03:56'
                // }
            ],
            loading: false,
            tableLoading: false,
            dialog: {
                editorBox: {
                    show: false,
                    type: '',
                    form: {
                        file: null,
                        data: {
                            recordid: '',
                            itemcode: '',
                            parentcode: '',
                            filename: '',
                            creator: '',
                            checkperson: '',
                            signperson: '',
                            starttime: '',
                            describ: ''
                        },
                        rules: {
                            recordid: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            itemcode: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            parentcode: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            filename: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            creator: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            checkperson: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            signperson: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            starttime: [
                                { required: true, message: '该项不能为空', trigger: 'change' }
                            ],
                            describ: [
                                // { required: true, message: '该项不能为空', trigger: 'change' }
                            ]
                        }
                    }
                },
                parentBox: {
                    show: false,
                    form: {
                        data: {
                            itemcode: '',
                            parentcode: ''
                        },
                        rules: {

                        }
                    }
                }
            }
        }
    },
    created () {
        this.activeMenu = window.localStorage.getItem('activeMenu');
    },
    mounted () {
        let that = this;
        window.PEDataObj = {
            // 将数据绑定到window上，供main页面使用
            vm: that,
            // tableArr：用于导出成Excel的表格的信息
            tableArr: [
                {
                    name: '',
                    id: 'singleTable'
                }
            ]
        };
        that.$nextTick(() => {
            // 立即获取的height有一定偏差，通过setTimeout延迟来解决
            setTimeout(() => {
                that.singleTableHeight = that.getRoleHeight(that.$refs['singleTable'].$el) - 45;
            }, 1);
        });
        that.store.dispatch('getUserInfo').then((res) => {
            that.userInfo.data = res.data;
        });
        that.getListTopicTree();
        that.getTableData();
    },
    methods: {
        //  计算树区域高度
        getRoleHeight (dom) {
            return window.innerHeight - dom.offsetTop;
        },
        // 获取节点树
        getListTopicTree () {
            let that = this
            let url = 'pass/ems/v1/tmcdocdirectorys/gettree/';
            that.listTopicTree.loading = true;
            that.axios.get(url)
                .then(function (res) {
                    if (res.code === '0') {
                        that.listTopicTree.data = res.data;
                        // let data = [{
                        //     itemcode: '节点名称',
                        //     parentcode: '父节点名称',
                        //     path: '路径',
                        //     note: '描述'
                        // }];
                    } else {
                        that.$message.error(res.message);
                    }
                    that.listTopicTree.loading = false;
                });
        },
        selectByTopicId (data) {
            let that = this;
            that.filterForm.data.itemcode = data.itemcode;
            that.filterForm.data.path = data.path;
            that.getTableData(1);
        },
        // 获取表格中的数据
        getTableData (pageNum) {
            let that = this,
                params = {
                    // parentcode: that.filterForm.data.path
                    itemcode: that.filterForm.data.itemcode
                };
            that.pageNum = pageNum || that.pageNum;
            that.tableLoading = true;
            let url = 'pass/ems/v1/tcm0317s/?pageNum=' + that.pageNum + '&pageSize=' + that.pageSize;
            that.axios.get(url, {
                params: params
            })
                .then(function (res) {
                    if (res.code === '0') {
                        let arr = [];
                        for (let item of res.data.list) {
                            arr.push({
                                recordid: item.recordid,
                                parentcode: item.parentcode,
                                filename: item.filename,
                                creator: item.creator,
                                checkperson: item.checkperson,
                                signperson: item.signperson,
                                starttime: item.starttime,
                                describ: item.describ
                            })
                        }
                        that.tableData = arr;
                        that.total = res.data.total;
                    } else {
                        that.$message.error(res.message);
                    }
                    that.tableLoading = false;
                }).catch(function () {
                    that.tableLoading = false;
                });
        },
        // 改变表格显示条数
        tableSizeChange (val) {
            let that = this;
            that.pageSize = val;
            that.getTableData(1);
        },
        but_add () {
            let that = this;
            for (let key in that.dialog.editorBox.form.data) {
                that.dialog.editorBox.form.data[key] = '';
            }
            that.dialog.editorBox.form.data.parentcode = that.filterForm.data.path;
            that.dialog.editorBox.form.data.creator = that.userInfo.data.userName;
            that.dialog.editorBox.form.data.checkperson = that.userInfo.data.userName;
            that.dialog.editorBox.form.data.signperson = that.userInfo.data.userName;
            that.dialog.editorBox.form.data.starttime = formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss');
            that.dialog.editorBox.form.file = null;
            that.dialog.editorBox.type = 'add';
            that.dialog.editorBox.show = true;
        },
        but_look (row) {
            let that = this;
            for (let key in that.dialog.editorBox.form.data) {
                if (typeof row[key] !== 'undefined') {
                    that.dialog.editorBox.form.data[key] = row[key];
                } else {
                    console.log(key);
                }
            }
            that.dialog.editorBox.type = 'look';
            that.dialog.editorBox.show = true;
        },
        but_del (row) {
            let that = this;
            that.$confirm('是否删除该条数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                state: 'warning'
            }).then(() => {
                that.axios.delete('pass/ems/v1/tcm0317s/' + row.recordid)
                    .then(function (res) {
                        if (res.code === '0') {
                            that.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            that.getTableData();
                        } else {
                            that.$message.error(res.message);
                        }
                    }).catch(function () {
                    });
            }).catch(() => {
            });
        },
        dataSave () {
            let that = this;
            let SubmitData = {
                recordid: that.dialog.editorBox.form.data.recordid,
                parentcode: that.dialog.editorBox.form.data.parentcode,
                filename: that.dialog.editorBox.form.data.filename,
                creator: that.dialog.editorBox.form.data.creator,
                checkperson: that.dialog.editorBox.form.data.checkperson,
                signperson: that.dialog.editorBox.form.data.signperson,
                starttime: that.dialog.editorBox.form.data.starttime,
                describ: that.dialog.editorBox.form.data.describ
            };
            that.$refs['dialog_form'].validate((valid) => {
                if (valid) {
                    let foorData = new FormData();
                    for (let key in SubmitData) {
                        foorData.append(key, SubmitData[key]);
                    }
                    that.loading = true;
                    that.axios.post('pass/ems/v1/tcm0317s/', foorData, {
                         headers: {
                            'Content-Type': 'multipart/form-data'
                        }
                    })
                        .then(function (res) {
                            if (res.code === '0') {
                                let fileData = new FormData();
                                fileData.append('file', that.dialog.editorBox.form.file);
                                fileData.append('path', that.dialog.editorBox.form.data.parentcode);
                                that.axios.post('pass/ems/v1/multipart/upload/', fileData, {
                                    headers: {
                                        'Content-Type': 'multipart/form-data'
                                    }
                                })
                                    .then(function (res) {
                                        if (res.code === '0') {
                                            that.$message({
                                                message: '新增成功',
                                                type: 'success'
                                            });
                                            that.getTableData();
                                            that.dialog.editorBox.show = false;
                                        } else {
                                            that.$message.error(res.message);
                                        }
                                    }).catch(function () {
                                        that.loading = false;
                                    });
                            } else {
                                that.$message.error(res.message);
                            }
                        }).catch(function () {
                            that.loading = false;
                        });
                }
            });
        },
        // 上传与下载文件按钮
        selectFile () {
            let that = this;
            if (that.dialog.editorBox.type === 'add') {
                that.$refs['licFile'].click();
            } else {
                // 下载
                if (that.dialog.editorBox.form.data.parentcode && that.dialog.editorBox.form.data.filename) {
                    let url = that.dialog.editorBox.form.data.parentcode + '/' + that.dialog.editorBox.form.data.filename;
                    url = '/zhongsteel.pass.web/pass/ems/static/' + url.replace(/\\/g, '/');
                    console.log(url);
                    const xhr = new XMLHttpRequest();
                    xhr.open('GET', url, true);
                    xhr.responseType = 'blob';
                    xhr.onload = () => {
                        if (xhr.status === 200) {
                            var fileName = that.dialog.editorBox.form.data.filename;
                            // 获取文件blob数据并保存
                            that.saveAs(xhr.response, fileName);
                        }
                    };
                    xhr.send();
                }
            }
        },
        // js实现文件下载而不直接打开
        saveAs (data, name) {
            var urlObject = window.URL || window.webkitURL || window;
            var exportBlob = new Blob([data]);
            var saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
            saveLink.href = urlObject.createObjectURL(exportBlob);
            saveLink.download = name;
            saveLink.click();
        },
        // 实现文件选择功能
        tirggerFile ($event) {
            let that = this;
            if (event.target.files[0]) {
                that.dialog.editorBox.form.data.filename = event.target.files[0].name;
                that.dialog.editorBox.form.file = event.target.files[0];
            }
        },
        // 选择上传目录
        selectParent () {
            let that = this;
            that.dialog.parentBox.form.data.parentcode = that.dialog.editorBox.form.data.parentcode;
            that.dialog.parentBox.show = true;
        },
        selectByParent (data) {
            let that = this;
            that.dialog.parentBox.form.data.itemcode = data.itemcode;
            that.dialog.parentBox.form.data.parentcode = data.path;
        },
        parentSave () {
            let that = this;
            that.dialog.editorBox.form.data.parentcode = that.dialog.parentBox.form.data.parentcode;
            that.dialog.parentBox.show = false;
        }
    }
}
</script>

<style lang="less">
.APDocument {
    min-width: 700px;
    height: 100%;
    .box {
        height: 100%;
        padding: 15px 15px 0 15px;
        .box-left {
            float: left;
            width: 200px;
            height: calc(100% - 3px);
            overflow: auto;
            .box-left-bt {
                line-height: 47px;
                font-size: 16px;
                border-bottom: 1px solid #ccc;
            }
            .box-left-tree {
                height: calc(100% - 50px);
                overflow: auto;
                padding: 7px 7px 0 0;
            }
        }
        .box-right {
            border-left: 1px solid #ccc;
            margin-left: 200px;
            .box-top {
                .box-right-bt {
                    line-height: 47px;
                    padding: 0 7px;
                    font-size: 16px;
                }
                .box-top-gjl {
                    padding: 7px 0 7px 7px;
                    border-top: 1px solid #ccc;
                    // border-bottom: 1px solid #ccc;
                    // margin-bottom: 6px;
                }
            }
            .box-bottom {
                padding-left: 7px;
            }
        }
    }
    .tree-row-on {
        color: #3279e8;
        font-weight: 600;
    }
    .input-but {
        .el-input-group__append {
            background-color: #409eff;
            color: #fff;
        }
    }
    .is-success {
        .input-but {
            .el-input-group__append {
                border-color: #67c23a;
            }
        }
    }
    .is-error {
        .input-but {
            .el-input-group__append {
                border-color: #f56c6c;
            }
        }
    }
}
</style>
