<template>
  <div class="item-select-dlg dialog-box">
    <el-form
      ref="selectCarDriverRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshSelectCarDriver()" @reset="resetSelectCarDriver">
        <template #operation>
          <el-button type="primary" @click="onSubmit" :size="layoutStore.defaultFormItemSize">
            确定
          </el-button>
        </template>
      </filter-box>
    </el-form>
    <table-box
      ref="tourCarDriver"
      class="main-box"
      :data="tourCarDriverWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourCarDriverWidgetCurrentPage - 1) * tourCarDriverWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="false"
      @sort-change="tourCarDriverWidget.onSortChange"
      @refresh="tourCarDriverWidget.refreshTable()"
      @radio-select-change="onSelectChange"
    >
      <vxe-column title="" type="radio" align="center" :width="80" />
      <vxe-column title="司机名称" field="name" />
      <vxe-column title="性别" field="sexDictMap.name" />
      <vxe-column title="年龄" field="age" />
      <vxe-column title="驾龄" field="drivingYears" />
      <vxe-column title="司机照片" min-width="100px">
        <template v-slot="scope">
          <upload-file-list
            :file-list="
              parseUploadData(scope.row.image, {
                id: scope.row.id,
                fieldName: 'image',
                asImage: true
              })
            "
            type="card"
            direction="horizontal"
            :readonly="true"
          />
        </template>
      </vxe-column>
      <vxe-column title="联系方式" field="mobile" />
      <vxe-column title="司机类型" field="typeDictMap.name" />
      <vxe-column title="所属区域" field="belongTabDictMap.name" />
      <vxe-column title="热度值" field="hotValue" />
      <vxe-column title="评分" field="mark" />
      <template slot="empty">
        <div class="table-empty unified-font">
          <img src="@/assets/img/empty.png">
          <span>暂无数据</span>
        </div>
      </template>
      <!-- 分页 -->
      <template #pagination>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            :total="tourCarDriverWidgetTotalCount"
            :current-page="tourCarDriverWidgetCurrentPage"
            :page-size="tourCarDriverWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourCarDriverWidget.onCurrentPageChange"
            @size-change="tourCarDriverWidget.onPageSizeChange">
          </el-pagination>
        </el-row>
      </template>
    </table-box>
  </div>
</template>

<script lang="ts">
export default {
  name: 'selectCarDriver',
};
</script>

<script setup lang="ts">
import { DialogProp } from '@/components/Dialog/types';
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode, stringCase } from '@/common/utils';
import { TourCarDriverData } from '@/api/generated/tourCarDriverController';
import { SysDeptData } from '@/api/generated/sysDeptController';
import { TourCarDriverController, SysDeptController } from '@/api/generated';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const pps = withDefaults(
  defineProps<{
    modelValue: TourCarDriverData[];
    subPage?: number | string | boolean;
    id?: ANY_OBJECT;
    props: {
      label: string;
      value: string;
    };
    // 当使用Dialog.show弹出组件时，须定义该prop属性，以便对dialog进行回调
    dialog?: DialogProp<ANY_OBJECT[]>;
  }>(),
  {
    modelValue: () => [],
    props: () => {
      return {
        label: 'name',
        value: 'id',
      };
    },
    dialog: undefined,
  },
);
// 选中数据
const dialogSelectItems = ref<TourCarDriverData>();

const onCancel = () => {
  if (pps.dialog) {
    pps.dialog.cancel();
  }
};

const onSubmit = () => {
  if (pps.dialog) {
    pps.dialog.submit(dialogSelectItems.value);
  }
};

/**
 * 重置过滤值
 */
const resetSelectCarDriver = () => {
  refreshSelectCarDriver();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetSelectCarDriver();
};
/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourCarDriverWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
  };
  return new Promise((resolve, reject) => {
    TourCarDriverController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
      nextTick(() => {
        setTableSelectRow();
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourCarDriverVerify = () => {
  return true;
};
// 表格组件表格组件参数
const tourCarDriverOptions: TableOptions<TourCarDriverData> = {
  loadTableData: loadTourCarDriverWidgetData,
  verifyTableParameter: loadTourCarDriverVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourCarDriver = ref();
const tourCarDriverWidget = useTable(tourCarDriverOptions);
const {
  dataList: tourCarDriverWidgetDataList,
  currentPage: tourCarDriverWidgetCurrentPage,
  pageSize: tourCarDriverWidgetPageSize,
  totalCount: tourCarDriverWidgetTotalCount,
} = tourCarDriverWidget;
const setTableSelectRow = () => {
  if (tourCarDriver.value == null || !Array.isArray(tourCarDriverWidget.dataList.value))
    return;
  setTimeout(() => {
    let selectRow = findItemFromList(
      tourCarDriverWidget.dataList.value,
      (dialogSelectItems.value || {})[pps.props.value],
      pps.props.value,
    );
    tourCarDriver.value.getTableImpl().setRadioRow(selectRow);
  }, 50);
};
const refreshSelectCarDriver = () => {
  // 重新获取数据组件的数据
  tourCarDriverWidget.refreshTable();
};
const onSelectChange = (row?: TourCarDriverData) => {
  dialogSelectItems.value = row;
};

const formInit = () => {
  refreshSelectCarDriver();
};

onMounted(() => {
  if (Array.isArray(pps.modelValue) && pps.modelValue.length > 0) {
    dialogSelectItems.value = {
      ...pps.modelValue[0],
    };
  }
  formInit();
});
</script>

<style scoped>
.main-box {
  flex-grow: 1;
}
</style>
