<template>
  <div>
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol"
        :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col  :xl="8">
            <a-form-item name="orgCode">
              <template #label><span title="机构名称">机构名称</span></template>
              <j-dict-select-tag placeholder="请选择机构名称" v-model:value="queryParam.orgCode"
                dictCode="sys_depart,depart_name,org_code" allow-clear />
            </a-form-item>
          </a-col>
          <a-col  :xl="8">
            <a-form-item name="createTime">
              <template #label><span title="操作时间">操作时间</span></template>
              <a-range-picker value-format="YYYY-MM-DD" v-model:value="queryParam.createTime"
                class="query-group-cust" />
            </a-form-item>
          </a-col>
          <a-col  :xl="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
                <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset"
                  style="margin-left: 8px">重置</a-button>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 详情 -->
    <a-drawer v-model:visible="bkDetailListOpen" title="详情" width="1200" :footer-style="{ textAlign: 'right' }"
      :bodyStyle="{ padding: '14px', height: '80vh', display: 'flex', flexDirection: 'column', overflow: 'auto' }"
      wrapClassName="org-list-modal" @cancel="handleBkDetailListClose">
      <template #footer>
        <a-button @click="handleBkDetailListClose" type="primary">关闭</a-button>
      </template>
      <div style="padding:0px 8px;">
        <DirectiveBkItemList ref="bkDetailListRef" :bkOrgCode="bkOrgCode" :pkid="pkid"></DirectiveBkItemList>
      </div>
    </a-drawer>
  </div>
</template>

<script lang="ts" name="directivebk-directiveBkMain" setup>
import { ref, reactive, nextTick } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { columns, superQuerySchema } from './DirectiveBkMain.data';
import { list, deleteOne, batchDelete, getImportUrl } from './DirectiveBkMain.api';
import { getExportUrl } from './DirectiveBkItem.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { useUserStore } from '/@/store/modules/user';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
import { cloneDeep } from "lodash-es";
import DirectiveBkItemList from './DirectiveBkItemList.vue'

const formRef = ref();
const queryParam = reactive<any>({});
const bkDetailListOpen = ref(false)
const bkDetailListRef = ref()
const toggleSearchStatus = ref<boolean>(false);
const userStore = useUserStore();
const bkOrgCode = ref('')
const pkid = ref('')
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '指令快照主表',
    api: list,
    columns,
    canResize: false,
    useSearchForm: false,
    scroll: { y: '58vh' },
    actionColumn: {
      width: 120,
      fixed: 'right',
    },
    pagination: {
      current: 1,
      pageSize: 15,
      pageSizeOptions: ['15', '50', '70', '100'],
    },
    beforeFetch: async (params) => {
      let rangerQuery = await setRangeQuery();
      return Object.assign(params, rangerQuery);
    },
  },
  exportConfig: {
    name: "服务指令快照",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
});
const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;
const labelCol = reactive({
  xs: 24,
  sm: 4,
  xl: 8,
  xxl: 8
});
const wrapperCol = reactive({
  xs: 24,
  sm: 16,
});

/**
 * 详情
 */
function handleDetail(record: Recordable) {
  bkOrgCode.value = record.orgCode
  pkid.value = record.id
  bkDetailListOpen.value = true
  nextTick(() => {
    bkDetailListRef.value.init()
  })
}

/**
 * 关闭详情
 */
function handleBkDetailListClose() {
  bkDetailListOpen.value = false
}

/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}

function exportXls(record) {
  queryParam.exportPkId = record.id
  queryParam.createTime_ = record.createTime
  onExportXls()
}

/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '导出',
      onClick: exportXls.bind(null, record)
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record) {
  return [
  ]
}

/**
 * 查询
 */
function searchQuery() {
  reload();
}

/**
 * 重置
 */
function searchReset() {
  formRef.value.resetFields();
  selectedRowKeys.value = [];
  //刷新数据
  reload();
}





let rangeField = 'createTime,'

/**
 * 设置范围查询条件
 */
async function setRangeQuery() {
  let queryParamClone = cloneDeep(queryParam);
  if (rangeField) {
    let fieldsValue = rangeField.split(',');
    fieldsValue.forEach(item => {
      if (queryParamClone[item]) {
        let range = queryParamClone[item];
        queryParamClone[item + '_begin'] = range[0];
        queryParamClone[item + '_end'] = range[1];
        delete queryParamClone[item];
      } else {
        queryParamClone[item + '_begin'] = '';
        queryParamClone[item + '_end'] = '';
      }
    })
  }
  return queryParamClone;
}
</script>

<style lang="less" scoped>
.jeecg-basic-table-form-container {
  padding: 0;
  margin-bottom: 14px;

  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 14px;
    white-space: nowrap;
  }

  .query-group-cust {
    min-width: 100px !important;
  }

  .query-group-split-cust {
    width: 30px;
    display: inline-block;
    text-align: center
  }

  .ant-form-item:not(.ant-form-item-with-help) {
    margin-bottom: 14px;
    height: 32px;
  }

  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
}
</style>
