<template>
  <div class="screen-config-page">
    <!-- 头部导航 -->
    <s-header :name="$t('advertManage.advertRule.addRule.adScreen')" :noback="false" />
    
    <!-- 主要内容区域 -->
    <div class="screen-config-container">
      <!-- 顶部标签选择区域 -->
      <div class="tag-section">
        <div class="screen-indicator">
          {{ 
            cofficentForm.screenType === "0" 
              ? $t("advertManage.advertRule.addRule.AScreen") 
              : $t("advertManage.advertRule.addRule.BScreen") 
          }}
        </div>
        
        <div class="selected-tags">
          <div 
            v-for="(item, index) in tagList" 
            :key="index" 
            class="tag-item"
          >
            <span class="tag-name">{{ item.name }}</span>
            <van-icon 
              @click.stop="delTag(item, index)" 
              name="clear" 
              class="tag-remove-icon" 
            />
          </div>
        </div>
        
        <div class="submit-section">
          <van-button 
            round 
            type="primary" 
            class="submit-button"
            @click="submitClk"
          >
            {{ $t("advertManage.advertRule.add") }}
          </van-button>
        </div>
      </div>
      
      <!-- 表单区域 -->
      <div class="form-section">
        <!-- 用户名搜索 -->
        <van-field 
          :border="false" 
          v-model="cofficentForm.username" 
          clearable
          :placeholder="$t('advertManage.advertRule.addRule.userNamePlace')"
          :label="$t('advertManage.advertRule.addRule.userName')"
          class="custom-field"
        >
          <template #left-icon>
            <van-icon name="user" color="#1989fa" />
          </template>
        </van-field>
        
        <!-- 广告名称 -->
        <van-field 
          :border="false" 
          v-model="cofficentForm.name" 
          clearable
          :placeholder="$t('advertManage.advertRule.addRule.adNamePlace')"
          :label="$t('advertManage.advertRule.addRule.adName')"
          class="custom-field"
        >
          <template #left-icon>
            <van-icon name="label" color="#1989fa" />
          </template>
        </van-field>
        
        <!-- 广告类型选择 -->
        <van-field 
          :border="false" 
          readonly 
          v-model="cofficentForm.type"
          :placeholder="$t('advertManage.advertRule.addRule.adTypePlace')"
          :label="$t('advertManage.advertRule.addRule.adType')"
          class="custom-field"
          @click="busiInpClk"
        >
          <template #left-icon>
            <van-icon name="photo" color="#1989fa" />
          </template>
          <template #right-icon>
            <van-icon 
              v-if="cofficentForm.type" 
              @click.stop="cofficentForm.type = ''" 
              name="clear" 
              class="field-clear-icon" 
            />
            <van-icon name="arrow-down" class="field-arrow-icon" />
          </template>
        </van-field>
        
        <!-- 搜索按钮 -->
        <div class="search-button-section">
          <van-button 
            round 
            type="primary" 
            size="large" 
            class="search-button"
            @click="toSearch"
          >
            <van-icon name="search" class="button-icon" />
            {{ $t("advertManage.advertRule.addRule.search") }}
          </van-button>
        </div>
      </div>
      
      <!-- 可用标签区域 -->
      <div class="available-tags-section">
        <h3 class="section-title">
          可用广告
        </h3>
        
        <div class="tag-list">
          <div 
            v-for="(item, index) in allTagList" 
            :key="index" 
            class="tag-item"
            @click="addAllTag(item)"
          >
            <span class="tag-name">{{ item.name }}</span>
            <van-icon name="plus" class="tag-add-icon" />
          </div>
        </div>
      </div>
    </div>
    
    <!-- 广告类型选择弹窗 -->
    <van-popup 
      v-model:show="busiPopShow" 
      position="bottom" 
      round
      class="type-select-popup"
    >
      <van-picker 
        :title="$t('advertManage.advertRule.addRule.adTypePlace')" 
        :columns="busiPopList"
        :columns-field-names="busiPopFieldName" 
        @confirm="busiPopConfirm" 
        @cancel="busiPopCancel" 
      />
    </van-popup>
  </div>
</template>

