<!-- 新增爬虫 -->
<template>
    <el-dialog :title="dialogTitle" v-model="dialogVisible" :close-on-click-modal="false" width="80%">
        <template #header="{ titleId, titleClass }">
            <div class="edit-css-header">
                <div class="header-left">
                    <span :id="titleId" :class="titleClass">{{ dialogTitle }}</span>
                </div>
                <div class="mark-container">
                    <el-button-group class="ml-4">
                        <el-button type="primary" :icon="Link" @click="handleDataTag">标注</el-button>
                    </el-button-group>
                </div>
            </div>
        </template>
        <div class="flex gap-2">
            <el-space class="edit-tag-list">
                <el-tag class="edit-tag" type="primary">{{ formData.site }} </el-tag>
                <el-tag class="edit-tag" type="success">{{ formData.channel }} </el-tag>
                <el-tag class="edit-tag" type="info">{{ formData.code }} </el-tag>
                <el-tag class="edit-tag" type="warning">{{ formData.href }} </el-tag>
                <el-tag class="edit-tag" type="danger">{{ formData.modifyuser }} </el-tag>
            </el-space>
        </div>
        <div class="space" />
        <el-form ref="form0" label-width="160px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="列表延迟时间(MS)">
                        <el-input v-model="formData.listDelayTime" placeholder="500"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="列表翻页延迟时间(MS)">
                        <el-input v-model="formData.listTurnDelayTime" placeholder="500"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="详情页延迟时间(MS)">
                        <el-input v-model="formData.contentDelayTime" placeholder="500"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="采集最大页数">
                        <el-input v-model="formData.maxPages" :placeholder="defaultFormValue.maxPages"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        
        <el-tabs v-model="activeName" class="demo-tabs">
            <el-tab-pane label="列表页CSS选择器" name="first">
                <el-form ref="form1" :model="formData" label-width="160px">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="条目区域块">
                                <el-input v-model="formData.listBodyCss" placeholder="div"></el-input>
                                <div class="form-item-sub-line">ctrl+2</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="条目">
                                <el-input v-model="formData.listItemCss" placeholder="ul.list li"></el-input>
                                <div class="form-item-sub-line">ctrl+3</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="条目链接">
                                <el-input v-model="formData.listLinkCss" placeholder="a"></el-input>
                                <div class="form-item-sub-line">ctrl+4</div>
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="条目发布时间">
                                <el-input v-model="formData.listPublishTimeCss" placeholder="span"></el-input>
                                <div class="form-item-sub-line">ctrl+5</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="翻页下一页">
                                <el-input v-model="formData.listNextPageCss" placeholder="li.nextpage a"></el-input>
                                <div class="form-item-sub-line">ctrl+6</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="space" />
            </el-tab-pane>

            <el-tab-pane label="详情页CSS选择器" name="content">
                <el-form ref="form2" :model="formData" label-width="160px">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="详情页标题">
                                <el-input v-model="formData.titleCss" placeholder="ul.list li"></el-input>
                                <div class="form-item-sub-line">ctrl+alt+3</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="详情页发布时间">
                                <el-input v-model="formData.publishTimeCss" placeholder="a"></el-input>
                                <div class="form-item-sub-line">ctrl+alt+4</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="详情页发布单位">
                                <el-input v-model="formData.publishUnitCss" placeholder="span"></el-input>
                                <div class="form-item-sub-line">ctrl+alt+5</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="详情页正文">
                                <el-input v-model="formData.contentCss" placeholder="li.nextpage a"></el-input>
                                <div class="form-item-sub-line">ctrl+alt+6</div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="详情页附件">
                                <el-input v-model="formData.attachCss" placeholder="span"></el-input>
                                <div class="form-item-sub-line">ctrl+alt+7</div>
                            </el-form-item>
                        </el-col>

                    </el-row>
                </el-form>

                <div class="space" />
            </el-tab-pane>
            <el-tab-pane label="列表页JS代码" name="list_item_js">
                <template #label><span class="custom-tabs-label">
                        <el-icon>
                            <ElementPlus />
                        </el-icon>
                        <span>列表页JS代码</span>
                    </span>
                </template>
                <el-divider>手写列表页提取JS代码 <el-button type="primary"
                        @click='editorHandle.ImportListCode'>导入样例</el-button></el-divider>
                <el-row>
                    <el-input v-model="formData.listJs" class="codeEditor" :rows="6" type="textarea"
                        placeholder="Please input" />
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="列表页翻页JS代码" name="list_trunpage_js">

                <template #label><span class="custom-tabs-label">
                        <el-icon>
                            <ElementPlus />
                        </el-icon>
                        <span>列表页翻页JS代码</span>
                    </span>
                </template>
                <el-divider>手写列表页翻页JS代码 <el-button type="primary"
                        @click='editorHandle.ImportListTrunPageCode'>导入样例</el-button></el-divider>
                <el-row>
                    <el-input v-model="formData.listTurnPageJs" class="codeEditor" :rows="6" type="textarea"
                        placeholder="Please input" />
                </el-row>
            </el-tab-pane>

            <el-tab-pane label="详情页JS代码" name="content_js">

                <template #label><span class="custom-tabs-label">
                        <el-icon>
                            <ElementPlus />
                        </el-icon>
                        <span>详情页JS代码</span>
                    </span>
                </template>
                <el-divider>手写附件下载/上传JS代码 <el-button type="primary"
                        @click='editorHandle.ImportContentCode'>导入样例</el-button></el-divider>
                <el-row><el-input v-model="formData.contentJs" class="codeEditor" :rows="6" type="textarea"
                        placeholder="Please input" />
                </el-row>
            </el-tab-pane>
        </el-tabs>
        <div slot="footer" class="dialog-footer" style="text-align:right">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleSave">保 存</el-button>
        </div>
    </el-dialog>
