<template>
    <!-- 采集列表 -->
    <div>
        <div class="title">
            <el-row>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">资讯题目</div>
                        <el-input v-model="message_name" placeholder="请输入资讯名称"></el-input>
                    </div>
                </el-col>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">来源</div>
                        <el-input v-model="message_from" placeholder="请输入来源"></el-input>
                    </div>
                </el-col>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">导入状态</div>
                        <el-select v-model="message_state" placeholder="请选择导入状态">
                            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                </el-col>
                <el-col :span="6" class="right">
                    <div class="btnList">
                        <button class="search" @click="goSearch">搜索</button>
                        <button class="reset" @click="goReset">重置</button>
                    </div>
                </el-col>
            </el-row>
        </div>
        <!--表格内容 start------------------------------------------------------------>
        <div class="layerBox">
            <tableTitle :name="tableDivTitle" />
            <div class="btnList">
                <button class="navigation" @click="toNavigation">关联导航池</button>
                <button class="consultList" @click="importMessage">导入资讯列表</button>
            </div>

            <el-row>
                <template>
                    <el-table class="my-table" v-loading="loading" :data="tableData" style="width: 100%">
                        <el-table-column fixed prop="id" label="编号" width="80">
                        </el-table-column>
                        <el-table-column prop="title" label="资讯题目" width="230">
                        </el-table-column>
                        <el-table-column prop="oldtitle" label="原始资讯题目" width="230">
                        </el-table-column>
                        <el-table-column prop="category.name" label="导航池名称" width="120">
                        </el-table-column>
                        <el-table-column prop="source" label="来源" width="120">
                        </el-table-column>
                        <el-table-column label="导入状态" prop="state" width="100">
                        </el-table-column>
                        <el-table-column prop="author" label="作者" width="90">
                        </el-table-column>
                        <el-table-column prop="created_at" label="采集时间" width="160">
                        </el-table-column>
                        <el-table-column prop="updated_at" label="修改时间" width="160">
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="200">
                            <template slot-scope="scope">
                                <div class="listBtnBox">
                                    <div class="listDeleteBtn" @click="deleteRow(scope.row.id, scope.row)"><i
                                            class="el-icon-delete"></i>删除</div>
                                    <div class="listEditBtn" @click="goEdit(scope.row.id, scope.row)"><i
                                            class="el-icon-edit-outline"></i>编辑</div>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>
            </el-row>
        </div>
        <!--分页 start------------------------------------------------------------>
        <div class="alignBox">
            <el-row>
                <el-col :span="24">
                    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="page" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                        :total="total">
                    </el-pagination>
                </el-col>
            </el-row>
        </div>
        <!--分页 end------------------------------------------------------------>
        <!--表格内容 end------------------------------------------------------------>

        <!-- 弹出框 start------------------------------------------------------------>
        <el-dialog title="关联导航池" :visible.sync="dialogTableVisible" width="50%" :close-on-click-modal="false">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
                <div class="dialogText">
                    <el-form-item label="关联导航池:" prop="name">
                        <el-select v-model="ruleForm.name" placeholder="请选择关联的导航池名称">
                            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <div class="dialogBtn">
                    <button class="cancel" @click="cancelForm">取消</button>
                    <button class="submit" @click="submitForm">提交</button>
                </div>
            </el-form>
        </el-dialog>
        <!-- 弹出框 end------------------------------------------------------------>
    </div>

</template>

<script>
//表格标题
import tableTitle from './components/tableTitle.vue';
//引入公用样式
import '@/styles/global.less';
import { getInfo, updateInfo, delInfo, addCatid, addArt } from '@/api/crawler'

