<div class="ai-content" id="ai-content">
  <div class="header">
    <button type="button" class="close"><span>&times;</span></button>
  </div>
  <div class="modal-body clearfix">
    <h2><img src="{{ asset('data/assets/images/ai_1.png') }}" alt="ai_1"> AI帮写</h2>

    <div class="form-group clearfix">
      <div class="ai-request">
        {!! Form::textarea("request", '', ['class'=>'form-control', 'rows'=>5, 'placeholder' => '请输入文本要求（每天最多提交30次）']) !!}
        <button type="button" class="btn btn-primary sendBtn" @click="generate" :disabled="ai.disabled">一键生成</button>
      </div>
    </div>

    <div class="form-group clearfix" v-if="ai.content">
      <div class="operate">
        <span @click="apply"><i class="iconfont icon-xiugaiziliao"></i> 一键应用</span>
        <span class="copy-btn-ai copy-text" @click="copy"><i class="iconfont icon-copy"></i> 复制</span>
      </div>
      <div class="chatgpt">
        <textarea rows="22" placeholder="" name="ai_text" cols="50" class="form-control chatgpt-txt" v-model="ai.text"></textarea>
      </div>
    </div>

  </div>
</div>

<script>
    new Vue({
        el: '#ai-content',
        data: {
            type: 1,
            disabled: false,
            token: '',
            lang: "",
            ai: {
                disabled: false,
                content: '',
                text: ''
            }
        },
        methods: {
            generate: function () {
                let _this = this
                let keyword = $('textarea[name="request"]').val()
                if (keyword == '') {
                    swal({
                        title: "",
                        text: '请填写要求！',
                        type: "error",
                        showConfirmButton: false,
                        timer: 1500,
                    });
                    return false
                }

                _this.ai.disabled = true

                $.ajax({
                    type: "POST",
                    url: HOME_URL + '/admin/collect-search',
                    data: {keyword: keyword, type: 3},
                    dataType: 'json',
                    success: function (data) {
                        if (data.status == 'success') {
                            _this.token = data.token
                            _this.lang = data.lang

                            $.ajax({
                                headers: {
                                    "Authorization": _this.token
                                },
                                type: "GET",
                                url: "{{ config('setting.chat_url') }}",
                                data: {wd: '请用"' + _this.lang + '"写包含关键词 "' + keyword + '" 的内容 字数不少于1000字，不要出现公司名称',notify_url: HOME_URL + '/api/ai-search',source: HOME_URL},
                                dataType: 'json',
                                success: function (data) {
                                    if (data) {
                                        let text = data.text
                                        text = text.replace(/#/g, "");
                                        text = text.replace(/\*/g, "");
                                        _this.ai.text = text
                                        _this.ai.content = text.replace(/\n\n/g, "<br/>");
                                        _this.ai.content = text.replace(/\n/g, "<br/>");
                                    } else {
                                        swal({
                                            title: "",
                                            text: '生成失败！',
                                            type: "error",
                                            showConfirmButton: false,
                                            timer: 1500,
                                        });
                                    }

                                    _this.ai.disabled = false
                                },
                                error: function (err) {
                                    _this.ai.disabled = false
                                    swal({
                                        title: "",
                                        text: '请求超时！',
                                        type: "error",
                                        showConfirmButton: false,
                                        timer: 1500,
                                    });
                                    return false;
                                }
                            })
                        } else {
                            swal({
                                title: "",
                                text: data.message,
                                type: "error",
                                showConfirmButton: false,
                                timer: 1500,
                            });
                            _this.ai.disabled = false
                            return false;
                        }
                    },
                    error: function (err) {
                        _this.ai.disabled = false
                        swal({
                            title: "",
                            text: '请求过于频繁，请稍后重试！',
                            type: "error",
                            showConfirmButton: false,
                            timer: 1500,
                        });
                        return false;
                    }
                })
            },
            copy: function () {
                var clipboard = new ClipboardJS(".copy-text", {
                    container: document.getElementById('ai-content'),
                    text: function () {
                        return $('textarea[name="ai_text"]').val()
                    }
                });
                swal({
                    title: "",
                    text: "复制成功。",
                    type: "success",
                    showConfirmButton: false,
                    timer: 1500,
                });
            },
            apply: function () {
                let _this = this
                let ue = UE.getEditor('editor-' + '{{ config('managerLang')  }}');
                ue.execCommand('insertHtml', _this.ai.content);
            }
        }
    })

    $(".ai-icon").click(function () {
        $(".ai-content").addClass('active')
    })
    $(".ai-content .close").click(function () {
        $(".ai-content").removeClass('active')
    })
</script>