<script>
// 导入公用方法
import { setLocal, styleUrl } from "@/common/js/utils";
// 导入接口
import { Api_getAdRuleList } from "@/service/advertManage";
import sHeader from "@/components/SimpleHeader";
import { onMounted, reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
export default {
  components: { sHeader },
  setup() {
    const cofficentForm = reactive({
      type: "",
      screenType: "0",
      username: "",
      name: "",
    });
    const route = useRoute();
    const router = useRouter();
    const tagList = ref([]);
    // 导入vuex
    const store = useStore();
    // 所有标签
    const allTagList = ref([]);
    const busiPopList = reactive([
      {
        name: "280",
        id: "280",
      },
      {
        name: "280en",
        id: "280en",
      },
      {
        name: "301",
        id: "301",
      },
      {
        name: "301en",
        id: "301en",
      },
      {
        name: "320",
        id: "320",
      },
      {
        name: "320en",
        id: "320en",
      },
      {
        name: "330",
        id: "330",
      },
      {
        name: "330en",
        id: "330en",
      },
      {
        name: "P10",
        id: "P10",
      },
      {
        name: "P10en",
        id: "P10en",
      },
      {
        name: "P20",
        id: "P20",
      },
      {
        name: "P20en",
        id: "P20en",
      },
      {
        name: "P30",
        id: "P30",
      },
      {
        name: "P30en",
        id: "P30en",
      },
    ]);
    const busiPopFieldName = reactive({
      text: "name",
      value: "id",
    });
    // 广告类型弹窗
    const busiPopShow = ref(false);
    // 刚进页面
    onMounted(() => {
      // 加载样式
      styleUrl('advertManage');
      const val = route.query.val || "0";
      console.log("val", val);
      // val=0是A屏进来的，1是B屏进来的
      if (val) {
        cofficentForm.screenType = val;
      }
      // 回显
      const A_advertList = store.state.$S_advertManage_A_advertList;
      const B_advertList = store.state.$S_advertManage_B_advertList;
      if (val === '0') {
        tagList.value = A_advertList;
      } else {
        tagList.value = B_advertList;
      }
    });
    // 点击所有标签
    const addAllTag = (row) => {
      if (tagList.value.length > 0) {
        const isRepeat = tagList.value.some((item) => {
          return item.id === row.id;
        });
        if (isRepeat) {
          return;
        }
      }
      tagList.value.push(row);
    };
    // 点击删除
    const delTag = (row, idx) => {
      tagList.value.splice(idx, 1);
    };
    // 点击添加
    const submitClk = () => {
      if (tagList.value.length > 0) {
        if (cofficentForm.screenType === "0") {
          // 储存数据到vuex
          store.commit("MUTA_A_ADVERTLIST", tagList.value);
          setLocal("advertManage-A-advertList", JSON.stringify(tagList.value));
        } else {
          // 储存数据到vuex
          store.commit("MUTA_B_ADVERTLIST", tagList.value);
          setLocal("advertManage-B-advertList", JSON.stringify(tagList.value));
        }
        setTimeout(() => {
          router.back();
        }, 300);
      }
    };
    // 点击搜索
    const toSearch = () => {
      Api_getAdRuleList(cofficentForm).then((res) => {
        console.log("res", res);
        allTagList.value = res.data.data || [];
      });
    };
    // 点击类型
    const busiInpClk = () => {
      busiPopShow.value = true;
    };
    const busiPopConfirm = ({ selectedOptions }) => {
      busiPopShow.value = false;
      // console.log("e", e);
      cofficentForm.type = selectedOptions[0].id;
    };
    const busiPopCancel = () => {
      busiPopShow.value = false;
    };
    return {
      cofficentForm,
      tagList,
      allTagList,
      delTag,
      submitClk,
      toSearch,
      busiInpClk,
      busiPopShow,
      busiPopList,
      busiPopFieldName,
      busiPopConfirm,
      busiPopCancel,
      addAllTag,
    };
  },
};
</script>

<style lang="less" scoped>
@primary-color: #4d6add;
@error-color: #f56c6c;
@text-color: #333;
@border-color: #dcdfe6;

.screen-config-page {
  background-color: #f5f7fa;
  min-height: 100vh;
}

.screen-config-container {
  padding: 16px;
}

.tag-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  justify-content: center;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 15px;
  margin-bottom: 16px;
  
  .screen-indicator {
    flex: 0 0 40px;
    display: flex;
    align-items: center;
  }
  
  .selected-tags {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    min-height: 40px;
  }
  
  .submit-section {
    flex: 0 0 auto;
    margin-left: 12px;
    
    .submit-button {
      min-width: 80px;
      // height: 34px;
    }
  }
}

.form-section {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 16px;
  margin-bottom: 16px;
}

.search-button-section {
  margin-top: 20px;
  
  .search-button {
    .button-icon {
      margin-right: 8px;
    }
  }
}

.available-tags-section {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 16px;
  
  .section-title {
    // font-size: @font-size-lg;
    font-weight: 600;
    // color: @text-color;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid @border-color;
  }
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.tag-item {
  display: flex;
  align-items: center;
  background: linear-gradient(135deg, #e59a6d, #e58765);
  color: white;
  border-radius: 20px;
  padding: 6px 14px;
  // font-size: @font-size-sm;
  cursor: pointer;
  transition: all 0.3s;
  
  .tag-name {
    margin-right: 6px;
  }
  
  .tag-add-icon, .tag-remove-icon {
    font-size: 16px;
    transition: transform 0.3s;
  }
  
  .tag-add-icon {
    opacity: 0.8;
  }
  
  &:hover {
    box-shadow: 0 4px 8px rgba(229, 138, 101, 0.3);
    transform: translateY(-2px);
    
    .tag-add-icon, .tag-remove-icon {
      transform: scale(1.2);
    }
  }
}

.custom-field {
  background-color: #fafafa;
  border-radius: 10px;
  padding: 10px 16px;
  margin-bottom: 15px;
  
  :deep(.van-field__label) {
    font-weight: 500;
    // color: @text-color-secondary;
  }
  
  .field-clear-icon {
    color: #c8c9cc;
    margin-right: 8px;
    
    &:active {
      color: #969799;
    }
  }
  
  .field-arrow-icon {
    color: #969799;
  }
}

.type-select-popup {
  :deep(.van-picker) {
    background-color: #f7f8fa;
  }
  
  :deep(.van-picker__toolbar) {
    background-color: white;
  }
}

@media (max-width: 480px) {
  .tag-section {
    
    .selected-tags {
      width: 100%;
      // margin-bottom: 15px;
    }
    

  }
  
  .tag-item {
    padding: 5px 10px;
    // font-size: @font-size-sm - 1;
  }
  
  .form-section {
    padding: 12px;
  }
}
</style>