</template>
<script setup>
import { ref, defineEmits } from 'vue';
import { TemplateJsCode } from './jscodetpl.js'
import { Link } from '@element-plus/icons-vue'
const emit = defineEmits(['custom-event', 'data-tag']);
let originData = {}

const dialogTitle = ref('仅编辑 CSS选择器部分')

const defaultFormValue = {
    delayTime: 500,
    maxPages: 1,
}
//表单数据
const formData = ref({
    site: '',
    channel: '',
    href: '',
    code: '',
    modifyuser: '',

    // list-css-tab
    listBodyCss: '',
    listItemCss: '',
    listLinkCss: '',
    listPublishTimeCss: '',
    listNextPageCss: '',
    listDelayTime: '',

    // content-css-tab
    titleCss: '',
    publishTimeCss: '',
    publishUnitCss: '',
    contentCss: '',
    attachCss: '',
    contentDelayTime: '',

    // list-js-tab
    listJs: '',

    // list-pageination-tab
    listTurnPageJs: '',
    listTurnDelayTime: '',

    // detail-js-tab
    contentJs: '',

    maxPages: '',
});

const activeName = ref("first")
const dialogVisible = ref(false)

//编辑器事件管理
const editorHandle = {
    ImportListCode: () => {
        formData.value.listJs = TemplateJsCode.ListJsCode
    },
    ImportContentCode: () => {
        formData.value.contentJs = TemplateJsCode.ContentJsCode
    },
    ImportAttachCode: () => {
        formData.value.attachJs = TemplateJsCode.AttachJsCode
    },
    ImportListTrunPageCode:()=>{
        formData.value.listTurnPageJs=TemplateJsCode.ListTurnPageJsCode
    }
}

const setPageData = (row) => {
    if (!row) return
    formData.value = row
    // 保存一份原始数据
    originData = JSON.parse(JSON.stringify(row))
}
const refreshPageData = (key, value) => {
    if (!key) return
    formData.value[key] = value
}

const getPageData = () => {
    const formDataValue = formData.value
    const payload = {
      // list-css
      listBodyCss: formDataValue.listBodyCss || '',
      listItemCss: formDataValue.listItemCss || '',
      listLinkCss: formDataValue.listLinkCss || '',
      listPublishTimeCss: formDataValue.listPublishTimeCss || '',
      listNextPageCss: formDataValue.listNextPageCss || '',
      listDelayTime: Number(formData.value.listDelayTime || defaultFormValue.delayTime),
      // content-css
      titleCss: formDataValue.titleCss || '',
      publishTimeCss: formDataValue.publishTimeCss || '',
      publishUnitCss: formDataValue.publishUnitCss || '',
      contentCss: formDataValue.contentCss || '',
      attachCss: formDataValue.attachCss || '',
      contentDelayTime: Number(formDataValue.contentDelayTime || defaultFormValue.delayTime),
      // js-list
      listJs: formDataValue.listJs || '',
      // js_nextpage
      listTurnPageJs: formDataValue.listTurnPageJs || '',
      listTurnDelayTime: Number(formDataValue.listTurnDelayTime || defaultFormValue.delayTime),
      // js_content
      contentJs: formDataValue.contentJs || '',

      maxPages: Number(formDataValue.maxPages || defaultFormValue.maxPages)
    }

    return {
        _originData: originData,
        value: payload
    }
}

const handleSave = () => {
    // dialogVisible.value = false;
    const payload = getPageData()
    emit("custom-event", payload)
    // formData.value = {}
}

const handleDataTag = () => {
    emit('data-tag', originData)
}

//这里是重点: 向外部组建暴露可供调用的方法
defineExpose({
    dialogVisible,
    formData,
    setPageData,
    getPageData,
    refreshPageData,
    emit
})
</script>
<style scoped lang="scss">
::v-deep {
    .el-form-item {
        margin-bottom: 10px;
    }
}

.codeEditor {
    width: 100%;
    font-size: 12pt;
    font-weight: bold;
    line-height: 1.5em;
    padding: 10px;
}
.edit-tag-list {
    flex-wrap: wrap;
}
.edit-tag {
    margin-bottom: 4px;
}
.edit-css-header {
    display: flex;
}
.mark-container {
    margin-left: 20px;
}
.form-item-sub-line {
    line-height: 24px;
    color: var(--el-color-primary);
}
</style>