<template>
    <div class="configuration-modal">
        <div>
            <a-form-model
                ref="ruleForm"
                class="config-form-modal"
                :model="configForm"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
            >
                <p class="separate-title-header">{{ modalTitle === 'add' ? '新增' : '编辑'}}配置信息</p>
                <div class="separate-module-con">
                    <a-form-model-item label="广告账户">
                        <acount-search :disabled="modalTitle === 'edit'" class="acount-search-class" :appId.sync="configForm.accountId" :multiple="false" request="1,3"></acount-search>
                    </a-form-model-item>
                    <a-form-model-item label="dp链接范围" class="dp-link-start" prop="dpLinkStartIndex">
                        <a-input class="common-input" :disabled="modalTitle === 'edit'" v-model="configForm.dpLinkStartIndex" placeholder="请输入" allow-clear/>
                        <span>~</span>
                    </a-form-model-item>
                    <a-form-model-item label="" class="dp-link-end" prop="dpLinkEndIndex">
                        <a-input class="common-input" :disabled="modalTitle === 'edit'" v-model="configForm.dpLinkEndIndex" placeholder="请输入" allow-clear/>
                    </a-form-model-item>
                    <a-form-model-item label="预算方式">
                        <a-radio-group v-model="configForm.accountBudgetMode" button-style="solid">
                            <a-radio-button value="1">不限于算</a-radio-button>
                            <a-radio-button value="2">指定预算</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.accountBudgetMode === '2'" label="账户日预算" prop="accountBudget">
                        <a-input-number v-model="configForm.accountBudget" :min="1000" :max="9999999.99" :step="0.01"/> 元
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">广告组信息</p>
                    <a-form-model-item label="推广目标">
                        <a-radio-group :disabled="modalTitle === 'edit'" v-model="configForm.campaignLandingType" button-style="solid">
                            <a-radio-button value="APP">应用</a-radio-button>
                            <a-radio-button value="LINK" disabled>销售线索收集</a-radio-button>
                            <a-radio-button value="SHOP" disabled>电商店铺推广</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="广告类型">
                        <a-radio-group :disabled="modalTitle === 'edit'" v-model="configForm.advertisingType" button-style="solid">
                            <a-radio-button value="1">信息流广告</a-radio-button>
                            <a-radio-button value="2">搜索广告</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">基础信息</p>
                    <a-form-model-item label="广告组名称" prop="campaignName">
                        <a-input class="common-input-long" v-model="configForm.campaignName" allow-clear placeholder="请输入广告组名称"/>
                    </a-form-model-item>
                    <a-form-model-item label="广告计划名称" prop="adName">
                        <a-input class="common-input-long" id="groupName" v-model="configForm.adName" allow-clear placeholder="请输入广告计划名称"/>
                        <div>
                            <a-tag v-for="item in adNameTags" :key="item" color="#1890ff" @click="handleGetChangeAll(item, 'groupName')">+ {{ item }}</a-tag>
                        </div>
                    </a-form-model-item>
                    <a-form-model-item label="创建数量: 广告组数量" class="campaign-cnt-class" prop="campaignCnt">
                        <a-input-number v-model="configForm.campaignCnt" :min="1"/>
                        <a-tooltip placement="top">
                            <template slot="title">
                                <span>输入的数量为每天所创建的广告组数量</span>
                                <span>以及每个广告组创建的广告计划数量</span>
                            </template>
                            <a-icon class="form-itrm-icon" type="smile-o"/>
                        </a-tooltip>
                    </a-form-model-item>
                    <a-form-model-item label="广告计划数量" class="campaign-ad-class" prop="campaignAdCnt">
                        <a-input-number v-model="configForm.campaignAdCnt" :min="1"/>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">投放内容</p>
                    <a-form-model-item label="投放目标">
                        <a-radio-group :disabled="modalTitle === 'edit'" v-model="configForm.launchTarget" button-style="solid">
                            <a-radio-button value="OCPM">转量化</a-radio-button>
                            <a-radio-button value="CPC" disabled>点击量</a-radio-button>
                            <a-radio-button value="CPM" disabled>展示量</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item :disabled="modalTitle === 'edit'" label="下载方式">
                        <a-radio-group v-model="configForm.adDownloadType" button-style="solid">
                            <a-radio-button value="DOWNLOAD_URL">下载链接</a-radio-button>
                            <a-radio-button value="EXTERNAL_URL">落地页</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.adDownloadType === 'EXTERNAL_URL'" label="落地页链接">
                        <a-input class="common-input-long" :disabled="modalTitle === 'edit'" v-model="configForm.adExternalUrl" placeholder="请输入落地页链接" allow-clear/>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.adDownloadType === 'EXTERNAL_URL'" label="直达链接内容">
                        <a-input class="common-input-long" v-model="configForm.adOpenUrl" placeholder="请输入直达链接内容" allow-clear/>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.adDownloadType === 'DOWNLOAD_URL'" label="下载链接">
                        <a-input class="common-input-long" :disabled="modalTitle === 'edit'" v-model="configForm.adDownloadUrl" placeholder="请输入下载链接" allow-clear @change="handleDownloadLink"/>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.adDownloadType === 'DOWNLOAD_URL'" label="应用包名">
                        <a-input class="common-input-long" :disabled="modalTitle === 'edit'" v-model="configForm.adPackage" placeholder="请输入应用包名" allow-clear/>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">优化目标</p>
                    <a-form-model-item label="转化跟踪方式">
                        <a-select class="common-input-long" :disabled="modalTitle === 'edit'" v-model="configForm.adConvertType" placeholder="请选择" @change="handleConvertTypeChange">
                            <a-select-option value="AD_CONVERT_SOURCE_TYPE_APP_DOWNLOAD">应用下载API</a-select-option>
                            <a-select-option value="AD_CONVERT_SOURCE_TYPE_SDK">应用下载SDK</a-select-option>
                            <a-select-option value="AD_CONVERT_SOURCE_TYPE_OPEN_URL">应用直达API（应用直达链接）</a-select-option>
                            <a-select-option value="AD_CONVERT_SOURCE_TYPE_NORMAL_APP_DOWNLOAD">普通应用下载</a-select-option>
                        </a-select>
                    </a-form-model-item>
                    <a-form-model-item label="优化目标">
                        <a-select class="common-input-long" :disabled="modalTitle === 'edit'" v-model="configForm.adConvertId" placeholder="请选择" @change="handleConvertChange">
                            <a-select-option
                                v-for="(item, index) in optimizationOption"
                                :key="index"
                                :value="item.convert_id "
                            >
                                {{ item.external_name }}
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">设置投放位置</p>
                    <a-form-model-item label="广告位置">
                        <a-radio-group :disabled="modalTitle === 'edit'" v-model="configForm.adverPosition" button-style="solid">
                            <a-radio-button value="a">系统优选广告位</a-radio-button>
                            <a-radio-button value="b">按媒体指定位置</a-radio-button>
                            <a-radio-button value="c">按场景指定位置</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <p v-if="configForm.adverPosition === 'a'">系统将根据您设置的定向，选择预估效果最好的广告位进行投放，并触达尽可能多的目标用户</p>
                        <div v-if="configForm.adverPosition === 'c'" class="position-radio-change">
                            <p>位置选择</p>
                            <a-radio-group v-model="configForm.radioChangeValue">
                                <a-radio :style="radioStyle" value="VIDEO_SCENE">沉浸式竖版视频场景</a-radio>
                                <a-radio :style="radioStyle" value="FEED_SCENE">信息流场景</a-radio>
                                <a-radio :style="radioStyle" value="TAIL_SCENE">视频后贴和尾帧场景</a-radio>
                            </a-radio-group>
                        </div>
                        <div v-if="configForm.adverPosition === 'b'" class="position-radio-change">
                            <p>位置选择</p>
                            <a-checkbox-group v-model="configForm.checkboxChangeValue">
                                <a-checkbox :style="radioStyle" value="INVENTORY_FEED">今日头条</a-checkbox>
                                <a-checkbox :style="radioStyle" value="INVENTORY_VIDEO_FEED">西瓜视频</a-checkbox>
                                <a-checkbox :style="radioStyle" value="INVENTORY_HOTSOON_FEED">抖音火山版</a-checkbox>
                                <a-checkbox :style="radioStyle" value="INVENTORY_AWEME_FEED">抖音短视频</a-checkbox>
                                <a-checkbox :style="radioStyle" value="INVENTORY_UNION_SLOT">穿山甲</a-checkbox>
                            </a-checkbox-group>
                        </div>
                    </div>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">用户定向--新建定向</p>
                    <a-form-model-item label="地域">
                        <a-radio-group v-model="configForm.adDistrict" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="CITY">按省市</a-radio-button>
                            <a-radio-button value="COUNTY">按区县</a-radio-button>
                            <a-radio-button value="BUSINESS_DISTRICT">按商圈</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="性别">
                        <a-radio-group v-model="configForm.adGender" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="GENDER_MALE">男</a-radio-button>
                            <a-radio-button value="GENDER_FEMALE">女</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="年龄">
                        <a-radio-group v-model="configForm.adAge" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="AGE_BETWEEN_18_23">18-23</a-radio-button>
                            <a-radio-button value="AGE_BETWEEN_24_30">24-30</a-radio-button>
                            <a-radio-button value="AGE_BETWEEN_31_40">31-40</a-radio-button>
                            <a-radio-button value="AGE_BETWEEN_41_49">41-49</a-radio-button>
                            <a-radio-button value="AGE_ABOVE_50">50+</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="自定义人群">
                        <a-radio-group v-model="configForm.adRetargetingTagsInclude" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="a" disabled>自定义人群</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="媒体定向">
                        <a-radio-group v-model="configForm.adSuperiorPopularityType" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="GAME" disabled>游戏优质媒体</a-radio-button>
                            <a-radio-button value="b" disabled>自定义</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="平台">
                        <a-radio-group v-model="configForm.adPlatform" button-style="solid">
                            <a-radio-button value="NONE" :disabled="!!configForm.adDownloadUrl">不限</a-radio-button>
                            <a-radio-button value="IOS" :disabled="!!configForm.adDownloadUrl">IOS</a-radio-button>
                            <a-radio-button value="ANDROID">Andriod</a-radio-button>
                            <a-radio-button value="PC" :disabled="!!configForm.adDownloadUrl">PC</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.adDownloadUrl" label="安卓版本">
                        <a-radio-group v-model="configForm.adAndroidOsv" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="a">自定义</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <div v-if="configForm.adAndroidOsv === 'a'" class="position-radio-change">
                            <p>安卓版本</p>
                            <a-radio-group v-model="configForm.androidChangeValue">
                                <a-radio :style="radioStyle" value="8.0">Andriod 8.0以上</a-radio>
                                <a-radio :style="radioStyle" value="7.1">Andriod 7.1以上</a-radio>
                                <a-radio :style="radioStyle" value="7.0">Andriod 7.0以上</a-radio>
                                <a-radio :style="radioStyle" value="6.0">Andriod 6.0以上</a-radio>
                            </a-radio-group>
                        </div>
                    </div>
                    <a-form-model-item label="网络">
                        <a-radio-group v-model="configForm.adAc" button-style="solid">
                            <a-radio-button value="NONE">不限</a-radio-button>
                            <a-radio-button value="WIFI">Wi-Fi</a-radio-button>
                            <a-radio-button value="2G">2G</a-radio-button>
                            <a-radio-button value="3G">3G</a-radio-button>
                            <a-radio-button value="4G">4G</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="已安装用户">
                        <a-radio-group v-model="configForm.adHideIfExists" button-style="solid">
                            <a-radio-button value="0">不限</a-radio-button>
                            <a-radio-button value="1">过滤</a-radio-button>
                            <a-radio-button value="2">定向</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item v-if="configForm.adConvertType" label="过滤已转化用户">
                        <a-radio-group v-model="configForm.adHideIfConverted" button-style="solid">
                            <a-radio-button value="NO_EXCLUDE">不限</a-radio-button>
                            <a-radio-button value="AD">广告计划</a-radio-button>
                            <a-radio-button value="CAMPAIGN">广告组</a-radio-button>
                            <a-radio-button value="ADVERTISER">广告账户</a-radio-button>
                            <a-radio-button value="CUSTOMER">公司账户</a-radio-button>
                            <a-radio-button value="APP">APP</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="智能放量">
                        <a-radio-group v-model="configForm.adAutoExtendEnabled" button-style="solid">
                            <a-radio-button value="0">不开启</a-radio-button>
                            <a-radio-button value="1">开启</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">预算与出价</p>
                    <a-form-model-item label="投放场景">
                        <a-radio-group v-model="configForm.adSmartBidType" button-style="solid">
                            <a-radio-button value="SMART_BID_CUSTOM" :disabled="modalTitle === 'edit'">常规投放</a-radio-button>
                            <a-radio-button value="SMART_BID_CONSERVATIVE" disabled>放量投放</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="预算">
                        <a-input-group compact>
                            <a-select :disabled="modalTitle === 'edit'" v-model="configForm.adBudgetMode" @change="handleBudgetMode">
                                <a-select-option value="BUDGET_MODE_DAY">日预算</a-select-option>
                                <a-select-option value="BUDGET_MODE_TOTAL">总预算</a-select-option>
                            </a-select>
                            <a-input-number class="common-input" v-model="configForm.adBudget" :min="1" :step="0.01"/>
                        </a-input-group>
                    </a-form-model-item>
                    <a-form-model-item label="投放时间">
                        <a-radio-group v-model="configForm.adScheduleType" button-style="solid">
                            <a-radio-button value="SCHEDULE_FROM_NOW">从今天起长期投放</a-radio-button>
                            <a-radio-button value="SCHEDULE_START_END">设置开始和结束日期</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <div v-if="configForm.adScheduleType === 'SCHEDULE_START_END'" class="position-radio-change">
                            <a-range-picker v-model="configForm.launchDateRange" format="YYYY-MM-DD" @change="handleLaunchData"/>
                        </div>
                    </div>
                    <a-form-model-item label="投放时段">
                        <a-radio-group v-model="configForm.launchTimeInterval" button-style="solid">
                            <a-radio-button value="buxian">不限</a-radio-button>
                            <a-radio-button value="a">指定时间段</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <div v-if="configForm.launchTimeInterval === 'a'" class="position-radio-change">
                            <selectCheckAll
                                style="width:100%"
                                :scheduleTime.sync="configForm.scheduleTimeData"
                                ref="selectCheck"
                            />
                        </div>
                    </div>
                    <a-form-model-item label="付费方式">
                        <a-radio-group v-model="configForm.adPricing" button-style="solid">
                            <a-radio-button value="PRICING_OCPM" :disabled="modalTitle === 'edit'">按展示付费（oCPM）</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="目标转化出价">
                        <a-input-number class="common-input" v-model="configForm.adCpaBid" :min="0.1" :max="10000" :step="0.01"/> 元
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">第三方检测链接</p>
                    <a-form-model-item label="点击监测">
                        <a-input class="common-input-long" placeholder="请输入链接地址" v-model="configForm.creativeActionTrackUrl" allow-clear/>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">创意信息</p>
                    <a-form-model-item label="创意方式">
                        <a-radio-group v-model="configForm.creativeMaterialMode" button-style="solid">
                            <a-radio-button value="STATIC_ASSEMBLE" :disabled="modalTitle === 'edit'">程序化创意</a-radio-button>
                            <a-radio-button value="NONE" :disabled="modalTitle === 'edit'">自定义创意</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="创意内容">
                        <a-radio-group v-model="configForm.creativeImageMode" button-style="solid">
                            <a-radio-button value="CREATIVE_IMAGE_MODE_VIDEO_VERTICAL">竖版视频</a-radio-button>
                            <a-radio-button value="CREATIVE_IMAGE_MODE_VIDEO">横版视频</a-radio-button>
                            <a-radio-button value="CREATIVE_IMAGE_MODE_LARGE">大图横图</a-radio-button>
                            <a-radio-button value="CREATIVE_IMAGE_MODE_SMALL">小图</a-radio-button>
                            <a-radio-button value="CREATIVE_IMAGE_MODE_LARGE_VERTICAL">大图竖图</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <a-form-model-item label="卡片主图">
                        <a-upload
                            name="avatar"
                            list-type="picture-card"
                            class="avatar-uploader"
                            :show-upload-list="false"
                            :before-upload="beforeUpload"
                            @change="handleChange"
                        >
                            <div v-if="imageUrl" class="upload-img-class" @mouseover="handleChangeActive">
                                <div class="img-flex-url">
                                    <img :src="imageUrl" alt="avatar"/>
                                </div>
                                <!-- TODO 划过图片显示覆盖层 -->
                                <!-- <div v-if="isCoverImgDiv" class="cover-img-div" @mouseleave="handleRemoveActive">
                                    <a-icon class="delete-icon-class" type="delete" @click.stop="handleDeleteImg"/>
                                </div> -->
                            </div>
                            <div v-if="!imageUrl && !isCoverImgDiv">
                                <a-icon type="plus" style="margin-top: 10px;"/>
                                <div class="ant-upload-text">Upload</div>
                            </div>
                        </a-upload>
                    </a-form-model-item>
                    <a-form-model-item label="卡片标题" prop="creativeProductDescription">
                        <a-input class="common-input-long" placeholder="请输入卡片标题" v-model="configForm.creativeProductDescription" allow-clear/>
                    </a-form-model-item>
                    <a-form-model-item label="推广卖点">
                        <a-button type="primary" @click="handleAddOrigin"><a-icon type="plus" />推广卖点</a-button>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <a-form-model-item
                            v-for="(item, index) in configForm.defaultFormList"
                            :key="item.id"
                            class="examin-form-model"
                            :prop="'defaultFormList.' + index + '.value'"
                            :rules="[
                                {required: true, message: '请输入', trigger: 'blur'},
                                {min: 1, max: 10, message: '长度位6-9字符', trigger: 'blur'}
                            ]"
                        >
                            <a-input class="common-input-long" v-model="item.value" placeholder="请输入" allow-clear/>
                            <a-icon class="examin-form-icon" type="delete" @click="handleDelOrigin(item)"/>
                        </a-form-model-item>
                    </div>
                    <a-form-model-item label="行动号召">
                        <a-radio-group v-model="configForm.creativeEnablePersonalAction" button-style="solid">
                            <a-radio-button :value="true">开启智能优选</a-radio-button>
                            <a-radio-button :value="false">关闭智能优选</a-radio-button>
                        </a-radio-group>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <a-form-model-item label="">
                            <a-input class="common-input-long" placeholder="请输入" v-model="configForm.creativeCallToAction" allow-clear/>
                        </a-form-model-item>
                    </div>
                    <a-form-model-item label="来源">
                        <a-input class="common-input-long" placeholder="请输入来源" v-model="configForm.creativeSource" allow-clear/>
                    </a-form-model-item>
                </div>
                <div class="separate-module-con">
                    <p class="rock-title">创意分类</p>
                    <a-form-model-item label="创意分类" prop="creativeThirdIndustryIds">
                        <a-cascader
                            v-model="configForm.creativeThirdIndustryIds"
                            class="common-input-long"
                            :options="createOptions"
                            :fieldNames="fieldNames"
                            placeholder="请选择创意分类"
                        />
                    </a-form-model-item>
                    <a-form-model-item label="创意标签">
                        <a-select
                            v-model="configForm.creativeLabel"
                            mode="tags"
                            style="width: 320px"
                            placeholder="按回车生成标签"
                            dropdownClassName="display-none-selset"
                            :token-separators="[',', ' ', '，']"
                            @change="handleEnterChang"
                        >
                        </a-select>
                    </a-form-model-item>
                    <div class="adver-position-radio">
                        <div class="creative-label-content">
                            <p>已选：{{ creativeTag.length || 0 }}</p>
                            <div class="label-content-select-tag">
                                <a-tag
                                    v-for="item in creativeTag"
                                    :key="item"
                                    :closable="true"
                                    class="tag-select-class"
                                    @close="handleTagsClose(item)"
                                >
                                    {{ item }}
                                </a-tag>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="separate-module-con btn-Unique-con">
                    <a-button class="sure-btn" type="primary" @click="handleRelaxSure">确定</a-button>
                    <a-button class="cancel-btn" @click="handleRelaxCancel">取消</a-button>
                </div>
            </a-form-model>
        </div>
    </div>
