<template>
  <div
    class="search mb-[15px] flex h-[170px] w-full flex-col items-center justify-around"
  >
    <div class="flex gap-10">
      <router-link v-for="(v, i) in subMenus" :key="i" :to="v.path">
        {{ v.meta.title }}
      </router-link>
    </div>
    <div class="flex items-center justify-center gap-2">
      <div class="search-box">
        <div class="flex gap-2">
          <div class="item border">
            <span class="pl-[15px] text-[14px]">选择时间</span>
            <el-popover
              trigger="click"
              placement="bottom-start"
              popper-class="search-popover"
            >
              <div class="flex flex-col items-center">
                <div class="tab-box">
                  <div
                    :class="['tab-item', { active: form.type == 'year' }]"
                    @click="changeTab('year')"
                  >
                    按年
                  </div>
                  <div
                    :class="['tab-item', { active: form.type == 'month' }]"
                    @click="changeTab('month')"
                  >
                    按月
                  </div>
                  <div
                    :class="['tab-item', { active: form.type == 'date' }]"
                    @click="changeTab('date')"
                  >
                    按日
                  </div>
                </div>
                <div class="tab-content w-ull">
                  <date-picker
                    v-show="form.type == 'year'"
                    v-model="form.year"
                    type="year"
                    value-format="yyyy"
                    placeholder="选择年"
                  >
                  </date-picker>
                  <date-picker
                    v-show="form.type == 'month'"
                    v-model="form.month"
                    type="month"
                    value-format="yyyy-MM"
                    placeholder="选择月"
                  >
                  </date-picker>
                  <date-picker
                    v-show="form.type == 'date'"
                    v-model="form.day"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日"
                  >
                  </date-picker>
                </div>
              </div>
              <el-input
                size="small"
                placeholder="暂未选择"
                :value="dateInputVal"
                slot="reference"
                clearable
                @clear="handleClearDate"
              ></el-input>
            </el-popover>
          </div>
          <div
            class="item border"
            v-if="$route.path == '/enginesearch/queueupdata'"
          >
            <span class="pl-[15px] text-[14px]">选择业务</span>
            <el-popover
              trigger="click"
              placement="bottom-start"
              popper-class="search-popover"
            >
              <div class="w-full">
                <div class="mb-3">推荐业务</div>
                <div class="flex w-full flex-col gap-3">
                  <div
                    class="flex cursor-pointer hover:text-primary"
                    v-for="(v, i) in calcBusinessList"
                    :key="v.id"
                    @click="form.businessName = v.businessName"
                  >
                    <div :class="['mr-4', ' w-[20px]', `index-${i + 1}`]">
                      {{ i + 1 }}
                    </div>
                    <div class="flex-1 truncate">{{ v.businessName }}</div>
                  </div>
                </div>
              </div>
              <el-input
                size="small"
                placeholder="请输入"
                slot="reference"
                v-model="form.businessName"
                clearable
                @input="handleBusinessNameInput"
              ></el-input>
            </el-popover>
          </div>
          <div class="item border" v-else>
            <span class="pl-[15px] text-[14px]">选择评价选项</span>
            <el-select
              v-model="form.pjOption"
              placeholder="请选择评价选项"
              clearable
              filterable
              size="small"
            >
              <el-option
                v-for="(v, i) in pjOption"
                :key="i"
                :label="v"
                :value="v"
              ></el-option>
            </el-select>
          </div>
          <div class="item border">
            <span class="pl-[15px] text-[14px]">选择部门</span>
            <el-popover
              trigger="click"
              placement="bottom-start"
              popper-class="search-popover"
            >
              <div class="w-full">
                <div class="mb-3">推荐部门</div>
                <div class="flex w-full flex-col gap-3">
                  <div
                    class="flex cursor-pointer hover:text-primary"
                    v-for="(v, i) in calcDept"
                    :key="v.id"
                    @click="form.deptName = v.name"
                  >
                    <div :class="['mr-4', ' w-[20px]', `index-${i + 1}`]">
                      {{ i + 1 }}
                    </div>
                    <div class="flex-1 truncate">{{ v.name }}</div>
                  </div>
                </div>
              </div>
              <el-input
                size="small"
                placeholder="请输入"
                slot="reference"
                v-model="form.deptName"
                clearable
                @input="handleDeptNameInput"
              ></el-input>
            </el-popover>
          </div>
          <div class="item">
            <span class="pl-[15px] text-[14px]">选择窗口</span>
            <el-popover
              trigger="click"
              placement="bottom-start"
              popper-class="search-popover"
            >
              <div class="w-full">
                <div class="mb-3">推荐窗口</div>
                <div class="flex w-full flex-col gap-3">
                  <div
                    class="flex cursor-pointer hover:text-primary"
                    v-for="(v, i) in calcWindow"
                    :key="v.id"
                    @click="form.windowNum = v.fromnum"
                  >
                    <div :class="['mr-4', ' w-[20px]', `index-${i + 1}`]">
                      {{ i + 1 }}
                    </div>
                    <div class="flex-1 truncate">
                      {{ v.fromnum }}-{{ v.name }}
                    </div>
                  </div>
                </div>
              </div>
              <el-input
                size="small"
                placeholder="请输入"
                slot="reference"
                v-model="form.windowNum"
                clearable
                @input="handleWindowInput"
              ></el-input>
            </el-popover>
          </div>
        </div>
        <div class="btn" @click="handleSearch">检索</div>
      </div>
      <div class="back-btn" @click="$router.push('/engine')">返回</div>
    </div>
  </div>
