<template>
  <div class="normal-container">
    <div class="view-create-box">
      <div class="left-box">
        <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
          <a-form-item label="项目"
            ><a-select
              placeholder="请选择项目"
              :options="projectSel"
              v-model:value="projectId"
              @change="onProjectChange"
            ></a-select
          ></a-form-item>
          <a-form-item label="数据来源">
            <a-select
              placeholder="请选择"
              :options="modularSel"
              v-model:value="modularId"
              @change="onModularChange"
            ></a-select>
          </a-form-item>
          <a-form-item label="展示类型">
            <a-select
              placeholder="请选择展示类型"
              :options="showTypeSel"
              v-model:value="showTypeId"
              @change="onShowTypeChange"
            ></a-select>
          </a-form-item>
          <a-form-item label="是否导出" v-if="showTypeId === 1">
            <a-switch
              v-model:checked="isExport"
              :checkedValue="1"
              :unCheckedValue="0"
            >
            </a-switch>
          </a-form-item>
          <a-form-item label="字段" v-if="fieldList.length">
            <a-checkbox-group v-model:value="fieldIds">
              <a-checkbox
                v-for="(item, index) in fieldList"
                :key="index"
                :value="item.value"
                >{{ item.label }}</a-checkbox
              >
            </a-checkbox-group>
          </a-form-item>
          <a-form-item label="x轴" v-if="xDataList.length">
            <a-radio-group
              :options="xDataList"
              v-model:value="xDataId"
            ></a-radio-group>
          </a-form-item>
          <a-form-item label="y轴" v-if="yDataList.length">
            <a-checkbox-group
              :options="yDataList"
              v-model:value="yDataId"
            ></a-checkbox-group>
          </a-form-item>
        </a-form>
        <div class="footer">
          <a-button
            class="preview-btn"
            :loading="previewLoading"
            @click="() => {toPreview({})}"
            >预览</a-button
          >
          <a-button type="primary" @click="addViewName">点击保存</a-button>
        </div>
      </div>
      <div class="right-box">
        <div class="y-table-container" v-if="previewData.type === 'table'">
          <div class="y-table-name">
            <div class="title">{{ previewData.preview_name }}</div>
          </div>
          <div class="y-table-filter">
            <div
              v-for="item in previewData.filterConfig"
              :key="item.name"
              class="filter-item"
            >
              <span>{{ item.label }}：</span>
              <a-select
                v-if="item.type === 'select'"
                class="input-item"
                :options="item.options"
                v-model:value="previewData.filterData[item.name]"
                placeholder="请选择"
                allow-clear
                @change="toFilt"
              ></a-select>
              <a-input
                v-if="item.type === 'text'"
                class="input-item"
                placeholder="请输入"
                allow-clear
                v-model:value="previewData.filterData[item.name]"
                @change="toFilt"
              />
              <a-range-picker
                v-if="item.type === 'time'"
                class="date-item"
                v-model:value="previewData.filterData[item.name]"
                @change="toFilt"
              />
              <a-range-picker
                v-else-if="item.type === 'date_time'"
                class="date-time-item"
                v-model:value="previewData.filterData[item.name]"
                show-time
                format="YYYY-MM-DD HH:mm"
                @change="toFilt"
              />
              <!-- 数值区间 -->
              <div
                v-else-if="item.type === 'number_range' && previewData.filterData[item.name]"
                class="number_range_box"
              >
                <a-input-number
                  placeholder="最小值"
                  style="width: 140px"
                  v-model:value="previewData.filterData[item.name].min"
                  @change="toFilt"
                />
                <span class="divider"> - </span>
                <a-input-number
                  placeholder="最大值"
                  style="width: 140px"
                  v-model:value="previewData.filterData[item.name].max"
                  @change="toFilt"
                />
              </div>
            </div>
            <div v-if="previewData.isExport" class="filter-item">
              <a-button type="primary">导出</a-button>
            </div>
          </div>
          <div class="y-table-content">
            <a-table
              :columns="previewData.columnConfig"
              :data-source="previewData.dataList"
              :pagination="false"
              :scroll="{ x: 1000, y: `calc(100vh - 260px)` }"
              size="small"
              bordered
            >
              <template #bodyCell="{ column, record }">
                <a-image
                  v-if="column.show_type === 'img'"
                  :src="record[column.dataIndex]"
                  :width="160"
                />
                <a
                  v-else-if="column.show_type === 'link'"
                  target="_blank"
                  :href="record[column.dataIndex]"
                  >{{ record[column.dataIndex] }}</a
                >
                <div
                  v-else-if="column.show_type === 'richText'"
                  v-html="record[column.dataIndex]"
                ></div>
                <template v-else>{{ record[column.dataIndex] }}</template>
              </template>
            </a-table>
            <a-pagination
              v-model:current="previewData.page"
              :total="previewData.total"
              :page-size="previewData.perPage"
              :hide-on-single-page="false"
              size="small"
              class="pagination-box"
              @change="() => { toPreview({}) }"
            />
          </div>
        </div>
        <y-chart
          v-else-if="previewData.type === 'chart'"
          :chart-cfg="previewData.chartCfg"
          :filter-config="previewData.filter"
          @toFilt="() => {toPreview({})}"
        ></y-chart>
        <div class="preview-area" v-else>
          <div><BarChartOutlined /></div>
          <div>预览区</div>
        </div>
      </div>
      <a-modal
        :open="nameVisible"
        title="保存视图"
        :bodyStyle="{ paddingTop: '20px' }"
        @cancel="nameVisible = false"
        @ok="toSaveView"
      >
        <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
          <a-form-item label="视图名称">
            <a-input placeholder="请输入视图名称" v-model:value="previewName" />
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import {
  getProModularField,
  preview,
  saveView,
  getShowTypeSelect,
  getFieldOpts,
} from "./service";
import { message } from "ant-design-vue";
import { BarChartOutlined } from "@ant-design/icons-vue";
import yChart from "@/components/common/y-chart.vue";
import { cloneDeep } from "lodash-es";

