<template>
    <!-- 建立网站 -->
    <div>
        <div class="title">
            <el-row>
                <el-col :span="6" class="left">
                    <div class="searchBox">
                        <div class="searchTitle">网站名称:</div>
                        <el-input v-model="webName" placeholder="请输入网站名称"></el-input>
                    </div>
                </el-col>
                <el-col :span="10" class="right">
                    <div class="btnList">
                        <button class="search" @click="goSearch">搜索</button>
                        <button class="reset" @click="reset">重置</button>
                    </div>
                </el-col>
            </el-row>
        </div>
        <!--表格内容 start------------------------------------------------------------>
        <div class="layerBox">
            <tableTitle :name="tableDivTitle" />
            <button class="btn" @click="addWebsite">添加网站</button>
            <el-row>
                <template>
                    <el-table class="my-table"  v-loading="loading" :data="tableData" style="width: 100%">
                        <el-table-column fixed prop="id" label="编号" width="100">
                        </el-table-column>
                        <el-table-column prop="name" label="网站名称" width="">
                        </el-table-column>
                        <el-table-column prop="url" label="网站网址" width="">
                        </el-table-column>
                        <el-table-column prop="created_at" label="创建时间" width="">
                        </el-table-column>
                        <el-table-column prop="updated_at" label="修改时间" width="">
                        </el-table-column>
                        <el-table-column fixed="right" label="操作" width="330">
                            <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 class="listLookBtn" @click="goLook(scope.row.id, scope.row)"><i
                                            class="el-icon-view"></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="dialogName" :visible.sync="dialogTableVisible" width="50%" :close-on-click-modal="false">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <div class="dialogText">
                    <el-form-item label="网站名称:" prop="name">
                        <el-input v-model="ruleForm.name" :disabled="this.dialogName == '查看' ? true : false"
                            placeholder="请输入采集的源网站名称"></el-input>
                    </el-form-item>
                    <el-form-item label="网站地址:" prop="url">
                        <el-input v-model="ruleForm.url" :disabled="this.dialogName == '查看' ? true : false"
                            placeholder="请输入网站地址（即网站首页URL）"></el-input>
                    </el-form-item>
                    <el-form-item label="规则类型:" prop="ruleType" class="keywords">
                        <el-select v-model="ruleForm.ruleType" placeholder="请选择规则类型" :disabled="disabledType">
                            <el-option label="一般规则" value="1"></el-option>
                            <!-- <el-option label="接口规则" value="2"></el-option>
                            <el-option label="自定义规则" value="3"></el-option> -->
                        </el-select>
                        <el-tooltip class="item" effect="dark" content="列表翻页后，网址变化的为一般接口；列表翻页后，网址没变化的为接口规则。"
                            placement="top-start">
                            <i class="el-icon-warning"></i>
                        </el-tooltip>
                    </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';
// 引入api
import { addWeb, getWeb, updateWeb, delWeb } from '@/api/crawler'

