<template>
  <div>
    <el-dialog title="选择标签" :visible.sync="visiable" width="900px" :before-close="close">
      <div class="container">
        <el-tabs tab-position="left" @tab-click="onTabsClick" v-model="activeName">
          <el-tab-pane :label="item.levelName" v-for="item in tabList" :key="item.id" :name="item.id" v-loading="tabPaneLoading">
            <div class="rightContent">
              <div class="searchWrap">
                <div><el-input prefix-icon="el-icon-search" v-model="search" placeholder="请输入标签名称" @keyup.enter.native="getList" clearable @clear="getList"></el-input></div>
                <div class="btn"><el-button size="small" type="primary" @click="showPopVisible = true">新增标签</el-button></div>
              </div>
              <div class="radioContainer">
                <div v-for="item in valueList" :key="item.tagId" class="radioLine">
                  <div class="lineTitle">{{ item.tagName }}</div>
                  <el-radio v-for="value in item.itemList" :key="value.tagItemId" :label="value" v-model="selectedTag">{{ value.tagItemName }}</el-radio>
                </div>
              </div>
              <el-pagination class="fr" layout="prev, pager, next" v-if="totalCount" :total="totalCount" :page-size="pageSize" :current-page.sync="currentPage" @current-change="getGroupList"> </el-pagination>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <manual-tag-edit :options="manualTagPop" :showPop.sync="showPopVisible" @save="addNewTag"></manual-tag-edit>
  </div>
</template>

<script>
import { pageMemberTagDetail, platformHomePageV2 } from '@/service/api/aiApi.js';
import ManualTagEdit from './manualTagEdit';
export default {
  data() {
    return {
      activeName: '',
      search: '',
      tabList: [],
      valueList: [],
      tabPaneLoading: false,
      totalCount: 0,
      currentPage: 1,
      pageSize: 5,
      showPopVisible: false,
      manualTagPop: {
        show: false,
        popTitle: '',
        tagId: '',
        tagLevelGroupId: '',
        tagTwoLevelGroupId: '',
        isSync: false
      },
      selectedTag: ''
    };
  },
  props: {
    visiable: {
      type: Boolean,
      default: false
    }
  },
  mounted() {
    this.getPlatformHomePageV2();
  },
  components: {
    ManualTagEdit
  },
  methods: {
    confirm() {
      if (!this.selectedTag) {
        this.$message.waring('请选择标签');
        return;
      }
      this.$emit('save', this.selectedTag);
      this.close();
    },
    addNewTag() {
      this.activeName = this.tabList[0].id;
      this.currentPage = 1;
      this.getPageMemberTagDetail();
    },
    getList() {
      this.currentPage = 1;
      this.getPageMemberTagDetail();
    },
    onTabsClick(val) {
      const { name } = val;
      console.log(name);
      this.getPageMemberTagDetail();
      this.currentPage = 1;
    },
    close() {
      this.selectedTag = '';
      this.$emit('update:visiable', false);
    },
    async getPageMemberTagDetail() {
      const param = {
        search: this.search,
        tagLevelGroupId: this.activeName,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      const { result } = await pageMemberTagDetail(param);
      if (result) {
        this.totalCount = result.totalCount;
        this.valueList = result.result;
      }
    },
    async getPlatformHomePageV2() {
      const param = {
        tagLevelType: 2
      };
      const { result } = await platformHomePageV2(param);
      if (result) {
        this.tabList = [{ levelName: '所有活动标签', id: result[0].tagLevelGroupId }, ...result[0].children];
      }
      if (this.tabList.length) {
        this.activeName = this.tabList[0].id;
        this.currentPage = 1;
        this.getPageMemberTagDetail();
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.rightContent {
  padding: 15px 18px;
  box-sizing: border-box;
}
.searchWrap {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}
.container {
  height: 521px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
}
.radioContainer {
  height: 400px;
  overflow-y: auto;
  .el-radio + .el-radio {
    margin: 0;
  }
}
.radioLine {
  .lineTitle {
    font-weight: bolder;
    margin-bottom: 12px;
    width: 100%;
  }
  display: flex;
  flex-wrap: wrap;
  gap: 10px;

  margin-bottom: 24px;
}
.fr {
  float: right;
}
</style>