</template>

<script>
import { findBottomSubarrays } from "@/utils";
import { mapState } from "vuex";
let pjOption = ["非常满意", "满意", "基本满意", "不满意", "非常不满意"];
export default {
  components: {},
  data() {
    return {
      pjOption,
      calcBusiness: [],
      calcDept: [],
      calcWindow: [],
      subMenus: [],
      form: {
        type: "year",
        businessName: "",
        deptName: "",
        windowNum: "",
        year: "",
        month: "",
        day: "",
        pjOption: "",
      },
    };
  },
  computed: {
    ...mapState("user", ["businessList", "deptList", "windowList"]),
    activeKey() {
      return this.$route.path;
    },
    dateInputVal() {
      if (this.form.type == "year") {
        return this.form.year;
      } else if (this.form.type == "month") {
        return this.form.month;
      } else {
        return this.form.day;
      }
    },
    calcBusinessList: {
      get() {
        return this.calcBusiness;
      },
      set(val) {
        this.calcBusiness = val;
      },
    },
    calcDeptList: {
      get() {
        return this.calcDept;
      },
      set(val) {
        this.calcDept = val;
      },
    },
    calcWindowList: {
      get() {
        return this.calcWindow;
      },
      set(val) {
        this.calcWindow = val;
      },
    },
  },
  created() {
    this.calcBusiness = this.businessList.slice(0, 10);
    this.calcDept = this.deptList.slice(0, 10);
    this.calcWindow = this.windowList.slice(0, 10);
    this.getSubMenus();
  },
  mounted() {},
  methods: {
    changeRouter(e) {
      this.$router.push(e.name);
    },
    // 获取当前顶层路由下的所有子路由
    getSubMenus() {
      let path = this.$route?.meta.parentPath
        ? this.$route.meta.parentPath
        : this.$route.path;
      let options = this.$router.options.routes[0].children;
      let curRouters = options.filter((v) => v.path == path);
      this.subMenus = findBottomSubarrays(curRouters).filter(
        (v) => !v.meta.hidden
      );
    },
    changeTab(key) {
      this.form.year = "";
      this.form.month = "";
      this.form.day = "";
      this.form.type = key;
    },

    handleClearDate() {
      this.form.year = "";
      this.form.month = "";
      this.form.day = "";
    },
    handleBusinessNameInput(name) {
      if (name != "") {
        let list = this.businessList.filter((v) =>
          v.businessName.includes(name)
        );
        this.calcBusinessList = list.slice(0, 10);
      } else {
        this.calcBusinessList = this.businessList.slice(0, 10);
      }
    },
    handleDeptNameInput(name) {
      if (name != "") {
        let list = this.deptList.filter((v) => v.name.includes(name));
        this.calcDeptList = list.slice(0, 10);
      } else {
        this.calcDeptList = this.deptList.slice(0, 10);
      }
    },
    handleWindowInput(value) {
      if (value != "") {
        let list = this.windowList.filter(
          (v) => v.name.includes(value) || v.fromnum.includes(value)
        );
        this.calcWindowList = list.slice(0, 10);
      } else {
        this.calcWindowList = this.windowList.slice(0, 10);
      }
    },
    handleSearch() {
      this.$emit("search", this.form);
    },
  },
};
</script>

<style lang="less" scoped>
.search {
  background: url("@/assets/img/engineSearch_bg.svg") no-repeat center / 100%
    100%;
  a {
    color: rgba(254, 254, 254, 0.65);
  }
  .router-link-active {
    color: #fff;
    font-weight: 600;
  }
}
.border {
  border-right: 1px solid #ccc;
}
.search-box {
  height: 68px;
  width: 60%;
  padding: 0px 15px;
  background: #fff;
  border-radius: 34px;
  display: flex;
  align-items: center;
}

.btn,
.back-btn {
  width: 96px;
  height: 60px;
  flex-shrink: 0;
  font-weight: 700;
  color: #fff;
  line-height: 60px;
  text-align: center;
  background: linear-gradient(90deg, #5ab6ff, #2e9aff);
  border-radius: 30px;
  cursor: pointer;
}
.back-btn {
  color: var(--primary);
  background: #fff;
}
:deep(.el-input) {
  .el-input__inner {
    border: none;
  }
}
.tab-box {
  width: 300px;
  height: 40px;
  background-color: #ebebeb;
  border-radius: 20px;
  display: flex;
  position: relative;
  .tab-item {
    width: 100px;
    height: 100%;
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
  .active {
    background-color: #fff;
    border: 2px solid var(--primary);
  }
}
.index-1 {
  color: #ff5948;
}
.index-2 {
  color: #ff6f48;
}
.index-3 {
  color: #ff8d48;
}

:deep(.el-picker-panel) {
  border: none;
  box-shadow: none;
  border-radius: 0px;
}
:global(.search-popover) {
  width: 348px;
  min-height: 330px;
}
</style>