export default {
    components: {
        tableTitle,//表格标题
    },
    data() {
        return {
            tableDivTitle: "网站列表",  //表格标题
            webName: '', // 网站名称
            activeId: '', // 被编辑的网站的id
            activeType: '', //被编辑网站的type
            disabled: true, //是否禁用
            loading:true,


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

            //表格相关
            tableData: [],

            //弹出框相关
            dialogTableVisible: false,  //是否显示
            dialogName: '',     //弹框标题名
            disabledType: true,  // 编辑的时候类型不可以改变

            //表单相关
            ruleForm: {
                name: '',
                url: '',
                ruleType: "",
            },
            rules: {   //规则
                name: [
                    { required: true, message: '请输入采集的源网站名称', trigger: 'blur' },
                ],
                url: [
                    { required: true, message: '请输入网站地址（即网站首页URL）', trigger: 'blur' }
                ],
                ruleType: [
                    { required: true, message: '请选择规则类型', trigger: 'blur' }
                ]
            },

        }
    },
    methods: {
        //1.列表和分页相关 start ------------------------------------------------------------>
        //1.1 获取网站列表数据
        getData() {
            getWeb({
                page: this.page,
                pageSize: this.pageSize,
            }).then(data => {
                this.loading=false
                console.log(data);
                this.tableData = data.data.rep
                this.total = data.data.count
            })
        },
        //1.2 删除内容
        deleteRow(id, val) {
            this.activeId = id
            this.activeType = val.type
            this.$confirm('注意:删除后，该条信息及其绑定关系全部删除', '是否确认删除该条信息？', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(() => {
                console.log("当前删除：" + id)
                delWeb({
                    // id:12,
                    id: this.activeId,
                    type: this.activeType,
                }).then(data => {
                    console.log(data);
                    if (data.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getData()
                    } else if (data.code == 0) {
                        this.$message({
                            type: 'error',
                            message: data.message
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'warning',
                    message: '已取消'
                });
            });
        },

        //1.3 列表内容分页
        //直接跳转
        handleSizeChange(val) {
            this.page = val;
            this.getData();
        },
        //1.4 点击分页
        handleCurrentChange(val) {
            this.page = val;
            this.getData();
        },

        // 1.5 搜索按钮
        goSearch() {
            if (this.webName) {
                getWeb({
                    page: this.page,
                    pageSize: this.pageSize,
                    keyWord: this.webName
                }).then(data => {
                    // console.log(data);
                    this.total = data.data.count
                    this.tableData = data.data.rep
                })
            } else {
                getWeb({
                    page: this.page,
                    pageSize: this.pageSize,
                    keyWord: this.webName
                }).then(data => {
                    // console.log(data.message);
                    this.$message.error(data.message);
                })
            }
        },
        //1.6 重置按钮
        reset() {
            this.webName = ''
            this.getData()
        },
        //列表和分页相关 end ------------------------------------------------------------>

        //1.7 添加
        addWebsite() {
            this.dialogTableVisible = true
            this.dialogName = "添加"
            this.disabledType = false //添加的时候类型可以选择
            this.ruleForm.name = ""
            this.ruleForm.url = ""
            this.ruleForm.ruleType = ""
        },
        //1.8 编辑
        goEdit(id, val) {
            this.dialogTableVisible = true //弹框显示
            this.dialogName = "编辑"    //弹框名字
            this.disabledType = true //编辑的时候类型不可以改变
            this.activeId = id
            this.activeType = val.type
            this.ruleForm.name = val.name
            this.ruleForm.url = val.url
            if (val.type == 1) {
                this.ruleForm.ruleType = "规则类"
            } else if (val.type == 2) {
                this.ruleForm.ruleType = "接口类"
            } else if (val.type == 3) {
                this.ruleForm.ruleType = "自定义"
            }
        },
        //1.9 查看
        goLook(id, val) {
            // console.log(id);
            // console.log(val.type);
            // console.log(val.name);
            console.log(this.ruleForm.ruleType);


            //跳转到规则列表页
            this.$router.push({
                path: '/webRule',
                query: {
                    id: id,
                    type: val.type,
                    name: val.name
                }
            })
        },

        // 弹出层相关方法
        // 提交表单
        submitForm() {
            if (!this.ruleForm.name) {
                this.$message.error("请将数据填写完整");
                return
            }
            if (!this.ruleForm.url) {
                this.$message.error("请将数据填写完整");
                return
            }
            if (!this.ruleForm.ruleType) {
                this.$message.error('请将数据填写完整')
                return
            }
            if (this.dialogName == "添加") {
                //添加
                addWeb({
                    name: this.ruleForm.name,
                    url: this.ruleForm.url,
                    type: this.ruleForm.ruleType
                }).then(data => {
                    console.log(data);
                    if (data.code == 0) {
                        this.$message.error(data.message)
                        this.dialogTableVisible = false
                    } else if (data.code == 200) {
                        this.$message({
                            message: data.data,
                            type: 'success'
                        });
                        this.dialogTableVisible = false
                        this.getData()
                    }
                })
            } else if (this.dialogName == "编辑") {
                updateWeb({
                    name: this.ruleForm.name,
                    url: this.ruleForm.url,
                    id: this.activeId,
                }).then(data => {
                    console.log(data);
                    if (data.code == 0) {
                        this.$message({
                            message: data.message,
                            type: 'error'
                        })
                    } else if (data.code == 200) {
                        this.$message({
                            message: "修改成功",
                            type: 'success'
                        })
                        this.dialogTableVisible = false
                        this.getData()
                    }
                })
            } else if (this.dialogName == "查看") {
                this.dialogTableVisible = false
            }
        },
        //取消添加或编辑
        cancelForm() {
            this.dialogTableVisible = false
        },
    },
    mounted() {
        //获取网站列表数据
        this.getData()

    }
}
</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 {
        .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 30px;
        }

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

    .btn {
        position: absolute;
        top: 30px;
        right: 20px;
        height: 38px;
        color: #fff;
        background-color: #5570f1;
        border: none;
        border-radius: 8px;
        padding: 8px 28px 9px;
        box-sizing: border-box;
    }

    .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: #55b5f1;
        background-color: rgba(85, 181, 241, 0.16);

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

    // from表单中每条数据的下外边距
    ::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;
    }
}

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

    // from表单中每条数据的下外边距
    ::v-deep .el-form-item {
        margin-bottom: 50px;
    }

    //下拉框宽度
    ::v-deep .el-select {
        width: 100%;
    }

    .keywords {
        position: relative;

        i {
            position: absolute;
            top: 0;
            left: -15px;
        }
    }
}

// 弹出层按钮
.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;
    }
}
</style>