const projectSel = ref([]); // 项目下拉
const modularSel = ref([])  // 数据来源下拉
const showTypeSel = ref([]); // 展示类型下拉
const fieldList = ref([]); // 字段列表
const xDataList = ref([]); // x轴数据列表
const yDataList = ref([]); // y轴数据列表
const projectId = ref();
const modularId = ref();
const showTypeId = ref();
const fieldIds = ref([]);
const xDataId = ref();
const yDataId = ref();
const isExport = ref(0);

const previewLoading = ref(false);
const nameVisible = ref(false);
const previewName = ref();

const previewData = reactive({
  type: "",
  filterConfig: [], // 筛选条件
  columnConfig: [], // 表格表头
  dataList: [], // 表格数据
  filterData: {},
  config: {},
  filter: [],
  page: 1,
  perPage: 20,
  total: 0,
});

onMounted(() => {
  toGetProModularField();
  toGetShowTypes();
});

const toGetProModularField = () => {
  getProModularField().then((res) => {
    projectSel.value = res.data;
  });
};

// 获取展示类型下拉
const toGetShowTypes = () => {
  getShowTypeSelect().then((res) => {
    showTypeSel.value = res.data;
  });
};

// 获取字段列表
const toGetFieldOpts = () => {
  getFieldOpts({
    modularId: modularId.value,
    showTypeId: showTypeId.value,
  }).then((res) => {
    fieldList.value = res.data.list ? tranformList(res.data.list) : [];
    xDataList.value = res.data.x_data ? tranformList(res.data.x_data) : [];
    yDataList.value = res.data.y_data ? tranformList(res.data.y_data) : [];
    if (!fieldList.value.length) {
      fieldIds.value = [];
    } else {
      xDataId.value = undefined;
      yDataId.value = [];
    }
  });
};

const onProjectChange = (val) => {
  // 重置数据
  const target = projectSel.value.find((item) => item.value === val);
  modularSel.value = target.child;
  modularId.value = undefined;
  resetSelectData();
  resetPreviewData();
};

const onModularChange = () => {
  resetSelectData();
  resetPreviewData();
};

const onShowTypeChange = () => {
  toGetFieldOpts();
};

const tranformList = (list) => {
  return list.map((item) => {
    return {
      label: item.field_name,
      value: item.field_id,
    };
  });
}

// 重置配置数据
const resetSelectData = () => {
  showTypeId.value = undefined
  isExport.value = 0
  fieldList.value = [];
  fieldIds.value = [];
  xDataList.value = [];
  yDataList.value = [];
  xDataId.value = undefined;
  yDataId.value = [];
}

// 重置预览数据
const resetPreviewData = () => {
  previewData.type = "";
  previewData.filterConfig = [];
  previewData.columnConfig = [];
  previewData.dataList = [];
  previewData.filterData = {};
  previewData.config = {};
  previewData.page = 1;
  previewData.perPage = 20;
  previewData.total = 0;
};

