<template>
    <fullscreen :fullscreen.sync="fullscreen" class="container">
        <div class="container-box">
            <toolList @iconCli='iconCli' :tooltit='tooltit'></toolList>
            <div class="search">
                <mySearch :searchList="searchList" @searchInfo="searchInfo"></mySearch>
                <el-button type="primary" @click="dialogCheck(3)" size="medium">添加</el-button>
            </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="docName" label="文档名称">
                    </el-table-column>
                    <el-table-column prop="docTypeName" label="文档类型">
                    </el-table-column>
                    <el-table-column prop="contentTypeName" label="文档内容分类">
                    </el-table-column>
                    <el-table-column label="文档有效期">
                        <template slot-scope="scope">
                            {{scope.row.startTime +'~'+ scope.row.endTime}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="220px" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" @click="dialogCheck(1,scope.row)">查看</el-button>
                            <el-button size="mini" type="primary" @click="dialogCheck(2,scope.row)">修改</el-button>
                            <el-button size="mini" 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>
        </div>

        <el-dialog :title="titname" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
            :modal-append-to-body="false" :close-on-click-modal="false" :before-close="closedia" :fullscreen="true">
            <div v-loading="loadinged">
                <el-form :model="infolist" ref="infolist" :rules="rules"
                    style="height: calc(100vh - 180px);overflow-y: scroll;">
                    <div class="info-line">
                        <el-form-item prop="docName">
                            <span>文档名称</span>
                            <el-input v-model="infolist.docName" placeholder="文档名称" :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item>
                            <span>文档类型</span>
                            <el-select :popper-append-to-body="false" v-model="infolist.docTypeName" placeholder="文档类型" :disabled="disableStatus">
                                <el-option v-for="item in docType" :key="item.dataName" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>内容分类</span>
                            <el-select :popper-append-to-body="false" v-model="infolist.contentTypeName" placeholder="文档内容分类" :disabled="disableStatus">
                                <el-option v-for="item in contentType" :key="item.dataName" :label="item.dataName"
                                    :value="item.dataName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <span>重要内容摘要</span>
                            <el-input v-model="infolist.contentAbstract" placeholder="重要内容摘要"
                                :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="startTime">
                            <span>有效期(开始)</span>
                            <el-date-picker v-model="infolist.startTime" type="date" placeholder="文档有效期(开始)"
                                :disabled="disableStatus">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item prop="endTime">
                            <span>有效期(结束)</span>
                            <el-date-picker v-model="infolist.endTime" type="date" placeholder="文档有效期(结束)"
                                :disabled="disableStatus">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <span>文档提醒事项</span>
                            <el-input v-model="infolist.docNotice" placeholder="文档提醒事项"
                                :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="validDay">
                            <span>提醒时间范围</span>
                            <el-input v-model="infolist.validDay" type="number" placeholder="文档有效期前 x 天"
                                :disabled="disableStatus">
                            </el-input>
                        </el-form-item>
                    </div>
                    <span style="margin-bottom: 20px;padding-left: 20px;font-size: 16px;">重点数据周期展示</span>
                    <div style="margin: 5px 0 20px 0;padding: 0 20px;">
                    	<el-table class="com-table" ref="multipleTable" :data="infodata" tooltip-effect="dark"
                    		size="small" border style="width: 100%">

                    		<el-table-column label="开始时间" width="250px">
                    			<template slot-scope="scope">
                                    <el-date-picker v-model="scope.row.startTime" type="date" :disabled="disableStatus">
                                    </el-date-picker>
                    			</template>
                    		</el-table-column>
                            <el-table-column label="结束时间"  width="250px">
                    			<template slot-scope="scope">
                                    <el-date-picker v-model="scope.row.endTime" type="date" :disabled="disableStatus">
                                    </el-date-picker>
                    			</template>
                    		</el-table-column>
                            <el-table-column label="事项">
                    			<template slot-scope="scope">
                    				<el-input v-model="scope.row.remark" :disabled="disableStatus"></el-input>
                    			</template>
                    		</el-table-column>

                    		<el-table-column label="操作" width="120px" align="center" v-if="titname != '查看'">
                    			<template slot-scope="scope">
                    				<el-button size="mini" type="primary" @click="choline(1,scope)">添加</el-button>
                    				<el-button size="mini" type="danger" @click="choline(2,scope)">删除</el-button>
                    			</template>
                    		</el-table-column>
                    	</el-table>
                    </div>
                </el-form>
                <div slot="footer" style="float: right;margin-top: 20px;">
                    <el-button type="primary" @click="dialogCliadd(2)">确 定</el-button>
                    <el-button @click="dialogCliadd(1)">取 消</el-button>
                </div>
            </div>
        </el-dialog>
        <myMessage :messTit='messTit' @closeMessage="closeMessage" :centerDialogVisible="centerDialogVisible"
            v-if="centerDialogVisible"></myMessage>
    </fullscreen>

</template>
<script>
    import mySearch from "../../../components/search.vue";
    import myMessage from "../../../components/myMessage.vue"
    import toolList from '../../../components/toolList'
    import myUpload from '../../../components/upload'
    import uploadDown from '../../../components/uploadDown.vue'
    import deptTreeOnly from "../../../components/deptTreeOnly.vue"

    export default {
        components: {
            mySearch,
            myMessage,
            toolList,
            myUpload,
            uploadDown,
            deptTreeOnly
        },
        data() {
            const docName = (rule, value, callback) => {
                if (!this.infolist.docName) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
             const validDay = (rule, value, callback) => {
                if (!this.infolist.validDay) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            const startTime = (rule, value, callback) => {
                if (!this.infolist.startTime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
             const endTime = (rule, value, callback) => {
                if (!this.infolist.endTime) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                rules: {
                    docName: [{
                        required: true,
                        trigger: 'blur',
                        validator: docName
                    }],
                    validDay: [{
                        required: true,
                        trigger: 'blur',
                        validator: validDay
                    }],
                    startTime: [{
                        required: true,
                        trigger: 'change',
                        validator: startTime
                    }],
                    endTime: [{
                        required: true,
                        trigger: 'change',
                        validator: endTime
                    }],
                },
                searchList: [{
                        type: 'input',
                        tit: '文档名称',
                        value: '',
                        width: '32%'
                    },
                    {
                        type: 'date',
                        tit: '开始时间',
                        value: '',
                        width: '32%',
                    },
                    {
                        type: 'date',
                        tit: '结束时间',
                        value: '',
                        width: '32%',
                    },
                ],
                tooltit: '新兴市场产品文档管理',
                fullscreen: false,
                total: 0,
                pageSize: 1,
                tableData: [{}],
                dialogStatus: false,
                disableStatus: false,
                titname: '',
                infolist: {
                    startTime:'',
                    endTime:''
                },
                userInfo: {},
                params: {},
                centerDialogVisible: false,
                messTit: '',
                delid: '',
                loading: false,
                loadinged: false,
                docType:[],
                contentType:[],
                infodata:[{}],
            }
        },
        methods: {
            //搜索数据
            searchInfo(v) {
                this.params = {};
                v[0] ? this.params.docName = v[0] : '';
                v[1] ? this.params.startTime = this.$formatDate(v[1], "YYYY-MM-DD") : '';
                v[2] ? this.params.endTime = this.$formatDate(v[2], "YYYY-MM-DD") : '';
                this.getList(this.params, this.pageSize);
            },
            choline(v, n) {
            	if (v == 1) {
            		this.infodata.push({})
            	} else {
            		this.infodata.splice(n.$index, 1)
            	}
            },
            closedia() {
                this.infolist = {
                    startTime:'',
                    endTime:''
                };
                this.infodata = [{}];
                this.dialogStatus = false;
            },
            //获取列表
            getList(v, n) {
                this.pageSize = n;
                let _this = this;
                this.loading = true;
                this.$http({
                    url: "/market/cNewlyDoc/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;
                });
            },
            // 分页
            currchange(v) {
                this.pageSize = v;
                this.getList(this.params, this.pageSize);
            },
            //申请
            dialogCheck(v, n) {
                this.dialogStatus = true;
                let infolist = Object.assign({}, n);
                if (v === 1) {
                    this.titname = '查看';
                    this.disableStatus = true;
                } else if (v === 2) {
                    this.titname = '修改';
                    this.disableStatus = false;
                } else if (v === 3) {
                    this.titname = '添加';
                    this.disableStatus = false;
                    return
                }
                this.infolist = infolist;
                this.$http({
                    url: "/market/cNewlyDoc/query",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        id: n.id
                    },
                }).then((res) => {
                    this.infodata = res.data.detailList;
                });
            },
            //添加
            dialogCliadd(v) {
                if (v == 1 || this.titname == '查看') {
                    this.infolist = {
                        startTime:'',
                        endTime:''
                    };
                    this.infodata = [{}];
                    this.dialogStatus = false;
                    return
                }
                let _this = this;
                let info = this.infolist;
                info.opTime = _this.$formatDate(new Date(), "YYYY-MM-DD");
                info.opNo = this.userInfo.loginNo;
                info.opName = this.userInfo.loginName;
                info.startTime = info.startTime ? _this.$formatDate(info.startTime, "YYYY-MM-DD") : '';
                info.endTime = info.endTime ? _this.$formatDate(info.endTime, "YYYY-MM-DD") : '';
                info.detailList = this.infodata;
                if(info.detailList.length == 0){
                    this.infodata.push({})
                }

                if (this.titname == '修改') {
                    this.submitInfo("/market/cNewlyDoc/update", info);
                } else {
                    this.submitInfo("/market/cNewlyDoc/add", info);
                }
            },

            submitInfo(u, v) {
                let _this = this;
                this.$refs.infolist.validate(valid => {
                    if (valid) {
                        this.$http({
                            url: u,
                            method: "post",
                            headers: {
                                "Content-Type": "application/json",
                            },
                            data: v,
                        }).then((res) => {
                            if (res.data.result === 1) {
                                _this.$message({
                                    message: res.data.desc,
                                    type: 'error'
                                });
                            } else {
                                _this.$message({
                                    message: '成功',
                                    type: 'success'
                                });
                                _this.closedia();
                                _this.infolist = {
                                    startTime:'',
                                    endTime:''
                                };
                                _this.infodata = [{}];
                                _this.getList(this.params, this.pageSize);
                            }

                        });
                    }
                })
            },
            closeMessage(v) {
                this.centerDialogVisible = false;
                let _this = this;
                if (v === 1) {
                    _this.$http({
                        url: "/market/cNewlyDoc/del",
                        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);
                        }
                    });
                }
            },
            //删除
            delLine(v) {
                this.centerDialogVisible = true;
                this.messTit = '即将删除此条数据, 是否删除?';
                this.delid = v.id;
            },
            //功能栏
            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.$http({
                    url: "/market/cfgDataDict/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        dictCode: 'docType'
                    },
                }).then((res) => {
                    this.docType = res.data;
                });
                this.$http({
                    url: "/market/cfgDataDict/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        dictCode: 'contentType'
                    },
                }).then((res) => {
                    this.contentType = res.data;
                });
            },
        },
        mounted() {
            this.getList({}, 1);
            this.getUser();
        },
        created() {

        }
    }
</script>
<style scoped lang="scss">
    .onetab {
        margin-bottom: 20px;
        padding: 0 20px;
    }

    .titbox {
        div {
            float: right;

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

    .tabbox {
        margin-top: 15px;
    }

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

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

        div {
            width: 50%;
            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% - 120px);
        }
    }

    .online {
        width: 100%;

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

        span {
            vertical-align: top;
        }

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

        .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;
            }
        }
    }

    .adv-type {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        // border: 1px solid #ddd;
        border-radius: 5px;
        padding: 20px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);

        div {
            width: 33%;
            text-align: center;
            height: 80px;
            overflow: hidden;
            min-width: 100px;
            cursor: pointer;
            padding-top: 10px;
            margin: 10px 0;
        }

        div:hover {
            background: #CFE8FC;
            border-radius: 5px;
        }

        span {
            width: 100%;
            display: inline-block;
            height: 40px;

            // line-height: 40px;
            i {
                color: #0074D9;
                font-size: 36px;
            }
        }
    }
</style>
