<template>
  <div class="mx-5">
    <div class="asj-h2">分时调价</div>
    <div class="mt-3.5">
      规则执行时区: PDT
      <div>
        <el-tag v-if="isVisible" class="custom-tag" closable color="#e7edf4" @close="handleClose">
          <template #default>
            <div class="tag-content">
              <strong>自动化分时规则：</strong>
              <p>
                1.
                应用分时调价后，如需手动修改竞价，只能在此操作。在亚马逊后台或其他第三方系统进行的调价操作，竞价将会被当前时段的自动化执行结果覆盖。
              </p>
              <p>2. 广告活动开启分时调价，规则的修改将在下一个整点生效。</p>
            </div>
          </template>
        </el-tag>
      </div>
    </div>
    <el-form ref="formRef" :model="formData" label-position="top" style="margin-top: 20px">
      <el-form-item :rules="{ required: true, message: '必填项', trigger: 'blur' }" prop="name">
        <template #label>
          <span class="asj-h3">模板名称</span>
        </template>
        <el-input v-model="formData.name" style="width: 30%"></el-input>
      </el-form-item>
      <el-form-item :rules="[{ validator: checkConditions, trigger: 'xxx' }]" prop="rule.conditions">
        <template #label>
          <span class="asj-h3">设置竞价</span>
        </template>
        <div class="flex flex-col">
          <div class="flex gap-2 my-2">
            <el-select v-model="timeRange">
              <el-option v-for="item in timeRangeOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
            <el-select v-model="schedule">
              <el-option v-for="item in scheduleOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
            <el-input v-model="bid"
                      clearable
                      oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                      placeholder="1.0"
                      style="width: 150px"
            ></el-input>
            <el-button class="active-btn" link style="color: #3c58af"
                       @click="handleApply">应用
            </el-button>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <TimerBidTable
        ref="tableRef"
        :data="formData.rule.conditions"
        @click="formRef.clearValidate('rule.conditions')" />
    <div class="auto-page-foot">
      <el-button style="width: 200px" @click="cancel">取消</el-button>
      <el-button style="width: 200px" type="primary" @click="submitForm">提交</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
/**
 * @Name: timer-bid.vue
 * @Description: 自动化规则模板-分时调价
 * @Author: xinyan
 */
import { ref, onMounted, Ref } from 'vue';
import TimerBidTable from '/@/components/TimerBidTable/index.vue';
import { userFormData } from '/@/views/components/auto/auto-campaigns/common';

interface Props {
  mode: string;
  data: AutoTemplate;
  submitFormData: Function;
}

const props = defineProps<Props>();
const emits = defineEmits(['refresh']);

const formRef = ref();
const formData = ref(props.data)

const timeRange = ref('Option1');
const schedule = ref('Option1');
const bid = ref('1.0');
const tableRef = ref(null);

const timeRangeOptions = [
  {
    value: 'Option1',
    label: '24小时: 00:00-23:59',
  },
  {
    value: 'Option2',
    label: '凌晨: 00:00-06:59',
  },
  {
    value: 'Option3',
    label: '上午: 7:00-11:59',
  },
  {
    value: 'Option4',
    label: '工作时: 9:00-16:59',
  },
  {
    value: 'Option5',
    label: '下午: 12:00-16:59',
  },
  {
    value: 'Option6',
    label: '晚上: 17:00-20:59',
  },
  {
    value: 'Option7',
    label: '深夜: 21:00-23:59',
  },
];

const scheduleOptions = [
  {
    value: 'Option1',
    label: '每一天',
  },
  {
    value: 'Option2',
    label: '仅在工作日',
  },
  {
    value: 'Option3',
    label: '仅在周末',
  },
];
const isVisible = ref(true);

if (props.mode === 'add') {
  for (let i = 0; i < 7; i++) {
    const tmp = [];
    for (let j = 0; j < 24; j++) {
      tmp.push(0);
    }
    formData.value.rule.conditions.push(tmp);
  }
}

function handleClose() {
  isVisible.value = false;
}

const handleApply = () => {
  if (tableRef.value) {
    tableRef.value.applyBid(timeRange.value, schedule.value, parseFloat(bid.value));
  }
};

const checkConditions = (rule: any, value: any, callback: any) => {
  for (const bidList of formData.value.rule.conditions) {
    for (const val of bidList) {
      if (val > 0) {
        return callback();
      }
    }
  }
  callback(new Error('请先设置竞价！'));
};
const submitForm = async () => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      await props.submitFormData();
      emits('refresh');
    } else {
      console.log('验证失败');
    }
  });
};
const cancel = () => {
  emits('refresh');
};
</script>

<style scoped>
.custom-tag {
  height: auto;
  padding: 8px 8px 8px 16px;
  color: #505968;
  border-color: #abbbd8;
  align-items: flex-start !important;
  white-space: normal;
  line-height: 1.5;
}
</style>
