<template>
    <div style="display: flex; justify-content: space-between">
        <fullscreen :fullscreen.sync="fullscreen" class="container" style="margin: 0;width: 100%;">
            <div class="container-box" style="padding: 0 20px 0 0;">
                <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">
                    </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="name" label="模板名称">
                        </el-table-column>
                        <el-table-column prop="opTime" label="创建时间">
                        </el-table-column>
                        <el-table-column label="操作" width="180px" align="center" fixed="right">
                            <template slot-scope="scope">
                                <el-button size="mini" type="primary" @click="appendixInfo(1,scope.row)">查看</el-button>
                                <el-button size="mini" v-if="scope.row.finishStatus == 0" type="primary"
                                           @click="appendixInfoAdd(1,scope.row)">处理
                                </el-button>
                                <el-button size="mini" v-if="scope.row.finishStatus == 0 && advInfo.isAdmin"
                                           type="danger" @click="delLine(scope.row)">办结
                                </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="appendixInfoStatus" width="70%" :close-on-press-escape="false"
                           :show-close="true" :before-close="closeAppendixInfoList" :destroy-on-close="true"
                           :modal-append-to-body="false"
                           :close-on-click-modal="false" :fullscreen="false">
                    <div class="tabbox" style="height: 500px">
                        <el-table height="calc(100% - 50px)" class="com-table" ref="multipleTable"
                                  :data="appendixInfoData"
                                  tooltip-effect="dark" size="small" border style="width: 100%" v-loading="loading">
                            <el-table-column prop="appendixName" label="模板名称">
                            </el-table-column>
                            <el-table-column prop="cityName" label="地市">
                            </el-table-column>
                            <el-table-column prop="townName" label="区县">
                            </el-table-column>
                            <el-table-column prop="agentName" label="渠道名称">
                            </el-table-column>
                            <el-table-column prop="uploadUserName" label="上报人">
                            </el-table-column>
                            <el-table-column prop="opTime" label="创建时间">
                            </el-table-column>
                            <el-table-column label="操作" width="180px" align="center" fixed="right" v-if="appendixInfoFinishStatus == 0">
                                <template slot-scope="scope">
                                    <el-button size="mini" type="primary" @click="appendixInfoEdit(1,scope.row)">编辑
                                    </el-button>
                                    <el-button size="mini" type="danger" @click="appendixInfoDel(scope.row)">删除
                                    </el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-pagination class="pageBox" @current-change="appendixInfoListChange"
                                       layout="prev, pager, next" background
                                       :total="appendixInfoTotal">
                        </el-pagination>
                    </div>
                    <div class="dialog-footer myfooter">
                        <el-button v-if="advInfo.isAdmin" @click="exportZip()" type="primary">导 出</el-button>
                        <el-button @click="appendixInfoStatus = false">关 闭</el-button>
                    </div>
                </el-dialog>

                <el-dialog title="附件处理" :visible.sync="appendixInfoAddStatus" width="50%" :destroy-on-close="true"
                           :modal-append-to-body="false" :close-on-click-modal="false">
                    <div v-loading="loading">
                        <el-form :model="appendixInfoList" ref="appendixInfoList" :rules="appendixInfoRules">
                            <div class="info-line">
                                <el-form-item prop="cityName">
                                    <span>地市</span>
                                    <el-input v-model="appendixInfoList.cityName" placeholder="地市"
                                              :disabled="disableStatus"></el-input>
                                </el-form-item>
                                <el-form-item prop="townName">
                                    <span>区县</span>
                                    <el-input v-model="appendixInfoList.townName" placeholder="区县"
                                              :disabled="disableStatus"></el-input>
                                </el-form-item>
                            </div>
                            <div class="info-line">
                                <el-form-item prop="agentName">
                                    <span>渠道名称</span>
                                    <el-input v-model="appendixInfoList.agentName" placeholder="渠道名称"
                                              :disabled="disableStatus"></el-input>
                                </el-form-item>
                                <el-form-item prop="uploadUserName">
                                    <span>上报人</span>
                                    <el-input v-model="appendixInfoList.uploadUserName" placeholder="上报人"
                                              :disabled="disableStatus"></el-input>
                                </el-form-item>
                            </div>
                            <div style="padding-left: 80px" v-if="!disableStatus">
                                <myUpload @uploadBack="uploadBack" :fileInfo="fileInfo"
                                          :fileList="fileInfo.fileList"></myUpload>
                            </div>
                            <div style="padding:0 20px 0 100px" v-if="disableStatus">
                                <uploadDown :datalist="datalist" :dialogStatus="uploadstatus"></uploadDown>
                            </div>
                            <div class="info-line">
                                注：上传附件请以渠道名称命名
                            </div>
                        </el-form>
                        <div slot="footer" class="dialog-footer myfooter">
                            <el-button @click="saveAppendixInfo(1)" type="primary">保 存</el-button>
                            <el-button @click="appendixInfoAddStatus = false">取 消</el-button>
                        </div>
                    </div>
                </el-dialog>
            </div>
            <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible"
                       v-if="centerDialogVisible"></myMessage>
            <myMessage :messTit='appendixInfoDelTit' @closeMessage="appendixInfoDelCloseMessage"
                       :centerDialogVisible="appendixInfoDelStatus" v-if="appendixInfoDelStatus"></myMessage>
        </fullscreen>
    </div>
