<template>
  <t-popup
    overlayClassName="select-filter"
    placement="bottom-left"
    :visible="visible"
  >
    <t-button variant="outline" @click="toggleVisible">
      <template #icon><svg-icon name="sort" color="#262626" /></template>
      排序</t-button
    >
    <template #content>
      <div class="filter-box">
        <div class="filter-header">
          <p>请选择排序字段</p>
        </div>
        <div class="filter-body">
          <div class="filter-list">
            <div class="filter-item">
              <div class="filter-field">
                <t-select
                  v-model="fieldId"
                  filterable
                  clearable
                  @change="fieldIdChange"
                >
                  <t-option
                    v-for="item in metadataList"
                    :key="item.fieldId"
                    :label="item.fieldTitle"
                    :value="item.fieldId"
                  />
                </t-select>
              </div>
              <div class="filter-value">
                <t-radio-group
                  variant="primary-filled"
                  v-model="sortMethod"
                  allowUncheck
                >
                  <t-radio-button value="ASC">升序</t-radio-button>
                  <t-radio-button value="DESC">降序</t-radio-button>
                </t-radio-group>
              </div>
            </div>
          </div>
        </div>
        <div class="filter-footer">
          <div></div>
          <t-space :size="10">
            <t-button theme="default" size="small" @click="toggleVisible"
              >取消</t-button
            >
            <t-button theme="primary" size="small" @click="confirmHandle"
              >确定</t-button
            >
          </t-space>
        </div>
      </div>
    </template>
  </t-popup>
</template>

<script setup name="SopSortPanel">
import { randomCode } from '@/utils/tool';
import { ref, computed, reactive } from 'vue';
import { staticMetadata } from '../select-filter/config';
import { MessagePlugin } from 'tdesign-vue-next';

const emit = defineEmits(['confirm']);
const props = defineProps({
  metadata: {
    type: Array,
    default() {
      return [];
    },
  },
});
const sortMethod = ref('');
const visible = ref(false);
const toggleVisible = () => {
  visible.value = !visible.value;
};
const fieldId = ref('');
const metadataList = computed(() => {
  return [...props.metadata, ...staticMetadata];
});
const fieldIdChange = (fieldId) => {
  if (!fieldId) {
    sortMethod.value = undefined;
  }
};
const confirmHandle = () => {
  let data =
    sortMethod.value && fieldId.value
      ? [
          {
            fieldId: fieldId.value,
            order: sortMethod.value,
          },
        ]
      : [];
  emit('confirm', data);
  toggleVisible();
};
</script>

<style lang="less">
.select-filter {
  .t-popup__content {
    padding: 0;
  }
}
</style>
<style lang="less" scoped>
.filter-box {
  width: 400px;

  .filter-header {
    height: 32px;
    margin-bottom: 6px;
    color: @dark-text-color-2;
    padding: 6px 12px 0;
  }
  .filter-body {
    min-height: 60px;
    padding: 0 12px 12px;
  }
  .filter-list {
    margin-top: 6px;
  }
  .filter-item {
    font-size: 0;

    &:not(:first-child) {
      margin-top: 12px;
    }

    & > div {
      display: inline-block;
      vertical-align: top;
      font-size: 14px;

      &:not(:first-child) {
        margin-left: 6px;
      }
    }
  }
  .filter-field {
    width: 180px;
  }

  .filter-footer {
    border-top: 1px solid @light-border-color;
    padding: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
