<script lang="ts" setup>
import { ref, onMounted, Ref, watch, reactive } from 'vue';
import TimerBidTable from '/@/components/TimerBidTable/index.vue';
import SelectTmpl from './select-tmpl.vue';
import { userFormData } from './common';
import XEUtils from 'xe-utils';
import dayjs from 'dayjs';

interface Props {
  data: {
    campaignId: string;
    campaignType: string;
    profileId: string;
    ruleType: number;
  };
  RuleStatusButton?: { [key: string]: any };
}

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

const formRef = ref();
const { formData, submitFormData } = userFormData(props);

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 submit = async () => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      await submitFormData();
      emits('refresh');
    } else {
      console.log('验证失败');
    }
  });
};
const cancel = async () => {
  emits('refresh');
};

const timeRange = ref('Option1');
const schedule = ref('Option1');
const bid = ref('');
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)
function handleClose() {
  isVisible.value = false
}
</script>

<template>
  <div class="mx-5">
    <div class="asj-h2">分时调价</div>
    <div class="mt-3.5">
      规则执行时区: PDT
      <div>
        <el-tag v-if="isVisible" closable @close="handleClose" color="#e7edf4" class="custom-tag">
          <template #default>
            <div class="tag-content">
              <strong>自动化分时规则：</strong>
              <p>
                1. 应用分时调价后，如需手动修改竞价，只能在Xmars操作。在亚马逊后台或其他第三方系统进行的调价操作，竞价将会被Xmars当前时段的自动化执行结果覆盖。
              </p>
              <p>2. 广告活动开启分时调价，规则的修改将在下一个整点生效。</p>
            </div>
          </template>
        </el-tag>
      </div>
    </div>
    <SelectTmpl :data="formData" />
    <el-card class="mt-3">
      <el-form :model="formData" label-position="top" style="margin-top: 20px" ref="formRef">
        <el-form-item prop="rule.conditions" :rules="[{ validator: checkConditions, trigger: 'xxx' }]">
          <template #label>
            <span class="custom-title-icon"></span>
            <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" style="width: 150px" placeholder="1.0"></el-input>
              <el-button link class="active-btn" style="color: #3c58af">应用</el-button>
            </div>
            <TimerBidTable
              :data="formData.rule.conditions"
              @click="formRef.clearValidate('rule.conditions')"
              :disabled="formData.useTmpl" />
          </div>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="auto-page-foot">
      <el-button style="width: 200px" @click="cancel">取消</el-button>
      <el-button style="width: 200px" type="primary" @click="submit">提交</el-button>
    </div>
  </div>
</template>

<style scoped>
.active-btn:hover {
  color: #7a9ce4 !important; /* 如果需要改变字体颜色 */
  transition: all 0.3s; /* 添加过渡效果 */
}

.custom-title-icon {
  padding-right: 10px;
}

.custom-title-icon:before {
  content: '';
  width: 4px;
  height: 15px;
  background: #3569d6;
  position: absolute;
  transform: translateY(25%);
}

.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;
}

.tag-content {
  display: block;
  max-width: 100%;
}
</style>