</template>
<script>
import mySearch from "../../../components/search.vue";
import myUpload from "../../../components/upload";
import toolList from "../../../components/toolList";
import myMessage from "../../../components/myMessage.vue"

export default {
    components: {
        myUpload,
        mySearch,
        toolList,
        myMessage
    },
    data() {
        const cityName = (rule, value, callback) => {
            if (!this.appendixInfoList.cityName) {
                callback(new Error('不能为空'))
            } else {
                callback()
            }
        }
        const townName = (rule, value, callback) => {
            if (!this.appendixInfoList.townName) {
                callback(new Error('不能为空'))
            } else {
                callback()
            }
        }
        const agentName = (rule, value, callback) => {
            if (!this.appendixInfoList.agentName) {
                callback(new Error('不能为空'))
            } else {
                callback()
            }
        }
        const uploadUserName = (rule, value, callback) => {
            if (!this.appendixInfoList.uploadUserName) {
                callback(new Error('不能为空'))
            } else {
                callback()
            }
        }

        return {
            appendixInfoRules: {
                cityName: [{
                    required: true,
                    trigger: 'change',
                    validator: cityName
                }],
                townName: [{
                    required: true,
                    trigger: 'change',
                    validator: townName
                }],
                agentName: [{
                    required: true,
                    trigger: 'change',
                    validator: agentName
                }],
                uploadUserName: [{
                    required: true,
                    trigger: 'change',
                    validator: uploadUserName
                }],
            },
            tooltit: '重点攻坚渠道合作率-附件统计',
            searchList: [{
                type: 'input',
                tit: '名称',
                value: '',
                width: '100%',
            },],
            fullscreen: false,
            total: 0,
            pageSize: 1,
            tableData: [{}],
            dialogStatus: false,
            disableStatus: false,
            titname: '终端',
            infolist: {},
            typeOptions: [{
                dataCode: 1,
                dataName: 111,
            }],
            userInfo: {},
            params: {},
            termOption: [],
            centerDialogVisible: false,
            messTit: '',
            delid: '',
            loading: false,
            attList: [],
            uploadstatus: false,
            fileInfo: {
                limit: 50,
                url: '/market/cKeyChannelCoopAppendix/upload',
                fileList: []
            },
            datalist: {
                url: '/sysmgr/noticeatt/downfile',
                type: 2,
                attList: []
            },
            appendixInfoStatus: false,
            appendixId: '',
            appendixName:'',
            appendixInfoData: [{}],
            appendixInfoTotal: 0,
            appendixInfoListPageSize: 1,
            appendixInfoDelTit: '',
            appendixInfoDelStatus: false,
            appendixInfoDelId: '',
            appendixInfoAddStatus: false,
            appendixInfoList: {},
            appendixInfoFinishStatus:'',
            advInfo: {},
        }
    },
    methods: {
        //搜索数据
        searchInfo(v) {
            this.params = {};
            v[0] ? this.params.name = v[0] : '';
            this.getList(this.params, this.pageSize);
        },
        //获取列表
        getList(v, n) {
            this.pageSize = n;
            this.loading = true;
            this.tableData = [];
            let _this = this;
            this.$http({
                url: "/market/cKeyChannelCoopAppendix/queryPage",
                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;
            });
        },
        //获取列表
        getAppendixInfoList(v, n) {
            this.appendixInfoListPageSize = n;
            this.loading = true;
            this.appendixInfoData = [];
            let _this = this;
            this.$http({
                url: "/market/cKeyChannelCoopAppendix/queryAppendixInfoPage",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                    "page": '{"pageNo":"' + n + '","pageSize":"10"}'
                },
                data: {
                    appendixId: this.appendixId
                },
            }).then((res) => {
                this.appendixInfoData = res.data.data;
                this.appendixInfoTotal = res.data.totalRecord;
                this.loading = false;
            });
        },
        // 分页
        currchange(v) {
            this.pageSize = v;
            this.getList(this.params, this.pageSize);
        },
        appendixInfoListChange(v) {
            this.pageSize = v;
            this.getAppendixInfoList(null, this.appendixInfoListPageSize)

        },
        //查看
        appendixInfo(v, n) {
            this.appendixId = n.id;
            this.appendixName = n.name;
            this.appendixInfoFinishStatus = n.finishStatus;
            //获取列表数据
            this.getAppendixInfoList(1, this.appendixInfoListPageSize)
            this.appendixInfoStatus = true;

        },
        //删除
        delLine(v) {
            this.centerDialogVisible = true;
            this.messTit = '是否确认办结?';
            this.delid = v.id;
        },
        closeMessage(v) {
            this.centerDialogVisible = false;
            let _this = this;
            if (v === 1) {
                this.$http({
                    url: "/market/cKeyChannelCoopAppendix/finishAppendix",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        id: this.delid
                    },
                }).then((res) => {
                    if (res.data.result === 1) {
                        _this.$message({
                            message: res.data.desc,
                            type: 'error'
                        });
                    } else {
                        _this.$message({
                            message: '办结成功',
                            type: 'success'
                        });
                        _this.getList(this.params, this.pageSize);
                    }
                });
            }
        },
        appendixInfoDel(v) {
            this.appendixInfoDelStatus = true;
            this.appendixInfoDelTit = '是否确认删除?';
            this.appendixInfoDelId = v.id;
        },
        appendixInfoDelCloseMessage(v) {
            this.appendixInfoDelStatus = false;
            let _this = this;
            if (v === 1) {
                this.$http({
                    url: "/market/cKeyChannelCoopAppendix/delAppendixInfo",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        id: this.appendixInfoDelId
                    },
                }).then((res) => {
                    if (res.data.result === 1) {
                        _this.$message({
                            message: res.data.desc,
                            type: 'error'
                        });
                    } else {
                        _this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        _this.getAppendixInfoList(null, this.pageSize);
                    }
                });
            }
        },
        //查看
        appendixInfoAdd(v, n) {
            this.appendixInfoList = {
                cityName: "",
                uploadUserName: "",
                agentName: "",
                townName: "",
                appendixId: n.id,
                appendixName: n.name
            };
            this.disableStatus = false;
            var adv = this.advInfo;
            this.appendixInfoList.cityName = adv.cityName;
            this.appendixInfoList.uploadUserName = adv.userName;
            this.attList = [];
            this.fileInfo.fileList = [];
            this.appendixInfoAddStatus = true;
        },
        appendixInfoEdit(v, n) {
            var _this = this;
            //获取文件列表
            this.$http({
                url: '/market/cKeyChannelCoopAppendix/queryAppendixFilesList',
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    appendixInfoId: n.id,
                },
            }).then((res) => {
                if (res.data.result === 1) {
                    _this.$message({
                        message: res.data.desc,
                        type: 'error'
                    });
                } else {
                    console.log(res.data);
                    //显示文件列表
                    this.datalist.attList = res.data;
                    this.attList = [];
                    this.fileInfo.fileList = [];
                    res.data.forEach(item => {
                        this.fileInfo.fileList.push({
                            name: item.fileName,
                            id: item.id,
                            fileName: item.fileName,
                            fileCode: item.fileCode,
                            opName:item.opName,
                            opNo: item.opNo,
                            opTime: item.opTime
                        });
                        this.attList.push({
                            name: item.fileName,
                            id: item.id,
                            fileName: item.fileName,
                            fileCode: item.fileCode,
                            opName:item.opName,
                            opNo: item.opNo,
                            opTime: item.opTime
                        });
                    })
                    this.appendixInfoList = Object.assign({}, n); //拷贝
                    this.appendixInfoAddStatus = true;
                }
            });

        },
        saveAppendixInfo(v) {
            let _this = this;
            _this.appendixInfoList.opNo = _this.userInfo.loginNo;
            _this.appendixInfoList.opName = _this.userInfo.loginName;
            _this.appendixInfoList.opTime = _this.$formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
            if (v === 1) {
                //判断是否上传了附件
                if (_this.attList.length > 0) {
                    this.submitAppendixInfo("/market/cKeyChannelCoopAppendix/addAppendixInfo");
                } else {
                    _this.$message({
                        message: '请上传附件',
                        type: 'error'
                    });
                }
            }
        },
        submitAppendixInfo(u) {
            let _this = this;
            this.$refs.appendixInfoList.validate(valid => {
                if (valid) {
                    this.$http({
                        url: u,
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {
                            appendixInfo: _this.appendixInfoList,
                            filesList: _this.attList
                        },
                    }).then((res) => {
                        if (res.data.result === 1) {
                            _this.$message({
                                message: res.data.desc,
                                type: 'error'
                            });
                        } else {
                            _this.$message({
                                message: '成功',
                                type: 'success'
                            });
                            _this.fileInfo.fileList = [];
                            _this.attList = [];
                            _this.appendixInfoAddStatus = false;
                            _this.getAppendixInfoList(1, this.appendixInfoListPageSize)
                        }
                    });
                }
            })
        },
        //功能栏
        iconCli(v) {
            if (v === 1) {
                this.getList(this.params, this.pageSize);
            }
            if (v === 2) {
                this.fullscreen = !this.fullscreen
            }
        },
        getUser() {
            this.userInfo = JSON.parse(window.sessionStorage.userInfo);
            this.infolist = {
                opNo: '',
                opName: '',
            };
            this.infolist.opNo = this.userInfo.loginNo;
            this.infolist.opName = this.userInfo.loginName;
        },
        closeAppendixInfoList() {
            this.appendixInfoStatus = false;
        },
        //文件返回值
        uploadBack(v) {
            this.attList = v;
        },
        getAdvUserInfo() {
            this.$http({
                url: "/market/cKeyChannelCoopExcel/getUserInfo",
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                },
            }).then((res) => {
                if (res.data.result !== 1) {
                    this.advInfo = res.data.body;
                }
            });
        },
        exportZip() {
            let _this = this;
            let param = new FormData();
            param.append("id",_this.appendixId);
            _this.$http({
                url: '/market/cKeyChannelCoopAppendix/downloadAppendixZip',
                method: "post",
                responseType: 'blob',
                headers: {
                    "Content-Type": "application/json",
                },
                data: {
                    id:_this.appendixId
                },
            }).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.appendixName + ".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.appendixName + ".zip";
                    document.body.appendChild(downloadElement);
                    downloadElement.click();
                    document.body.removeChild(downloadElement);
                    window.URL.revokeObjectURL(href);
                }
            });
        }
    },
    mounted() {
        this.getAdvUserInfo();
        this.getList({}, 1);
        this.getUser();
    },
    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: 50%;
        display: inline-block;
    }

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

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

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

.online {
    width: 100%;

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

    span {
        vertical-align: top;
    }

    .el-textarea {
        width: calc(100% - 100px);
    }
}
</style>
