<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="计划名称" prop="campaignName">
        <el-input
          placeholder="请输入计划名称"
          v-model="queryParams.campaignName"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="账户名称" prop="accountName">
        <el-input
          placeholder="请输入账户名称"
          v-model="queryParams.accountName"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-document-add"
          size="mini"
          @click="handleAdd"
          >新建配置</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
    >
      <el-table-column label="计划名称" align="center" prop="campaignName">
      </el-table-column>
      <el-table-column label="账户名称" align="center" prop="accountName">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ scope.row.createTime ? scope.row.createTime.split("T")[0] : "-" }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="nickName" />
      <el-table-column label="创建数量" align="center" prop="groupCount">
        <template slot-scope="scope">
          {{ scope.row.groupCount ? scope.row.groupCount : "-" }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleFunction(scope.row)"
            >执行</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAllocationOne(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="getLog(scope)"
            >创建记录</el-button
          >
          <el-button size="mini" type="text" @click="getDetail(scope)"
            >配置条件</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handlePersonVisible(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      title="配置条件"
      :before-close="handleClose"
      :visible.sync="dialog"
      custom-class="demo-drawer"
      ref="drawer"
      size="40%"
    >
      <el-form
        :model="form"
        ref="form"
        size="small"
        label-width="150px"
        :rules="rule"
        v-if="dialog"
        style="margin-bottom: 70px"
      >
        <el-form-item label="转化目标" prop="ocpxActionType">
          <el-checkbox-group v-model="form.ocpxActionType" @change="checkValue">
            <el-checkbox-button
              v-for="(item, index) in formList.ocpxActionTypeList"
              :label="item.value"
              :key="item.value"
              >{{ item.label }}</el-checkbox-button
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item
          label="涨粉成本上限"
          prop="typeOne"
          v-if="form.ocpxActionType.includes(72)"
        >
          <el-input
            placeholder="请输入成本上限"
            v-model="form.typeOne"
            style="width: 450px"
            v-if="form.ocpxActionType.includes(72)"
          >
            <template slot="append">元/购买</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="订单支付成本上限"
          prop="typeTwo"
          v-if="form.ocpxActionType.includes(395)"
        >
          <el-input
            placeholder="请输入成本上限"
            v-model="form.typeTwo"
            style="width: 450px"
            v-if="form.ocpxActionType.includes(395)"
          >
            <template slot="append">元/购买</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="推广ROI成本上限"
          prop="typeThree"
          v-if="form.ocpxActionType.includes(192)"
        >
          <el-input
            placeholder="请输入成本上限"
            v-model="form.typeThree"
            style="width: 450px"
            v-if="form.ocpxActionType.includes(192)"
          >
            <template slot="append">元/购买</template>
          </el-input>
        </el-form-item>
        <el-form-item
          label="直播间成本上限"
          prop="typeFour"
          v-if="form.ocpxActionType.includes(740)"
        >
          <el-input
            placeholder="请输入成本上限"
            v-model="form.typeFour"
            style="width: 450px"
            v-if="form.ocpxActionType.includes(740)"
          >
            <template slot="append">元/购买</template>
          </el-input>
        </el-form-item>
        <div v-for="(item, index) in form.targetList" :key="index">
          <el-form-item :label="item.name">
            <el-collapse v-model="item.active" accordion style="width: 90%">
              <el-collapse-item
                title="如果选择定向包,可显示定向包名称,自定义则显示定向条件"
                name="1"
              >
                <el-form-item
                  label="定向选择"
                  :prop="'targetList.' + index + '.targetType'"
                  :rules="{
                    required: true,
                    message: '定向选择不能为空',
                    trigger: 'change',
                  }"
                  label-width="100px"
                >
                  <el-radio-group
                    v-model="item.targetType"
                    @change="
                      getPurchaseIntention(
                        item.targetType == 1 ? 0 : 1,
                        item.targetCustom
                      );
                      getPopulation(
                        item.targetType == 1 ? 0 : 1,
                        item.targetCustom
                      );
                    "
                  >
                    <el-radio-button
                      v-for="(item, index) in formList.typeList"
                      :key="index"
                      :label="item.value"
                      :disabled="item.value == 1"
                      >{{ item.label }}</el-radio-button
                    >
                  </el-radio-group>
                </el-form-item>
                <div v-if="item.targetType == 1">
                  <el-form-item
                    label="定向模板名称"
                    :prop="'targetList.' + index + '.targetName'"
                    :rules="{
                      required: true,
                      message: '定向模板名称不能为空',
                      trigger: 'blur',
                    }"
                    label-width="100px"
                  >
                    <el-input
                      placeholder="请输入定向模板名称"
                      v-model="item.targetName"
                      style="width: 300px"
                    >
                    </el-input>
                  </el-form-item>
                  <el-form-item
                    label="地区"
                    :prop="'targetList.' + index + '.targetLaunch.regionType'"
                    :rules="{
                      required: true,
                      message: '地区不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <!-- item.targetLaunch.region = undefined; -->
                    <el-radio-group
                      v-model="item.targetLaunch.regionType"
                      @change="
                        getRegion($event, item.targetLaunch);
                        item.targetLaunch.region = undefined;
                      "
                    >
                      <el-radio-button
                        v-for="(item, index) in formList.regionTypeList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item
                    v-if="item.targetLaunch.regionType != 0"
                    label="选择地区"
                    :prop="'targetList.' + index + '.targetLaunch.region'"
                    :rules="{
                      required: true,
                      message: '地区不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-cascader
                      ref="cascader"
                      v-model="item.targetLaunch.region"
                      :options="item.targetLaunch.regionList"
                      :props="props"
                      collapse-tags
                      clearable
                      @change="regionCascader(index, item.targetLaunch)"
                    ></el-cascader>
                  </el-form-item>

                  <el-form-item
                    label="性别"
                    :prop="'targetList.' + index + '.targetLaunch.genderType'"
                    :rules="{
                      required: true,
                      message: '性别不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-radio-group v-model="item.targetLaunch.genderType">
                      <el-radio-button
                        v-for="(item, index) in formList.genderTypeList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>

                  <el-form-item
                    label="年龄"
                    :prop="'targetList.' + index + '.targetLaunch.age'"
                    :rules="{
                      required: true,
                      message: '年龄不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-checkbox-group
                      v-model="item.targetLaunch.age"
                      @change="ageCheck"
                    >
                      <el-checkbox-button
                        v-for="(item, index) in formList.ageList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-checkbox-button
                      >
                    </el-checkbox-group>
                  </el-form-item>

                  <el-form-item
                    label="排除人群"
                    :prop="
                      'targetList.' +
                      index +
                      '.targetLaunch.excludePopulationType'
                    "
                    :rules="{
                      required: true,
                      message: '排除人群不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-radio-group
                      v-model="item.targetLaunch.excludePopulationType"
                      @change="getPopulation($event, item.targetLaunch)"
                    >
                      <el-radio-button
                        v-for="(item, index) in formList.excludePopulationList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item
                    label=" "
                    v-if="item.targetLaunch.excludePopulationType != '0'"
                    :prop="
                      'targetList.' + index + '.targetLaunch.excludePopulation'
                    "
                    :rules="{
                      required: true,
                      message: '排除人群不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-select
                      v-model="item.targetLaunch.excludePopulation"
                      multiple
                      collapse-tags
                      filterable
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="item in item.targetLaunch.populationList"
                        :key="item.orientation_id"
                        :label="item.orientation_name"
                        :value="item.orientation_id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </div>
                <div v-if="item.targetType == 2">
                  <el-form-item
                    label="定向模板名称"
                    :prop="'targetList.' + index + '.targetName'"
                    :rules="{
                      required: true,
                      message: '定向模板名称不能为空',
                      trigger: 'blur',
                    }"
                    label-width="100px"
                  >
                    <el-input
                      placeholder="请输入定向模板名称"
                      v-model="item.targetName"
                      style="width: 300px"
                    >
                    </el-input>
                  </el-form-item>
                  <!-- <el-form-item
                    label="选择定向模板"
                    :prop="
                      'targetList.' + index + '.targetCustom.targetTemplateId'
                    "
                    :rules="{
                      required: false,
                      message: '定向模板不能为空',
                      trigger: 'blur',
                    }"
                    label-width="100px"
                  >
                    <el-input
                      placeholder="请输入定向模板名称"
                      v-model="item.targetName"
                      style="width: 300px"
                    >
                    </el-input>
                  </el-form-item> -->
                  <el-form-item
                    label="地区"
                    :prop="'targetList.' + index + '.targetCustom.regionType'"
                    :rules="{
                      required: true,
                      message: '地区不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <!-- item.targetLaunch.region = undefined; -->
                    <el-radio-group
                      v-model="item.targetCustom.regionType"
                      @change="
                        getRegion($event, item.targetCustom);
                        item.targetCustom.region = undefined;
                      "
                    >
                      <el-radio-button
                        v-for="(item, index) in formList.regionTypeList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item
                    v-if="item.targetCustom.regionType != 0"
                    label="选择地区"
                    :prop="'targetList.' + index + '.targetCustom.region'"
                    :rules="{
                      required: true,
                      message: '地区不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-cascader
                      ref="cascader"
                      v-model="item.targetCustom.region"
                      :options="item.targetCustom.regionList"
                      :props="props"
                      collapse-tags
                      clearable
                      @change="regionCascader(index, item.targetCustom)"
                    ></el-cascader>
                  </el-form-item>

                  <el-form-item
                    label="性别"
                    :prop="'targetList.' + index + '.targetCustom.genderType'"
                    :rules="{
                      required: true,
                      message: '性别不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-radio-group v-model="item.targetCustom.genderType">
                      <el-radio-button
                        v-for="(item, index) in formList.genderTypeList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>

                  <el-form-item
                    label="年龄"
                    :prop="'targetList.' + index + '.targetCustom.age'"
                    :rules="{
                      required: true,
                      message: '年龄不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-checkbox-group
                      v-model="item.targetCustom.age"
                      @change="ageCheck"
                    >
                      <el-checkbox-button
                        v-for="(item, index) in formList.ageList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-checkbox-button
                      >
                    </el-checkbox-group>
                  </el-form-item>

                  <el-form-item
                    label="购物意图"
                    :prop="
                      'targetList.' +
                      index +
                      '.targetCustom.purchaseIntentionType'
                    "
                    :rules="{
                      required: true,
                      message: '购物意图不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-radio-group
                      v-model="item.targetCustom.purchaseIntentionType"
                      @change="getPurchaseIntention($event, item.targetCustom)"
                    >
                      <el-radio-button
                        v-for="(
                          item, index
                        ) in formList.purchaseIntentionTypeList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item
                    v-if="item.targetCustom.purchaseIntentionType == 1"
                    label=" "
                    :prop="
                      'targetList.' + index + '.targetCustom.purchaseIntention'
                    "
                    :rules="{
                      required: true,
                      message: '购物意图不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-cascader
                      ref="purchaseCascader"
                      v-model="item.targetCustom.purchaseIntention"
                      :options="item.targetCustom.purchaseIntentionList"
                      :props="props"
                      collapse-tags
                      clearable
                      labelInValue
                      @change="purchaseCascader(index, item.targetCustom, item)"
                    ></el-cascader>
                  </el-form-item>

                  <el-form-item
                    v-if="item.targetCustom.purchaseIntentionType == 1"
                    label="意向类型"
                    :prop="
                      'targetList.' + index + '.targetCustom.interestLevelList'
                    "
                    :rules="{
                      required: true,
                      message: '意向类型不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-checkbox-group
                      v-model="item.targetCustom.interestLevelList"
                    >
                      <el-checkbox :label="1">涨粉</el-checkbox>
                      <el-checkbox :label="2">购买</el-checkbox>
                    </el-checkbox-group>
                  </el-form-item>
                  <el-form-item
                    v-if="item.targetCustom.purchaseIntentionType == 1"
                    label="点击类型"
                    :prop="
                      'targetList.' + index + '.targetCustom.purchaseLevelList'
                    "
                    :rules="{
                      required: true,
                      message: '点击类型不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-checkbox-group
                      v-model="item.targetCustom.purchaseLevelList"
                    >
                      <el-checkbox :label="1">视频</el-checkbox>
                      <el-checkbox :label="2">直播</el-checkbox>
                    </el-checkbox-group>
                  </el-form-item>

                  <el-form-item
                    label="定向人群"
                    :prop="
                      'targetList.' + index + '.targetCustom.populationType'
                    "
                    :rules="{
                      required: true,
                      message: '定向人群不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-radio-group v-model="item.targetCustom.populationType">
                      <el-radio-button
                        v-for="(item, index) in formList.populationList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>

                  <el-form-item
                    label=" "
                    v-if="item.targetCustom.populationType != '0'"
                    :prop="'targetList.' + index + '.targetCustom.population'"
                    :rules="{
                      required: true,
                      message: '定向人群不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-select
                      v-model="item.targetCustom.population"
                      multiple
                      collapse-tags
                      filterable
                      placeholder="请选择"
                      @change="disabledExclude($event, item.targetCustom)"
                    >
                      <el-option
                        v-for="item in item.targetCustom.populationList"
                        :key="item.orientation_id"
                        :label="item.orientation_name"
                        :value="item.orientation_id"
                        :disabled="item.disabled"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item
                    label="排除人群"
                    :prop="
                      'targetList.' +
                      index +
                      '.targetCustom.excludePopulationType'
                    "
                    :rules="{
                      required: true,
                      message: '排除人群不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-radio-group
                      v-model="item.targetCustom.excludePopulationType"
                    >
                      <el-radio-button
                        v-for="(item, index) in formList.excludePopulationList"
                        :key="index"
                        :label="item.value"
                        >{{ item.label }}</el-radio-button
                      >
                    </el-radio-group>
                  </el-form-item>

                  <el-form-item
                    label=" "
                    v-if="item.targetCustom.excludePopulationType != '0'"
                    :prop="
                      'targetList.' + index + '.targetCustom.excludePopulation'
                    "
                    :rules="{
                      required: true,
                      message: '排除人群不能为空',
                      trigger: 'change',
                    }"
                    label-width="100px"
                  >
                    <el-select
                      v-model="item.targetCustom.excludePopulation"
                      multiple
                      collapse-tags
                      filterable
                      placeholder="请选择"
                      @change="disabledPopulation($event, item.targetCustom)"
                    >
                      <el-option
                        v-for="item in item.targetCustom.excludePopulationList"
                        :key="item.orientation_id"
                        :label="item.orientation_name"
                        :value="item.orientation_id"
                        :disabled="item.disabled"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-collapse-item>
            </el-collapse>
          </el-form-item>
        </div>
        <el-form-item label=" ">
          <el-button size="mini" type="text" @click="addTarget"
            >添加定向</el-button
          >
        </el-form-item>
      </el-form>
      <div
        style="
          width: 40%;
          height: 50px;
          background: white;
          position: fixed;
          bottom: 0;
          border-top: 1px solid #f2f2f2;
          display: flex;
          justify-content: end;
          align-items: center;
          padding: 0 20px;
        "
      >
        <el-button type="primary" size="mini" @click="submitForm"
          >确定</el-button
        >
        <el-button size="mini" @click="handleClose">取消</el-button>
      </div>
    </el-drawer>

    <el-dialog
      title="创建记录"
      :visible.sync="logVisible"
      :before-close="handleCloseLog"
      width="1000px"
      append-to-body
    >
      <el-table v-loading="logloading" :data="logList" ref="logTable">
        <el-table-column label="计划ID" align="center" prop="campaignId">
        </el-table-column>
        <el-table-column label="组ID" align="center" prop="unitId">
          <template slot-scope="scope">
            {{ scope.row.unitId ? scope.row.unitId : "-" }}
          </template>
        </el-table-column>
        <el-table-column label="出价类型" align="center" prop="ocpxActionType">
          <template slot-scope="scope">
            {{ scope.row.ocpxActionType | ocpxName }}
          </template>
        </el-table-column>
        <el-table-column label="出价" align="center" prop="cpaBid">
          <template slot-scope="scope">
            {{ scope.row.cpaBid ? scope.row.cpaBid / 1000 : "-" }}
          </template>
        </el-table-column>

        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{
              scope.row.createTime ? scope.row.createTime.split(" ")[0] : "-"
            }}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="message" />
      </el-table>

      <pagination
        v-show="logPage.total > 0"
        :total="logPage.total"
        :page.sync="logPage.pageNum"
        :limit.sync="logPage.pageSize"
        @pagination="getLogList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { refreshCache } from "@/api/system/dict/type";