</template>
<script>
import AcountSearch from '@/views/modules/Statistics/components/Treeselect.vue';
import SelectCheckAll from '../../BatchCreate/toutiaoTime';

import moment from 'moment';
import BMF from 'browser-md5-file';

import {getAction, postAction, putAction} from '@/api/manage';
import {mapGetters} from 'vuex';

let COS = require('cos-js-sdk-v5');
let cos = new COS({
    SecretId: 'AKIDE6IpMi8fJQRCg1iuWzFajjRs43kbbets',
    SecretKey: 'tXzuwMfplTTK3c9GFUyETilasvQfePu9'
});
let str = '';
for (let i = 0; i < 7 * 48; i++) {
    str += '0';
}
export default {
    name: 'configuration-modal',
    props: {
        visible: {
            type: Boolean,
            default: false
        }
    },
    components: {
        AcountSearch,
        SelectCheckAll
    },
    data() {
        return {
            detailData: {},
            modalTitle: '',
            toolConvertList: [],
            optimizationOption: [],
            md5Result: '',
            urlResult: '',
            createOptions: [],
            adNameTags: ['出价方式', '素材名称', '日期', 'dp链接标识', '创意方式'],
            fieldNames: {
                label: 'industryName',
                value: 'industryId',
                children: 'children'
            },
            fileList: [],
            isCoverImgDiv: false,
            imageUrl: '',
            creativeTag: [],
            radioStyle: {
                display: 'block',
                height: '30px',
                lineHeight: '30px',
                marginLeft: 0
            },
            labelCol: {span: 4},
            wrapperCol: {span: 14},
            rules: {
                campaignCnt: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                campaignAdCnt: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                dpLinkStartIndex: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                dpLinkEndIndex: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                accountBudget: [
                    {required: true, message: '请输入', trigger: 'blur'}
                ],
                campaignName: [
                    {required: true, message: '请输入广告组名称', trigger: 'blur'},
                    {min: 1, max: 5, message: '长度位1-100字符', trigger: 'blur'}
                ],
                adName: [
                    {required: true, message: '请输入广告计划名称'},
                    {min: 1, max: 100, message: '长度位1-100字符'}
                ],
                creativeProductDescription: [
                    {required: true, message: '请输入', trigger: 'blur'},
                    {min: 1, max: 7, message: '长度位1-7字符', trigger: 'blur'}
                ],
                creativeThirdIndustryIds: [{required: true, message: '请选择', trigger: 'change'}]
            },
            configForm: {
                defaultFormList: [],
                productSellingPoints: [], // 商品买点
                adBudgetMode: 'BUDGET_MODE_DAY', // 预算类型
                adDownloadUrl: '', // 下载链接
                accountBudget: '', // 账户日预算
                accountId: undefined, // 账户
                dpLinkStartIndex: '',
                dpLinkEndIndex: '',
                accountBudgetMode: '1', // 预算方式
                campaignLandingType: 'APP', // 推广目标
                advertisingType: '1', // 广告类型
                campaignName: '', // 广告组名称
                adName: '', // 广告计划名称
                campaignCnt: 0, // 广告组计划数量
                campaignAdCnt: 0, // 广告计划数量
                launchTarget: 'OCPM', // 投放目标
                adDownloadType: 'DOWNLOAD_URL', // 下载方式
                adExternalUrl: '', // 落地页链接
                adOpenUrl: '', // 直达链接内容
                adPackage: '', // 应用包名
                adConvertType: '', // 转化跟踪方式
                adConvertId: '', // 优化目标  --  走接口
                adverPosition: 'a', // 广告位置
                radioChangeValue: '', // 广告位置-按照场景指定位置
                checkboxChangeValue: [], // 广告位置-按照媒体指定位置
                adDistrict: 'NONE', // 定向-区域
                adGender: 'NONE', // 定向-性别
                adAge: 'NONE', // 定向-年龄
                adRetargetingTagsInclude: 'NONE', // 定向-自定义
                adSuperiorPopularityType: 'NONE', // 定向-媒体定向
                adPlatform: 'NONE', // 定向-平台
                adAc: 'NONE', // 定向-网络
                adHideIfExists: '0', // 定向-已安装用户
                adHideIfConverted: 'AD', // 定向-已转化用户
                adAutoExtendEnabled: '0', // 定向-智能播放
                adAndroidOsv: 'NONE', // 定向-安卓版本
                androidChangeValue: '', // 安卓版本-自定义
                adSmartBidType: 'SMART_BID_CUSTOM', // 投放场景
                adBudget: '', // 预算
                adScheduleType: 'SCHEDULE_FROM_NOW', // 投放时间
                launchDateRange: [], // 设置开始和结束时间
                launchTimeInterval: 'buxian', // 投放时段
                scheduleTimeData: str, // 指定时段选择
                adPricing: 'PRICING_OCPM', // 付费方式
                adCpaBid: '', // 目标转化出价
                creativeActionTrackUrl: '', // 监测地址
                creativeMaterialMode: 'STATIC_ASSEMBLE', // 创意方式
                creativeImageMode: 'CREATIVE_IMAGE_MODE_VIDEO_VERTICAL', // 创意内容
                creativeProductDescription: '', // 卡片标题
                creativeEnablePersonalAction: true, // 智能优选
                creativeSource: '', // 来源
                creativeThirdIndustryId: '', // 创意分类
                creativeThirdIndustryIds: [], // 创意分类
                adKeywords: [], // 创意标签
                creativeCallToAction: '', // 行动号召
                creativeLabel: undefined // 倡议标签
            }
        };
    },
    computed: {
        NewAountId() {
            return this.configForm.accountId;
        },
        NewAdPackage() {
            return this.configForm.adPackage;
        }
    },
    watch: {
        NewAdPackage(newVal, oldVal) {
            if (newVal && this.configForm.accountId) {
                this.handleGetOptimizationObjective();
            }
        },
        NewAountId(newVal, oldVal) {
            if (newVal && this.configForm.adPackage) {
                this.handleGetOptimizationObjective();
            }
            this.handleGetCreativeClassification();
        }
    },
    mounted() {
        const routeQuery = JSON.parse(localStorage.getItem('queryList'));
        if (routeQuery.accountModal === 'edit') {
            this.modalTitle = 'edit';
            this.handleGetDetailList();
        }
        else if (routeQuery.accountModal === 'add') {
            this.modalTitle = 'add';
        }
    },
    methods: {
        ...mapGetters(['nickname', 'avatar', 'userInfo']),
        moment,
        handleConvertChange(e) {
            const convertTitle = this.optimizationOption.filter(item => item.convert_id === e);
            this.configForm.creativeActionTrackUrl = convertTitle[0].action_track_url;
        },
        handleGetDetailList() {
            const routeQuery = JSON.parse(localStorage.getItem('queryList'));
            getAction('http://192.168.1.43:8080/bytedance-api/advertiser/aiBytedanceAdvertiserStrategy/queryById', {id: routeQuery.id}).then(result => {
                if (result.code === 0) {
                    const detailData = result.result;
                    this.configForm.accountId = detailData.accountId || '';
                    this.configForm.dpLinkStartIndex = detailData.dpLinkStartIndex || '';
                    this.configForm.dpLinkEndIndex = detailData.dpLinkEndIndex || '';
                    this.configForm.accountBudgetMode = detailData.accountBudgetMode || '';
                    this.configForm.accountBudget = detailData.accountBudget || '';
                    this.configForm.campaignLandingType = detailData.campaignLandingType || '';
                    this.configForm.advertisingType = detailData.advertisingType || '1';
                    this.configForm.campaignName = detailData.campaignName || '';
                    this.configForm.adName = detailData.adName || '';
                    this.configForm.campaignCnt = detailData.campaignCnt || '';
                    this.configForm.campaignAdCnt = detailData.campaignAdCnt || '';
                    this.configForm.launchTarget = detailData.launchTarget || 'OCPM';
                    this.configForm.adDownloadType = detailData.adDownloadType || 'DOWNLOAD_URL';
                    this.configForm.adExternalUrl = detailData.adExternalUrl || '';
                    this.configForm.adOpenUrl = detailData.adOpenUrl || '';
                    this.configForm.adDownloadUrl = detailData.adDownloadUrl || '';
                    this.configForm.adPackage = detailData.adPackage || '';
                    this.configForm.adConvertType = detailData.adConvertType || '';
                    this.configForm.adConvertId = detailData.adConvertId || '';
                    this.configForm.adDistrict = detailData.adDistrict || 'NONE';
                    this.configForm.adGender = detailData.adGender || 'NONE';
                    this.configForm.adAge = detailData.adAge || 'NONE';
                    this.configForm.adRetargetingTagsInclude = detailData.adRetargetingTagsInclude;
                    this.configForm.adSuperiorPopularityType = detailData.adSuperiorPopularityType;
                    this.configForm.adPlatform = detailData.adPlatform;
                    this.configForm.adAndroidOsv = detailData.adAndroidOsv;
                    this.configForm.adAc = detailData.adAc;
                    this.configForm.adHideIfExists = String(detailData.adHideIfExists);
                    this.configForm.adHideIfConverted = detailData.adHideIfConverted;
                    this.configForm.adAutoExtendEnabled = detailData.adAutoExtendEnabled;
                    this.configForm.adSmartBidType = detailData.adSmartBidType;
                    this.configForm.adBudgetMode = detailData.adBudgetMode;
                    this.configForm.adBudget = detailData.adBudget;
                    this.configForm.adScheduleType = detailData.adScheduleType;
                    this.configForm.launchDateRange[0] = detailData.adStartTime;
                    this.configForm.launchDateRange[1] = detailData.adEndTime;
                    this.configForm.adPricing = detailData.adPricing;
                    this.configForm.adCpaBid = detailData.adCpaBid;
                    this.configForm.creativeActionTrackUrl = detailData.creativeActionTrackUrl;
                    this.configForm.creativeMaterialMode = detailData.creativeMaterialMode;
                    this.configForm.creativeImageMode = detailData.creativeImageMode;
                    this.configForm.creativeProductDescription = detailData.creativeProductDescription;
                    this.configForm.creativeEnablePersonalAction = detailData.creativeEnablePersonalAction;
                    this.configForm.creativeCallToAction = detailData.creativeCallToAction;
                    this.configForm.creativeSource = detailData.creativeSource;
                    this.configForm.creativeThirdIndustryIds[0] = detailData.creativeThirdIndustryId.substr(0, 4);
                    this.configForm.creativeThirdIndustryIds[1] = detailData.creativeThirdIndustryId.substr(0, 6);
                    this.configForm.creativeThirdIndustryIds[2] = detailData.creativeThirdIndustryId;
                    this.imageUrl = detailData.imageUrl;
                    this.md5Result = detailData.creativeProductImageCode;
                    if (detailData.productSellingPoints) {
                        let productPointsList = [];
                        const randomNum = new Date().getTime() + Math.ceil(Math.random() * 1000);
                        detailData.productSellingPoints.forEach((item, index) => {
                            productPointsList.push({
                                id: randomNum + index,
                                value: item
                            });
                        });
                        this.configForm.defaultFormList = productPointsList;
                    }
                    if (detailData.adScheduleTime.indexOf(1) !== -1) {
                        this.configForm.launchTimeInterval = 'a';
                        this.configForm.scheduleTimeData = detailData.adScheduleTime;
                    }
                    else {
                        this.configForm.launchTimeInterval = 'buxian';
                    }
                    if (detailData.creativeSceneInventory) {
                        this.configForm.adverPosition = 'c';
                        this.radioChangeValue = detailData.creativeSceneInventory;
                    }
                    else if (detailData.creativeInventoryType) {
                        this.configForm.adverPosition = 'b';
                        this.configForm.checkboxChangeValue = detailData.creativeInventoryType.split(',');
                    }
                    else {
                        this.configForm.adverPosition = 'a';
                    }
                    if (detailData.adKeywords) {
                        this.creativeTag = detailData.adKeywords;
                    }
                    const paramsData = {
                        advancedCreativeType: 'ATTACHED_CREATIVE_NONE',
                        advertiserId: this.configForm.accountId,
                        appType: 'APP_ANDROID',
                        deliveryRange: 'DEFAULT',
                        externalUrl: this.configForm.adExternalUrl,
                        landingType: this.configForm.campaignLandingType,
                        packageName: this.configForm.adPackage,
                        promotionContent: this.configForm.adDownloadType
                    };
                    postAction('http://192.168.1.43:8080/bytedance-api/advertiser/aiBytedanceAdvertiserStrategy/getToolConvert', paramsData).then(result => {
                        if (result.code === 0) {
                            const updateResult = result.result.filter(item => item.external_actions.length > 0);
                            this.configForm.adConvertType = updateResult[0].convert_type;
                            this.optimizationOption = updateResult[0].external_actions;
                        }
                        else {
                            this.$message.error(result.message);
                        }
                    }).catch(error => {
                        console.log(error, 'eeee');
                    });
                }
                else {
                    this.$message.error(result.message);
                }
            }).catch(error => {
                console.log(error, 'eeee');
            });
        },
        handleConvertTypeChange(e) {
            this.toolConvertList.forEach(item => {
                if (item.convert_type === e) {
                    this.optimizationOption = item.external_actions;
                }
            });
        },
        // 优化目标数据获取
        handleGetOptimizationObjective() {
            const paramsData = {
                advancedCreativeType: 'ATTACHED_CREATIVE_NONE',
                advertiserId: this.configForm.accountId,
                appType: 'APP_ANDROID',
                deliveryRange: 'DEFAULT',
                externalUrl: this.configForm.adExternalUrl,
                landingType: this.configForm.campaignLandingType,
                packageName: this.configForm.adPackage,
                promotionContent: this.configForm.adDownloadType
            };
            postAction('http://192.168.1.43:8080/bytedance-api/advertiser/aiBytedanceAdvertiserStrategy/getToolConvert', paramsData).then(result => {
                if (result.code === 0) {
                    this.toolConvertList = result.result;
                    if (this.configForm.adConvertType) {
                        this.handleConvertTypeChange(this.configForm.adConvertType);
                    }
                }
                else {
                    this.$message.error(result.message);
                }
            }).catch(error => {
                console.log(error, 'eeee');
            });
        },
        // 创意分类接口
        handleGetCreativeClassification() {
            const paramsData = {
                accountId: this.configForm.accountId
            };
            getAction('http://192.168.1.43:8080/bytedance-api/advertiser/aiBytedanceAdvertiserStrategy/getIndustryList', paramsData).then(result => {
                if (result.code === 0) {
                    this.createOptions = result.result;
                }
                else {
                    this.$message.error(result.message);
                }
            }).catch(error => {
                console.log(error, 'eeee');
            });
        },
        handleAddList(data) {
            postAction('http://192.168.1.43:8080/bytedance-api/advertiser/aiBytedanceAdvertiserStrategy/addBytedanceStrategy', data).then(result => {
                if (result.code === 0) {
                    console.log(result, 'result--result');
                    this.$message.success('创建成功');
                    this.$router.replace('/account-config/account-config');
                    this.$bus.$emit('remove', '/account-config/components/configuration-modal');
                }
                else {
                    this.$message.error(result.message);
                }
            }).catch(error => {
                console.log(error, 'eeee');
            });
        },
        handleGetChangeAll(item, field) {
            let node = document.getElementById(field);
            this.configForm.adName += '-{{' + item + '}}-';
            node.focus();
            node.blur();
        },
        handleLaunchData(date, dateString) {
            this.configForm.launchDateRange = dateString;
        },
        handleBudgetMode(e) {
            if (e === 'BUDGET_MODE_TOTAL') {
                this.configForm.adScheduleType = 'SCHEDULE_START_END';
                this.$message.warning('请设置投放时间');
            }
        },
        handleDelOrigin(list) {
            let formData = this.configForm.defaultFormList;
            this.configForm.defaultFormList = formData.filter(item => list.id !== item.id);
        },
        handleAddOrigin() {
            if (this.configForm.defaultFormList.length > 10) {
                this.$message.error('最多可以添加10个卖点');
                return;
            }
            const randomNum = new Date().getTime() + Math.ceil(Math.random() * 1000);
            this.$nextTick(() => {
                this.configForm.defaultFormList.push({
                    id: randomNum,
                    value: ''
                });
            });
        },
        handleDownloadLink(e) {
            this.configForm.adPlatform = e.target.value ? 'ANDROID' : 'NONE';
        },
        handleDeleteImg() {
            this.imageUrl = '';
            this.isCoverImgDiv = false;
        },
        handleChangeActive() {
            this.isCoverImgDiv = true;
        },
        handleRemoveActive() {
            this.isCoverImgDiv = false;
        },
        getBase64(img, callback) {
            const reader = new FileReader();
            reader.addEventListener('load', () => callback(reader.result));
            reader.readAsDataURL(img);
        },
        beforeUpload(info) {
            this.handleMd5(info);
        },
        handleChange(info) {
            this.getBase64(info.file.originFileObj, imageUrl => {
                this.imageUrl = imageUrl;
            });
        },
        cosUpload(file) {
            let that = this;
            let date = new Date();
            let y = date.getFullYear();
            let MM = date.getMonth() + 1;
            MM = MM < 10 ? '0' + MM : MM;
            let d = date.getDate();
            d = d < 10 ? '0' + d : d;
            let timeElse = new Date().getTime();
            let arr = file.name.split('.');
            let str = '';
            for (let i = 0; i < arr.length; i++) {
                if (i === arr.length - 1) {
                }
                else {
                    if (i === arr.length - 2) {
                        str += arr[i];
                    }
                    else {
                        str += arr[i] + '.';
                    }
                }
            }
            cos.putObject(
                {
                    Bucket: 'media-1301855440',
                    Region: 'ap-chongqing',
                    // 存储桶所在地域，必须字段
                    Key: that.uploadType + '/' + y + '-' + MM + '-' + d + '/' + str + '-' + timeElse + '.' + arr[arr.length - 1],
                    StorageClass: 'STANDARD',
                    Body: file // 上传文件对象
                },
                function (err, data) {
                    if (err) {
                        that.$message.error('上传失败!!!' + err);
                        return;
                    }
                    that.loadingElse = false;
                    that.fileList.push({
                        uid: file.name,
                        name: file.name,
                        status: 'done',
                        url: '//' + data.Location
                    });
                    that.urlResult = '//' + data.Location;
                    console.log(that.fileList, 'fileListfileList--fileList');
                }
            );
        },
        handleMd5(file) {
            let bmf = new BMF();
            let that = this;
            return new Promise(function (resolve, reject) {
                bmf.md5(file, (err, md5) => {
                    that.md5Result = md5;
                    getAction('/file/imageCheck', {code: md5}).then(result => {
                        if (result.code === -206) {
                            that.cosUpload(file);
                            resolve();
                        }
                        else {
                            that.urlResult = result.url;
                            // reject();
                        }
                    }).catch(error => {
                        console.log(error, 'eeee');
                        reject();
                    });
                });
            });
        },
        handleRelaxSure() {
            let paramsFormData = this.configForm;
            if (paramsFormData.adverPosition === 'a') {
                paramsFormData.creativeSmartInventory = 0;
            }
            else if (paramsFormData.adverPosition === 'b') {
                paramsFormData.creativeInventoryTypes = paramsFormData.checkboxChangeValue;
            }
            else if (paramsFormData.adverPosition === 'c') {
                paramsFormData.creativeSceneInventory = paramsFormData.radioChangeValue;
            }
            if (paramsFormData.launchTimeInterval === 'buxian') {
                paramsFormData.adScheduleTime = '';
            }
            else if (paramsFormData.launchTimeInterval === 'a') {
                paramsFormData.adScheduleTime = paramsFormData.scheduleTimeData;
            }
            if (paramsFormData.adScheduleType === 'SCHEDULE_START_END') {
                paramsFormData.adStartTime = paramsFormData.launchDateRange[0];
                paramsFormData.adEndTime = paramsFormData.launchDateRange[1];
            }
            else if (paramsFormData.adScheduleType === 'SCHEDULE_FROM_NOW') {
                paramsFormData.adStartTime = '';
                paramsFormData.adEndTime = '';
            }
            if (paramsFormData.launchTimeInterval === 'buxian') {
                paramsFormData.adScheduleTime = str;
            }
            else if (paramsFormData.launchTimeInterval === 'a') {
                paramsFormData.adScheduleTime = paramsFormData.scheduleTimeData;
            }
            paramsFormData.productSellingPoints = this.configForm.defaultFormList && this.configForm.defaultFormList.map(item => item.value);
            paramsFormData.creativeThirdIndustryId = paramsFormData.creativeThirdIndustryIds[2];
            paramsFormData.creativeProductImageCode = this.md5Result;
            paramsFormData.imageUrl = this.urlResult;
            paramsFormData.adKeywords = this.creativeTag;
            paramsFormData.userId = this.userInfo().id;
            this.$refs.ruleForm.validate(valid => {
                if (this.modalTitle === 'add') {
                    this.handleAddList(paramsFormData);
                }
                else if (this.modalTitle === 'edit') {
                    this.handleEditList(paramsFormData);
                }
                // if (valid) {
                //     this.handleAddList(paramsFormData);
                // }
                // else {
                //     console.log('error submit!!');
                //     return false;
                // }
            });
        },
        handleEditList(data) {
            const queryData = data;
            const routeQuery = JSON.parse(localStorage.getItem('queryList'));
            queryData.id = routeQuery.id;
            putAction('http://192.168.1.43:8080/bytedance-api/advertiser/aiBytedanceAdvertiserStrategy/edit', queryData).then(result => {
                if (result.code === 0) {
                    this.$message.success(result.message);
                    this.$router.replace('/account-config/account-config');
                    this.$bus.$emit('remove', '/account-config/components/configuration-modal');
                }
                else {
                    this.$message.error(result.message);
                }
            }).catch(error => {
                console.log(error, 'eeee');
            });
        },
        handleRelaxCancel() {
            this.$emit('relaxCancel');
        },
        handleEnterChang(value) {
            this.creativeTag.push(...value);
            this.creativeTag = [...new Set(this.creativeTag)];
            for (let i = 0; i < this.creativeTag.length; i++) {
                if (this.creativeTag[i].length > 10) {
                    this.creativeTag.splice(i, 1);
                    this.$message.error('创意标签最多只能十个字符，超过的已清除');
                }
            }
            this.configForm.creativeLabel = [];
            if (this.creativeTag.length > 20) {
                this.creativeTag.splice(0, 20);
                this.$message.error('最多创建20个');
            }
        },
        handleTagsClose(removedTag) {
            const tags = this.creativeTag.filter(tag => tag !== removedTag);
            this.creativeTag = tags;
        }
    }
};
</script>
<style lang="less">
    @import "configuration-modal";
    .display-none-selset {
        display: none !important;
    }
</style>