// 请求预览数据
const toPreview = ({filter}) => {
  previewLoading.value = true;
  let filterData
  if (!filter) {
    const cloneFilter = cloneDeep(previewData.filterConfig)
    filterData = cloneFilter
      .filter((item) => {
        return previewData.filterData[item.name] !== undefined && previewData.filterData[item.name] !== null;
      })
      .map((item) => {
        if (item.type === 'time' && previewData.filterData[item.name]) {
          // 日期类型的参数
          return {
            name: item.name,
            type: item.type,
            start_time: previewData.filterData[item.name][0].format('YYYY-MM-DD'),
            end_time: previewData.filterData[item.name][1].format('YYYY-MM-DD'),
          };
        } else if (item.type === 'date_time' && previewData.filterData[item.name]) {
          // 带时分的日期类型参数
          return {
            name: item.name,
            type: item.type,
            start_time: previewData.filterData[item.name][0].format('YYYY-MM-DD HH:mm') + ':00',
            end_time: previewData.filterData[item.name][1].format('YYYY-MM-DD HH:mm') + ':59',
          };
        } else if (item.type === 'number_range') {
          // 数值区间
          return {
            name: item.name,
            type: item.type,
            min: previewData.filterData[item.name].min ? String(previewData.filterData[item.name].min) : '',
            max: previewData.filterData[item.name].max ? String(previewData.filterData[item.name].max) : '',
          };
        } else {
          return {
            name: item.name,
            type: item.type,
            value: previewData.filterData[item.name],
          };
        }
      });
  } else {
    filterData = filter;
  }

  preview({
    modularId: modularId.value,
    fieldIds: fieldIds.value.toString(),
    page: previewData.page,
    perPage: previewData.perPage,
    filter: filterData,
    showTypeId: showTypeId.value,
    xDataId: xDataId.value?.toString(),
    yDataId: yDataId.value?.toString(),
    isExport: isExport.value,
  })
    .then((res) => {
      previewData.type = res.data.type;
      if (res.data.type === "table") {
        previewData.filterConfig = res.data.filter;
        previewData.filterConfig.forEach((item) => {
          if (item.type === 'number_range' && !previewData.filterData[item.name]) {
            previewData.filterData[item.name] = {
              ...item,
              min: undefined,
              max: undefined,
            };
          } else {
            previewData.filterData[item.name] = previewData.filterData[item.name]
              ? previewData.filterData[item.name]
              : undefined;
          }
        });
        previewData.columnConfig = res.data.header;
        previewData.dataList = res.data.data;
        previewData.total = res.data.count;
        previewData.isExport = res.data.is_export
      } else {
        previewData.chartCfg = res.data.config;
        previewData.filter = res.data.filter;
      }
    })
    .finally(() => {
      previewLoading.value = false;
    });
};

const addViewName = () => {
  nameVisible.value = true;
  previewName.value = null;
};

const toSaveView = () => {
  if (!previewName.value) {
    message.error("请输入名称");
    return;
  }
  saveView({
    modularId: modularId.value,
    fieldIds: fieldIds.value.toString(),
    previewName: previewName.value,
    showTypeId: showTypeId.value,
    xDataId: xDataId.value?.toString(),
    yDataId: yDataId.value?.toString(),
    isExport: isExport.value,
  }).then(() => {
    message.success("保存成功，可前往视图列表查看");
    nameVisible.value = false;
  });
};

const toFilt = () => {
  previewData.page = 1;
  toPreview({});
};
</script>

<style lang="less" scoped>
.normal-container {
  height: calc(100vh - 120px);
  padding: 16px;
}
.view-create-box {
  display: flex;
  height: 100%;
}
.left-box {
  width: 320px;
  flex-shrink: 0;
  height: calc(100% - 20px);
  padding: 10px;
  border-right: 1px solid #ddd;

  .footer {
    text-align: right;
    .preview-btn {
      margin-right: 10px;
    }
  }
}
.right-box {
  padding: 0 0 0 10px;
  flex-grow: 1;
  overflow: auto;
}
.preview-area {
  background-color: #f6f6f6;
  height: 100%;
  width: 100%;
  font-size: 20px;
  color: #999;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .anticon-bar-chart {
    font-size: 100px;
  }
}
.y-table-name {
  .title {
    font-size: 18px;
    font-weight: bold;
  }
}
.y-table-filter {
  display: flex;
  flex-wrap: wrap;
}
.filter-item {
  margin-right: 10px;
  margin-bottom: 6px;
  font-size: 14px;
}
.input-item {
  width: 180px;
}
.date-item {
  width: 240px;
}
.date-time-item {
  width: 300px;
}
.number_range_box {
  display: inline-flex;
  align-items: center;

  .divider {
    margin: 0 4px;
  }
}
.y-table-content {
  margin-top: 10px;
}
.pagination-box {
  text-align: center;
  margin-top: 10px;
}
</style>