import vueUpload from "../trader/components/vueUpload";
import FileUpload from "../trader/components/upload";
import {
  getLaunchList,
  editLaunch,
  getRegion,
  getPopulation,
  getPurchaseIntention,
  adds,
  getById,
  getTemplateByIds,
  queryGroupRecord,
  createByStrategy,
} from "@/api/live/live";

export default {
  name: "account-list",
  components: {
    vueUpload,
    FileUpload,
  },
  data() {
    var bidValid = (rule, value, callback) => {
      let z = /-/g;
      let a = /\//g;
      let d = /\./g;
      value = value + "";
      if (value === "" || value === undefined || value === 0 || value === "0") {
        callback("出价必须填写");
      } else if (value.startsWith(".") || value.endsWith(".")) {
        callback("请填写正确出价");
      } else if (value.startsWith("-") || value.endsWith("-")) {
        callback("请填写正确出价");
      } else if (value.match(z)) {
        callback("请填写正确出价");
      } else if (value.match(/[^0-9+-\/]/g)) {
        callback("请填写正确出价");
      } else {
        callback();
      }
    };
    return {
      props: {
        multiple: true,
        value: "id",
        label: "name",
        emitPath: false,
      },
      dialog: false,
      strategyId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 字典表格数据
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 是否为编辑
      edit: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        campaignName: undefined,
        // accountName: undefined,
        accountName: undefined,
      },
      //页面所有的下拉数据
      queryParamsList: {},
      // 表单参数
      form: {
        ocpxActionType: [72, 395, 192, 740],
        targetList: [],
      },
      accountId: null,
      //上传报表下拉数据
      formList: {
        ocpxActionTypeList: [
          { value: 72, label: "粉丝关注" },
          { value: 395, label: "商品购买" },
          { value: 192, label: "直播推广ROI" },

          { value: 740, label: "进入直播间" },
        ],
        typeList: [
          { value: 1, label: "智能优选" },
          { value: 2, label: "自定义" },
        ],
        regionTypeList: [
          { value: 0, label: "不限" },
          { value: 2, label: "按地区" },
          { value: 3, label: "按城市等级" },
        ],
        regionList: [],
        genderTypeList: [
          { value: 0, label: "不限" },
          { value: 1, label: "女" },
          { value: 2, label: "男" },
        ],
        ageList: [
          { value: "0", label: "不限" },
          { value: "18-23", label: "18-23" },
          { value: "24-30", label: "24-30" },
          { value: "31-40", label: "31-40" },
          { value: "41-49", label: "41-49" },
          { value: "50-100", label: "50+" },
        ],
        excludePopulationList: [
          { value: 0, label: "不限" },
          { value: 1, label: "排除人群" },
        ],
        populationList: [
          { value: 0, label: "不限" },
          { value: 1, label: "自选人群" },
        ],
        purchaseIntentionTypeList: [
          { value: 0, label: "不限" },
          { value: 1, label: "自定义" },
        ],
        purchaseIntentionList: [],
      },
      rule: {
        ocpxActionType: [
          {
            required: true,
            message: "转化目标必选",
            trigger: "change",
          },
        ],
        typeOne: [
          {
            required: true,
            message: "涨粉成本上限必填",
            trigger: "blur",
          },
          {
            validator: bidValid,
          },
        ],
        typeTwo: [
          {
            required: true,
            message: "订单支付成本上限必填",
            trigger: "blur",
          },
          {
            validator: bidValid,
          },
        ],
        typeThree: [
          {
            required: true,
            message: "推广ROI成本上限必填",
            trigger: "blur",
          },
          {
            validator: bidValid,
          },
        ],
        typeFour: [
          {
            required: true,
            message: "直播间成本上限必填",
            trigger: "blur",
          },
          {
            validator: bidValid,
          },
        ],
      },
      fileList: [],
      //获取销售线索文件流
      dateFile: null,
      //销售线索上传loading
      confirmLoading: false,
      // 分配销售展示列表
      ksInfo: null,
      userId: null,
      logVisible: false,
      logList: [],
      logloading: false,
      populationList: [],
      logPage: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    this.userId = this.$store.getters.userId;
    this.getList();
  },
  filters: {
    ocpxName(status) {
      const statusMap = {
        72: "粉丝关注",
        395: "商品购买",
        192: "直播推广ROI",
        740: "进入直播间",
      };
      return statusMap[status];
    },
  },
  methods: {
    handleCloseLog() {
      this.logPage.pageNum = 1;
      this.logVisible = false;
    },
    getLog(item) {
      this.accountId = item.row.accountId;
      this.strategyId = item.row.id;
      this.logVisible = true;
      this.getLogList();
    },
    getLogList() {
      queryGroupRecord({
        accountId: this.accountId,
        strategyId: this.strategyId,
        pageNum: this.logPage.pageNum,
        pageSize: this.logPage.pageSize,
      }).then((res) => {
        this.logList = res.rows;
        this.logPage.total = res.total;
      });
    },
    getDetail(item) {
      this.dialog = true;
      this.accountId = item.row.accountId;
      this.strategyId = item.row.id;
      var params = {};
      params.accountId = this.accountId;
      getPopulation(params).then((res) => {
        if (res.data) {
          this.populationList = res.data;
        }
      });
      getById({ id: item.row.id })
        .then((res) => {
          var data = res.data;
          if (res.data.targetTemplate) {
            getTemplateByIds({ ids: res.data.targetTemplate }).then(
              (resAll) => {
                //           ocpxActionType: [72, 395, 192, 740],
                // targetList: [],
                this.$set(
                  this.form,
                  "ocpxActionType",
                  JSON.parse(data.ocpxActionArr).map((item) => {
                    return +item.type;
                  })
                );
                for (
                  let i = 0;
                  i < JSON.parse(data.ocpxActionArr).length;
                  i++
                ) {
                  if (JSON.parse(data.ocpxActionArr)[i].type == 72) {
                    this.$set(
                      this.form,
                      "typeOne",
                      typeof JSON.parse(data.ocpxActionArr)[i].cpaBid ==
                        "string"
                        ? JSON.parse(data.ocpxActionArr)
                            [i].cpaBid.split("/")
                            .map((c) => {
                              return c / 1000;
                            })
                            .join("/")
                        : JSON.parse(data.ocpxActionArr)[i].cpaBid / 1000
                    );
                  } else if (JSON.parse(data.ocpxActionArr)[i].type == 395) {
                    this.$set(
                      this.form,
                      "typeTwo",
                      typeof JSON.parse(data.ocpxActionArr)[i].cpaBid ==
                        "string"
                        ? JSON.parse(data.ocpxActionArr)
                            [i].cpaBid.split("/")
                            .map((c) => {
                              return c / 1000;
                            })
                            .join("/")
                        : JSON.parse(data.ocpxActionArr)[i].cpaBid / 1000
                    );
                  } else if (JSON.parse(data.ocpxActionArr)[i].type == 192) {
                    this.$set(
                      this.form,
                      "typeThree",
                      typeof JSON.parse(data.ocpxActionArr)[i].cpaBid ==
                        "string"
                        ? JSON.parse(data.ocpxActionArr)
                            [i].cpaBid.split("/")
                            .map((c) => {
                              return c / 1000;
                            })
                            .join("/")
                        : JSON.parse(data.ocpxActionArr)[i].cpaBid / 1000
                    );
                  } else if (JSON.parse(data.ocpxActionArr)[i].type == 740) {
                    this.$set(
                      this.form,
                      "typeFour",
                      typeof JSON.parse(data.ocpxActionArr)[i].cpaBid ==
                        "string"
                        ? JSON.parse(data.ocpxActionArr)
                            [i].cpaBid.split("/")
                            .map((c) => {
                              return c / 1000;
                            })
                            .join("/")
                        : JSON.parse(data.ocpxActionArr)[i].cpaBid / 1000
                    );
                  }
                }

                var dataAll = resAll.data;
                for (let i = 0; i < dataAll.length; i++) {
                  this.form.targetList.push({
                    name: "定向" + (this.form.targetList.length + 1),
                    active: "1",
                    targetType: +dataAll[i].targetType,
                    targetName: dataAll[i].targetName,
                    id: dataAll[i].id,
                    ...dataAll[i],
                    targetLaunch: {
                      region: [],
                      regionNone: [],
                      regionType: 0,
                      genderType: 0,
                      age: ["0"],
                      regionList: [],
                      populationList: [],
                      excludePopulation: [],
                      excludePopulationName: [],
                      excludePopulationType: 0,
                    },
                    targetCustom: {
                      targetTemplateId: undefined,
                      region: [],
                      regionNone: [],
                      regionType: 0,
                      regionList: [],
                      genderType: 0,
                      age: ["0"],
                      purchaseIntentionList: [],
                      purchaseIntention: [],
                      purchaseIntentionNone: [],
                      interestLevelList: [],
                      purchaseLevelList: [],
                      purchaseIntentionType: 0,
                      populationList: [],
                      excludePopulationList: [],
                      excludePopulation: [],
                      excludePopulationName: [],
                      excludePopulationType: 0,
                      populationType: 0,
                      population: [],
                      populationName: [],
                    },
                  });
                }
                for (let c = 0; c < this.form.targetList.length; c++) {
                  if (this.form.targetList[c].targetType == 1) {
                    if (this.form.targetList[c].regionType == "1") {
                      this.form.targetList[c].targetLaunch.regionType = 2;
                      this.form.targetList[c].targetLaunch.regionNone =
                        JSON.parse(this.form.targetList[c].region);
                      this.form.targetList[c].targetLaunch.region = JSON.parse(
                        this.form.targetList[c].region
                      );
                    } else if (this.form.targetList[c].regionType == "3") {
                      this.form.targetList[c].targetLaunch.regionType = 3;
                      this.form.targetList[c].targetLaunch.regionNone =
                        JSON.parse(this.form.targetList[c].region);
                      this.form.targetList[c].targetLaunch.region = JSON.parse(
                        this.form.targetList[c].region
                      );
                    } else {
                      this.form.targetList[c].targetLaunch.regionType = 0;
                    }

                    this.getRegion(
                      this.form.targetList[c].targetLaunch.regionType,
                      this.form.targetList[c].targetLaunch,
                      "edit",
                      c
                    );

                    this.form.targetList[c].targetLaunch.genderType =
                      +this.form.targetList[c].genderType;

                    this.form.targetList[c].targetLaunch.age =
                      this.form.targetList[c].age == "[]"
                        ? ["0"]
                        : JSON.parse(this.form.targetList[c].age).map(
                            (item) => {
                              return item.min + "-" + item.max;
                            }
                          );
                    this.form.targetList[c].targetLaunch.excludePopulationType =
                      this.form.targetList[c].excludePopulation == "[]" ? 0 : 1;
                    this.form.targetList[c].targetLaunch.excludePopulation =
                      this.form.targetList[c].targetLaunch
                        .excludePopulationType == 1
                        ? JSON.parse(this.form.targetList[c].excludePopulation)
                        : [];

                    setTimeout(() => {
                      this.getPopulation(
                        1,
                        this.form.targetList[c].targetLaunch
                      );
                    }, 100);
                  } else {
                    if (this.form.targetList[c].regionType == "1") {
                      this.form.targetList[c].targetCustom.regionType = 2;
                      this.form.targetList[c].targetCustom.regionNone =
                        JSON.parse(this.form.targetList[c].region);
                      this.form.targetList[c].targetCustom.region = JSON.parse(
                        this.form.targetList[c].region
                      );
                    } else if (this.form.targetList[c].regionType == "3") {
                      this.form.targetList[c].targetCustom.regionType = 3;
                      this.form.targetList[c].targetCustom.regionNone =
                        JSON.parse(this.form.targetList[c].region);
                      this.form.targetList[c].targetCustom.region = JSON.parse(
                        this.form.targetList[c].region
                      );
                    } else {
                      this.form.targetList[c].targetCustom.regionType = 0;
                    }

                    this.form.targetList[c].targetCustom.genderType =
                      +this.form.targetList[c].genderType;

                    this.form.targetList[c].targetCustom.age =
                      this.form.targetList[c].age == "[]"
                        ? ["0"]
                        : JSON.parse(this.form.targetList[c].age).map(
                            (item) => {
                              return item.min + "-" + item.max;
                            }
                          );
                    this.form.targetList[c].targetCustom.excludePopulationType =
                      this.form.targetList[c].excludePopulation == "[]" ? 0 : 1;
                    this.form.targetList[c].targetCustom.excludePopulation =
                      this.form.targetList[c].targetCustom
                        .excludePopulationType == 1
                        ? JSON.parse(this.form.targetList[c].excludePopulation)
                        : [];

                    this.form.targetList[c].targetCustom.populationType =
                      this.form.targetList[c].population == "[]" ? 0 : 1;
                    this.form.targetList[c].targetCustom.population =
                      this.form.targetList[c].targetCustom.populationType == 1
                        ? JSON.parse(this.form.targetList[c].population)
                        : [];

                    this.form.targetList[c].targetCustom.purchaseIntentionType =
                      this.form.targetList[c].purchaseIntention == "{}" ? 0 : 1;

                    this.form.targetList[c].targetCustom.purchaseIntention =
                      JSON.parse(this.form.targetList[c].purchaseIntention)
                        .categoriesIds
                        ? JSON.parse(this.form.targetList[c].purchaseIntention)
                            .categoriesIds
                        : [];
                    this.form.targetList[c].targetCustom.purchaseIntentionNone =
                      JSON.parse(this.form.targetList[c].purchaseIntention)
                        .categoriesIds
                        ? JSON.parse(
                            this.form.targetList[c].purchaseIntention
                          ).categoriesIds.map((item, index) => {
                            return {
                              data: {
                                id: item,
                                name: JSON.parse(
                                  this.form.targetList[c].purchaseIntention
                                ).categoriesNames[index],
                              },
                            };
                          })
                        : [];

                    this.form.targetList[c].targetCustom.interestLevelList =
                      JSON.parse(this.form.targetList[c].purchaseIntention)
                        .interestLevelList
                        ? JSON.parse(this.form.targetList[c].purchaseIntention)
                            .interestLevelList
                        : [];
                    this.form.targetList[c].targetCustom.purchaseLevelList =
                      JSON.parse(this.form.targetList[c].purchaseIntention)
                        .purchaseLevelList
                        ? JSON.parse(this.form.targetList[c].purchaseIntention)
                            .purchaseLevelList
                        : [];

                    setTimeout(() => {
                      this.getRegion(
                        this.form.targetList[c].targetCustom.regionType,
                        this.form.targetList[c].targetCustom,
                        "edit",
                        c
                      );
                    }, 700);
                    setTimeout(() => {
                      this.getPurchaseIntention(
                        this.form.targetList[c].targetCustom
                          .purchaseIntentionType,
                        this.form.targetList[c].targetCustom,
                        "edit",
                        c
                      );
                    }, 700);
                    this.getPopulation(1, this.form.targetList[c].targetCustom);
                  }
                }

                console.log(this.form.targetList);
              }
            );
          } else {
            this.$set(this.form, "targetList", []);
          }
        })
        .catch(() => {});
    },
    addTarget() {
      this.form.targetList.push({
        name: "定向" + (this.form.targetList.length + 1),
        active: "1",
        targetType: 2,
        targetName: undefined,
        targetLaunch: {
          region: [],
          regionNone: [],
          regionType: 0,
          genderType: 0,
          age: ["0"],
          regionList: [],
          populationList: [],
          excludePopulation: [],
          excludePopulationName: "",
          excludePopulationType: 0,
        },
        targetCustom: {
          targetTemplateId: undefined,
          region: [],
          regionNone: [],
          regionType: 0,
          regionList: [],
          genderType: 0,
          age: ["0"],
          purchaseIntentionList: [],
          purchaseIntention: [],
          purchaseIntentionNone: [],
          interestLevelList: [],
          purchaseLevelList: [],
          purchaseIntentionType: 0,
          populationList: [],
          excludePopulationList: [],
          excludePopulation: [],
          excludePopulationName: "",
          excludePopulationType: 0,
          populationType: 0,
          population: [],
          populationName: "",
        },
      });
      this.getPopulation(
        1,
        this.form.targetList[this.form.targetList.length - 1].targetCustom
      );
    },
    disabledExclude(e, list) {
      var data = JSON.parse(JSON.stringify(list.excludePopulationList));
      list.excludePopulationList = [];
      this.$nextTick(() => {
        e.forEach((item) => {
          data.forEach((c) => {
            if (item === c.orientation_id) {
              c.disabled = true;
            }
          });
        });
        list.excludePopulationList.push(...data);
      });
    },
    disabledPopulation(e, list) {
      var data = JSON.parse(JSON.stringify(list.populationList));
      list.populationList = [];
      this.$nextTick(() => {
        e.forEach((item) => {
          data.forEach((c) => {
            if (item === c.orientation_id) {
              c.disabled = true;
            }
          });
        });
        list.populationList.push(...data);
      });
    },
    getPopulation(e, list) {
      if (e != 0) {
        var params = {};
        params.accountId = this.accountId;
        getPopulation(params).then((res) => {
          if (res.data) {
            list.populationList = res.data.map((item) => {
              return {
                ...item,
                disabled: false,
              };
            });
            this.populationList = res.data;
            if (list.excludePopulationList) {
              list.excludePopulationList = res.data.map((item) => {
                return {
                  ...item,
                  disabled: false,
                };
              });
            }
          }

          // list.region.push(
          //   ...this.getChlidren([11, 4403, 441900, 440402], list.regionList)
          // );
        });
      }
    },
    getPurchaseIntention(e, list, type, index) {
      // if (e != 0) {
      list.purchaseIntentionList = [];
      var params = {};
      params.accountId = this.accountId;
      getPurchaseIntention(params).then((res) => {
        if (res.data) {
          // this.$nextTick(() => {
          list.purchaseIntentionList.push(...res.data);
          if (type == "edit" && list.purchaseIntentionType == 1) {
            list.purchaseIntention.push(
              ...this.getChlidren(
                list.purchaseIntention,
                list.purchaseIntentionList
              )
            );
            this.purchaseCascader(index, list);
          }
          // });
        }

        // list.region.push(
        //   ...this.getChlidren([11, 4403, 441900, 440402], list.regionList)
        // );
      });
      // }
    },
    getRegion(e, list, type, index) {
      if (e != 0) {
        var params = {};
        params.accountId = this.accountId;
        params.categoryType = e;
        getRegion(params).then((res) => {
          list.regionList = this.getTreeData(res.data);
          if (type == "edit") {
            list.region.push(...this.getChlidren(list.region, list.regionList));
            this.regionCascader(index, list);
          }
        });
      }
    },
    getChlidren(data, dataList) {
      var valueArr = [];
      for (let j = 0; j < data.length; j++) {
        for (let i = 0; i < dataList.length; i++) {
          if (data[j] == dataList[i].id) {
            if (!!dataList[i].children) {
              var arrData = this.transformToArrayFormat(dataList[i].children);
              valueArr.push(
                ...arrData.map((item) => {
                  return item.id;
                })
              );
            } else {
              valueArr.push(data[j]);
            }
          } else {
            if (!!dataList[i].children) {
              for (let o = 0; o < dataList[i].children.length; o++) {
                if (data[j] == dataList[i].children[o].id) {
                  if (!!dataList[i].children[o].children) {
                    var arrData2 = this.transformToArrayFormat(
                      dataList[i].children[o].children
                    );
                    valueArr.push(
                      ...arrData2.map((item) => {
                        return item.id;
                      })
                    );
                  } else {
                  }
                } else {
                }
              }
            }
          }
        }
      }
      return valueArr;
    },
    isArray(arr) {
      return Object.prototype.toString.call(arr) == "[object Array]";
    },
    transformToArrayFormat(nodes, setting = {}) {
      if (!nodes) return [];
      var childKey = setting.childKey || "children",
        r = [];
      if (this.isArray(nodes)) {
        for (var i = 0, l = nodes.length; i < l; i++) {
          r.push(nodes[i]);
          if (nodes[i][childKey])
            r = r.concat(
              this.transformToArrayFormat(nodes[i][childKey], setting)
            );
        }
      } else {
        r.push(nodes);
        if (nodes[childKey])
          r = r.concat(this.transformToArrayFormat(nodes[childKey], setting));
      }
      return r;
    },
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children) {
          if (data[i].children.length < 1) {
            // children若为空数组，则将children设为undefined
            data[i].children = null;
          } else {
            // // children若不为空数组，则继续 递归调用 本方法
            this.getTreeData(data[i].children);
          }
        } else {
          data[i].children = null;
        }
      }
      return data;
    },
    regionCascader(index, item) {
      var findIndex = 0;
      for (let i = 0; i < index; i++) {
        if (this.form.targetList[i].regionType != 0) {
          findIndex++;
        }
      }
      let CheckedNodes = this.$refs["cascader"][findIndex].getCheckedNodes();
      CheckedNodes = CheckedNodes.filter(
        (option) => !(option.parent && option.parent.checked)
      );
      item.regionNone = CheckedNodes.map((item) => {
        return item.data.id;
      });
      // CheckedNodes = CheckedNodes.filter(
      //   (option) => !(option.parent && option.parent.checked)
      // );
    },
    regionCascaderValue(index, item) {
      var findIndex = 0;
      for (let i = 0; i < index; i++) {
        if (this.form.targetList[i].regionType != 0) {
          findIndex++;
        }
      }
      let CheckedNodes = this.$refs["cascader"][findIndex].getCheckedNodes();
      CheckedNodes = CheckedNodes.filter(
        (option) => !(option.parent && option.parent.checked)
      );
      var data = CheckedNodes.map((item) => {
        return item.data.id;
      });
      return data;
    },
    purchaseCascaderValue(index, item) {
      var findIndex = 0;
      for (let i = 0; i < index; i++) {
        if (this.form.targetList[i].purchaseIntentionType == 1) {
          findIndex++;
        }
      }
      let CheckedNodes =
        this.$refs["purchaseCascader"][findIndex].getCheckedNodes();
      CheckedNodes = CheckedNodes.filter(
        (option) => !(option.parent && option.parent.checked)
      );
      return CheckedNodes;
    },
    purchaseCascader(index, item) {
      var findIndex = 0;
      for (let i = 0; i < index; i++) {
        if (this.form.targetList[i].purchaseIntentionType == 1) {
          findIndex++;
        }
      }
      if (this.$refs["purchaseCascader"][findIndex]) {
        let CheckedNodes =
          this.$refs["purchaseCascader"][findIndex].getCheckedNodes();
        CheckedNodes = CheckedNodes.filter(
          (option) => !(option.parent && option.parent.checked)
        );
        item.purchaseIntentionNone = CheckedNodes;
      }
    },
    ageCheck(e) {
      console.log(e);
      if (e[e.length - 1] == "0") {
        this.$nextTick(() => {
          e.splice(0, e.length - 1);
        });
      } else {
        this.$nextTick(() => {
          var index = e.findIndex((item) => {
            return item == "0";
          });
          if (index > -1) {
            e.splice(index, 1);
          }
        });
      }
    },
    checkValue(e) {
      if (!e.includes(72)) {
        this.$set(this.form, "typeOne", undefined);
      } else if (!e.includes(395)) {
        this.$set(this.form, "typeTwo", undefined);
      } else if (!e.includes(192)) {
        this.$set(this.form, "typeThree", undefined);
      } else if (!e.includes(740)) {
        this.$set(this.form, "typeFour", undefined);
      }
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getLaunchList({ ...this.queryParams, userId: this.userId }).then(
        (response) => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.uploadDate = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.replace({ path: "/live/create" });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.sellerId);
    },
    /** 执行 */
    handleFunction(item) {
      createByStrategy({ id: item.id }).then((res) => {
        this.$message.success("执行成功");
      });
    },
    /** 编辑 */
    handleAllocationOne(item) {
      this.$router.replace({ path: "/live/create", query: { id: item.id } });
    },
    handleClose() {
      this.dialog = false;

      this.resetForm("form");
      this.$set(this.form, "targetList", []);
    },
    /** 表单新增 */
    submitForm() {
      if (this.form.targetList.length == 0) {
        this.$message.error("请添加定向");
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.confirmLoading = true;
          var params = {};
          params.id = this.strategyId;
          params.ocpxActionArr = this.form.ocpxActionType.map((item) => {
            return {
              type: item,
              cpaBid:
                item == 72
                  ? ("" + this.form.typeOne).split("/").length > 1
                    ? ("" + this.form.typeOne)
                        .split("/")
                        .map((c) => {
                          return c * 1000;
                        })
                        .join("/")
                    : this.form.typeOne * 1000
                  : item == 395
                  ? ("" + this.form.typeTwo).split("/").length > 1
                    ? ("" + this.form.typeTwo)
                        .split("/")
                        .map((c) => {
                          return c * 1000;
                        })
                        .join("/")
                    : this.form.typeTwo * 1000
                  : item == 192
                  ? ("" + this.form.typeThree).split("/").length > 1
                    ? ("" + this.form.typeThree)
                        .split("/")
                        .map((c) => {
                          return c * 1000;
                        })
                        .join("/")
                    : this.form.typeThree * 1000
                  : item == 740
                  ? ("" + this.form.typeFour).split("/").length > 1
                    ? ("" + this.form.typeFour)
                        .split("/")
                        .map((c) => {
                          return c * 1000;
                        })
                        .join("/")
                    : this.form.typeFour * 1000
                  : null,
            };
          });
          // purchaseCascader
          // regionCascader

          params.array = this.form.targetList.map((item, index) => {
            if (item.targetType == 1) {
              return {
                accountId: this.accountId,
                targetName: item.targetName,
                id: item.id,
                targetType: item.targetType,
                regionType:
                  item.targetLaunch.regionType == 2
                    ? "1"
                    : item.targetLaunch.regionType == 3
                    ? "3"
                    : 0,
                region:
                  item.targetLaunch.regionType == 2
                    ? item.targetLaunch.regionNone.length == 0
                      ? this.regionCascaderValue(index, item.targetLaunch)
                      : item.targetLaunch.regionNone
                    : item.targetLaunch.regionType == 3
                    ? item.targetLaunch.region
                    : "[]",
                genderType: item.targetLaunch.genderType,
                age:
                  item.targetLaunch.age[0] == "0"
                    ? "[]"
                    : item.targetLaunch.age.map((c) => {
                        return {
                          min: c.split("-")[0],
                          max: c.split("-")[1],
                        };
                      }),
                excludePopulation:
                  item.targetLaunch.excludePopulationType == 0
                    ? "[]"
                    : item.targetLaunch.excludePopulation,
                excludePopulationName:
                  item.targetLaunch.excludePopulationType == 0
                    ? "不限"
                    : this.populationList.filter((c) => {
                        return (
                          c.orientation_id ==
                          item.targetLaunch.excludePopulation[0]
                        );
                      })[0].orientation_name,
              };
            } else if (item.targetType == 2) {
              return {
                accountId: this.accountId,
                targetName: item.targetName,
                id: item.id,
                targetType: item.targetType,
                regionType:
                  item.targetCustom.regionType == 2
                    ? "1"
                    : item.targetCustom.regionType == 3
                    ? "3"
                    : 0,
                region:
                  item.targetCustom.regionType == 2
                    ? item.targetCustom.regionNone.length == 0
                      ? this.regionCascaderValue(index, item.targetCustom)
                      : item.targetCustom.regionNone
                    : item.targetCustom.regionType == 3
                    ? item.targetCustom.region
                    : "[]",
                genderType: item.targetCustom.genderType,
                age:
                  item.targetCustom.age[0] == "0"
                    ? "[]"
                    : item.targetCustom.age.map((c) => {
                        return {
                          min: c.split("-")[0],
                          max: c.split("-")[1],
                        };
                      }),
                purchaseIntention:
                  item.targetCustom.purchaseIntentionType == 0
                    ? "{}"
                    : {
                        categoriesIds:
                          item.targetCustom.purchaseIntentionNone.length == 0
                            ? this.purchaseCascaderValue(
                                index,
                                item.targetCustom
                              ).map((c) => {
                                return c.data.id;
                              })
                            : item.targetCustom.purchaseIntentionNone.map(
                                (c) => {
                                  return c.data.id;
                                }
                              ),
                        categoriesNames:
                          item.targetCustom.purchaseIntentionNone.length == 0
                            ? this.purchaseCascaderValue(
                                index,
                                item.targetCustom
                              ).map((c) => {
                                return c.data.name;
                              })
                            : item.targetCustom.purchaseIntentionNone.map(
                                (c) => {
                                  return c.data.name;
                                }
                              ),
                        interestLevelList: item.targetCustom.interestLevelList,
                        purchaseLevelList: item.targetCustom.purchaseLevelList,
                      },
                population:
                  item.targetCustom.populationType == 0
                    ? "[]"
                    : item.targetCustom.population,
                populationName:
                  item.targetCustom.populationType == 0
                    ? "不限"
                    : this.populationList.filter((c) => {
                        return (
                          c.orientation_id == item.targetCustom.population[0]
                        );
                      })[0].orientation_name,
                excludePopulation:
                  item.targetCustom.excludePopulationType == 0
                    ? "[]"
                    : item.targetCustom.excludePopulation,
                excludePopulationName:
                  item.targetCustom.excludePopulationType == 0
                    ? "不限"
                    : this.populationList.filter((c) => {
                        return (
                          c.orientation_id ==
                          item.targetCustom.excludePopulation[0]
                        );
                      })[0].orientation_name,
              };
            }
          });

          // console.log(params);
          adds(params)
            .then((res) => {
              this.confirmLoading = false;
              this.handleClose();
              this.handleQuery();
            })
            .catch(() => {
              this.confirmLoading = false;
            });
        }
      });
    },
    /** 删除 */
    handlePersonVisible(item) {
      this.$modal
        .confirm("是否删除此条投流策略？")
        .then(() => {
          var params = {};
          params.id = item.id;
          params.state = 0;
          editLaunch(params)
            .then((res) => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            })
            .catch(() => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {});
    },
    // 失去焦点金额格式化
    inputMoney(el) {
      let temp = Number(el.target.value) || null;
      this.formBroadcast.goalGmv = this.priceFormat(temp);
    },
    // 获得焦点金额去掉格式
    uninputMoney(el) {
      if (!!el.target.value) {
        this.formBroadcast.goalGmv = this.delcommafy(el.target.value);
      }
    },
    priceFormat(num, n) {
      n = n || 2;
      let symbol = ",";
      if (num === null) return num;
      if (typeof num !== "number")
        throw new TypeError("num参数应该是一个number类型");
      if (n < 0) throw new Error("参数n不应该小于0");
      let hasDot = parseInt(num) != num; //这里检测num是否为小数，true表示小数
      let m = n != undefined && n != null ? n : 1;
      num =
        m == 0
          ? num.toFixed(m) + "."
          : hasDot
          ? n
            ? num.toFixed(n)
            : num
          : num.toFixed(m);
      symbol = symbol || ",";
      num = num
        .toString()
        .replace(/(\d)(?=(\d{3})+\.)/g, function (match, p1, p2) {
          return p1 + symbol;
        });
      if (n == 0 || (!hasDot && !n)) {
        //如果n为0或者传入的num是整数并且没有指定整数的保留位数，则去掉前面操作中的小数位
        num = num.substring(0, num.indexOf("."));
      }
      return num;
    },
    //去除千分位中的‘，’
    delcommafy(num) {
      if (!num) return num;
      num = num.toString();
      num = num.replace(/,/gi, "");
      if (num.indexOf(".00") > 0) num = parseInt(num);
      return num;
    },

    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.entire-line {
  width: 100%;
  ::v-deep .el-form-item__content {
    width: 60%;
  }
}
::v-deep .el-form-item__label {
  white-space: nowrap;
}
::v-deep .el-collapse-item__header {
  border: 1px solid #e6ebf5;
  background: rgb(250, 250, 250);
  padding-left: 15px;
}
::v-deep .el-collapse-item__content {
  padding: 15px;
}
::v-deep .el-cascader-menu {
  min-width: 140px;
}
</style>
