<template>
  <t-popup
    overlayClassName="select-filter"
    placement="bottom-left"
    :visible="visible"
  >
    <t-button variant="outline" @click="toggleVisible">
      <template #icon><svg-icon name="filter" color="#262626" /></template>
      筛选条件
    </t-button>
    <template #content>
      <div class="filter-box">
        <div class="filter-header">
          <p
            >筛选出符合以下<t-select
              size="small"
              v-model="formWidgetMetadataOperation"
              style="
                width: 80px;
                display: inline-block;
                margin-left: 5px;
                margin-right: 5px;
              "
            >
              <t-option value="AND" label="全部"></t-option>
              <t-option value="OR" label="任一"></t-option> </t-select
            >内容</p
          >
        </div>
        <div class="filter-body">
          <t-button theme="primary" size="small" @click="addHandle">
            <template #icon
              ><svg-icon name="add-circle" color="#fff" /></template
            >添加过滤条件
          </t-button>
          <div class="filter-list">
            <div v-for="item in dataList" :key="item.key" class="filter-item">
              <div class="filter-field">
                <t-select
                  v-model="item.fieldId"
                  filterable
                  @change="fieldChange(item)"
                >
                  <t-option
                    v-for="item in metadataList"
                    :key="item.fieldId"
                    :label="item.fieldTitle"
                    :value="item.fieldId"
                  />
                </t-select>
              </div>
              <div class="filter-operator">
                <t-select
                  v-model="item.operator"
                  @change="operatorChange(item)"
                >
                  <t-option
                    v-for="item in item.operatorList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </t-select>
              </div>
              <div class="filter-value">
                <field-value
                  v-model="item.fieldValue"
                  :field="item"
                ></field-value>
              </div>
              <div class="filter-action">
                <t-button
                  shape="square"
                  variant="text"
                  @click="deleteHandle(item)"
                >
                  <template #icon
                    ><svg-icon name="delete" color="#262626"
                  /></template>
                </t-button>
              </div>
            </div>
          </div>
        </div>
        <div class="filter-footer">
          <t-button variant="outline" size="small" @click="clearHandle">
            <template #icon><svg-icon name="clear" color="#262626" /></template
            >清除
          </t-button>
          <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="SelectFilter">
import { randomCode } from '@/utils/tool';
import { ref, computed, reactive } from 'vue';
import { getOperatorByCode, staticMetadata } from './config';
import FieldValue from './field-value.vue';
import { MessagePlugin } from 'tdesign-vue-next';

const emit = defineEmits(['confirm']);
const props = defineProps({
  metadata: {
    type: Array,
    default() {
      return [];
    },
  },
});
const formWidgetMetadataOperation = ref('AND');
const visible = ref(false);
const toggleVisible = () => {
  visible.value = !visible.value;
};

const dataList = ref([]);

const metadataList = computed(() => {
  // return [...props.metadata, ...staticMetadata];
  return [...props.metadata];
});

const selections = ['SINGLE_SELECT', 'MULTIPLE_SELECT', 'CHECKBOX', 'RADIO'];
const fieldChange = (row) => {
  const metad = metadataList.value.find((item) => item.fieldId === row.fieldId);
  row.code = metad.code;
  row.operatorList = getOperatorByCode(metad.code);
  if (metad.options) row.options = metad.options;
  row.operator = '';

  // value
  row.fieldValue = null;
  if (selections.includes(row.code)) row.fieldValue = [];
};
const operatorChange = (row) => {
  if (row.operator === 'RANGE' || selections.includes(row.code)) {
    row.fieldValue = [];
    return;
  }

  row.fieldValue = null;
};

const addHandle = () => {
  dataList.value.push(
    reactive({
      key: randomCode(),
      code: 'TEXT',
      fieldId: null,
      operator: '',
      fieldValue: null,
      operatorList: [],
      options: [],
    })
  );
};
const deleteHandle = (row) => {
  dataList.value = dataList.value.filter((item) => item.key !== row.key);
};
const clearHandle = () => {
  dataList.value = [];
  emit('confirm', dataList.value);
  toggleVisible();
};
const confirmHandle = () => {
  if (
    dataList.value.some(
      (item) => !item.fieldId || !item.operator || !item.fieldValue
    )
  ) {
    MessagePlugin.error('有字段未完成设置！');
    return;
  }
  emit('confirm', dataList.value, formWidgetMetadataOperation.value);
  toggleVisible();
};
</script>

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

  .filter-header {
    height: 32px;
    margin-bottom: 6px;
    color: @dark-text-color-2;
    padding: 6px 12px 0;
  }
  .filter-body {
    min-height: 200px;
    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-operator {
    width: 120px;
  }
  .filter-value {
    width: 260px;
  }
  .filter-action {
    width: 32px;
    text-align: center;
  }
  .filter-footer {
    border-top: 1px solid @light-border-color;
    padding: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