export default {
    components: {
        tableTitle,//表格标题
    },
    data() {
        return {
            tableDivTitle: "任务规则列表",//表格标题
            // searchWebName: "", //搜索网站名称
            // dialogName:'关联导航池',
            loading: true,

            //跳转传递的参数
            ruleId: "", //规则id
            source: "", //规则来源
            state: 0,  //导入状态

            //搜索框
            message_name: '',//资讯名称
            message_from: '', //来源
            message_state: '', //导入状态

            //分页相关
            page: 1,
            pageSize: 10,
            total: 20,

            tableData: [], //表格内数据
            // currentPage: 4,
            dialogTableVisible: false,
            // dialogName: '',
            ruleForm: {
                name: '',
            },
            rules: {   //规则
                name: [
                    { required: true, message: '请选择关联的导航池名称', trigger: 'blur' },
                ]
            },

            // dialog 关联导航池
            options: [],

            statusOptions: [
                {
                    value: '0',
                    label: '未导入'
                },
                {
                    value: '1',
                    label: '已导入'
                },
            ]
        }
    },
    mounted() {
        // 接收跳转传递的参数
        this.ruleId = this.$route.query.id
        this.source = this.$route.query.source
        //请求数据
        this.getData()
    },
    methods: {
        //1.列表和分页相关 start ------------------------------------------------------------>
        //1.1 开始请求列表信息方法
        getData() {
            getInfo({
                // rule_id: 12,
                rule_id: this.ruleId,
                page: this.page,
                pageSize: this.pageSize
            }).then(data => {
                console.log(data);
                if (data.code == 200) {
                    this.loading = false
                }
                this.total = data.data.count
                this.tableData = data.data.rep

                for (let item of this.tableData) {
                    if (item.state == 1) {
                        item.state = "已导入"
                    } else {
                        item.state = "未导入"
                    }
                }


            })
        },
        //1.2 删除内容
        deleteRow(id, val) {
            this.$confirm('注意:删除后，该条信息及其绑定关系全部删除', '是否确认删除该条信息？', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(() => {
                delInfo({
                    art_id: id
                }).then(data => {
                    console.log(data);
                    if (data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getData()
                        console.log("当前删除：" + id)
                    } else if (data.code == 0) {
                        this.$message({
                            type: 'error',
                            message: data.message
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消删除'
                });
            });
        },
        //1.3 修改网站状态

        //1.4 列表内容分页
        //直接跳转
        handleSizeChange(val) {
            this.page = val;
            this.getData();
        },
        //1.5 点击分页
        handleCurrentChange(val) {
            this.page = val;
            this.getData();
        },
        // 1.6 搜索按钮
        goSearch() {
            getInfo({
                rule_id: this.ruleId,
                page: this.page,
                pageSize: this.pageSize,
                title: this.message_name,
                source: this.message_from,
                state: this.message_state
            }).then(data => {
                console.log(data);
                this.total = data.data.count
                this.tableData = data.data.rep
            })
        },
        //1.7 重置按钮
        goReset() {
            this.message_name = '',
                this.message_from = '',
                this.message_state = ''
            this.getData()
        },
        //列表和分页相关 end ------------------------------------------------------------>

        //1.8 编辑
        goEdit(id, val) {
            console.log(id, val);
            // this.$router.push('/webCrawlerListEdit')
            this.$router.push({
                path: '/webCrawlerListEdit',
                query: {
                    ruleId: this.ruleId,
                    source: this.source,
                    id: id,
                    // data: val
                }
            })
        },
        //1.9 关联导航池
        toNavigation() {
            addCatid({
                rule_id: this.ruleId,
            }).then(data => {
                console.log(data);
                if (data.code == 200) {
                    this.dialogTableVisible = true
                    this.options = data.data
                }
                if (data.code == 0) {
                    this.$message({
                        message: data.message,
                        type: 'error'
                    })
                }
                this.getData()
            })
        },

        //关联导航池的取消按钮
        cancelForm() {
            this.dialogTableVisible = false
        },


        //关联导航池的提交按钮
        submitForm() {
            addCatid({
                rule_id: this.ruleId,//任务规则id
                cat_arr_id: this.ruleForm.name,//导航池栏目id
            }).then(data => {
                console.log(this.ruleForm.name);
                console.log(data);
                if (data.code == 200) {
                    this.dialogTableVisible = false
                    this.$message({
                        message: '关联成功!',
                        type: 'success'
                    })
                    this.getData()
                }
                if (data.code == 0) {
                    this.$message({
                        message: data.message,
                        type: 'error'
                    })
                    this.getData()
                }
            })
        },


        //2.0 导入资讯列表
        importMessage() {
            for (let item of this.tableData) {
                if (item.category.name == '') {
                    this.$message({
                        message: '还未关联导航池,请先关联导航池',
                        type: 'error'
                    })
                } else {
                    this.$confirm('', '是否确认将所有未导入的信息导入资讯列表？', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                    }).then(() => {
                        addArt({
                            rule_id: this.ruleId
                        }).then(data => {
                            console.log(data);
                            if (data.code == 200) {
                                this.$message({
                                    message: '导入成功',
                                    type: 'success'
                                })
                                this.getData()
                            }
                            if (data.code == 0) {
                                this.$message({
                                    message: data.message,
                                    type: 'error'
                                })
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消'
                        });
                    });
                }
            }


        }
    },
}
</script>

<style scoped lang="less">
.title {
    margin: 30px 30px 20px 30px;
    padding: 30px 30px 40px 30px;
    background-color: #fff;
    border-radius: 20px 20px 20px 20px;
    border: 1px solid #E9EDF7;

    .left {
        float: left;
    }

    .right {
        float: right;
    }

    .searchBox {
        margin-right: 30px;

        .searchTitle {
            padding-bottom: 10px;
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 14px;
            color: #999999;
            line-height: 16px;
        }

        .el-select {
            // width: 300px;
            display: inline-block;
            position: relative;
        }
    }

    .btnList {
        float: right;
        padding-top: 28px;

        button {
            // width: 120px;
            height: 38px;
            border: none;
            border-radius: 8px;
            padding: 0 20px;
        }

        .search {
            background-color: #5570f1;
            color: #fff;
            margin-right: 20px;
        }

        .reset {
            font-family: Microsoft YaHei, Microsoft YaHei;
            font-weight: 400;
            font-size: 16px;
            color: #333333;
            background: #F5F7FB;
            border-radius: 8px 8px 8px 8px;
            border: 1px solid rgba(85, 112, 241, 0.11);
        }
    }
}

.layerBox {
    padding: 30px 20px;
    position: relative;


    .btnList {
        position: absolute;
        top: 30px;
        right: 20px;
        height: 38px;

        >button {
            color: #fff;
            background-color: #5570f1;
            border: none;
            border-radius: 8px;
            padding: 8px 28px 9px;
            box-sizing: border-box;
        }

        >button:first-child {
            margin-right: 30px;
        }
    }

    .listBtnBox {
        justify-content: left;
    }


    .listDeleteBtn,
    .listEditBtn,
    .listLookBtn {
        margin-left: 0px;
        padding-left: 0px;
        margin-right: 20px;
        width: 76px;
        height: 36px;
        line-height: 36px;
    }

    .listLookBtn {
        text-align: center;
        border-radius: 8px;
        cursor: pointer;
        color: #999999;
        background-color: rgba(153, 153, 153, 0.16);

        >i {
            padding-right: 8px;
        }
    }
}

// 弹出层内容
.dialogText {
    margin: 0 7px 0 3px;
    padding-top: 20px;
    padding-bottom: 1px;
    padding: 20px 60px 1px 20px;
    background-color: #f5f7fb;
}

// 弹出层按钮
.dialogBtn {
    text-align: center;
    margin: 50px auto 20px;

    button {
        width: 184px;
        padding: 16px;
        font-family: Microsoft YaHei, Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        border: none;
        border-radius: 12px 12px 12px 12px;
    }

    // 取消
    .cancel {
        color: #333333;
        background-color: #f5f7fb;
        border: 2px solid rgba(85, 112, 241, 0.11);
    }

    // 提交
    .submit {
        color: #fff;
        background-color: #5570F1;
        margin-left: 40px;
    }
}

::v-deep .el-form-item {
    margin-bottom: 50px;
}

::v-deep .el-select {
    width: 100%;
}

::v-deep .el-input--medium,
::v-deep .el-form-item__label {
    line-height: 36px;
    font-size: 16px;
}
</style>