<template>
  <div class="p-2">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="3">
            <a-form-item label="电话号">
              <a-input placeholder="请输入电话号" v-model:value="queryParam.sim"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="3">
            <a-form-item label="设备号">
              <a-input placeholder="请输入设备号" v-model:value="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="4">
            <a-form-item label="供热公司">
              <a-select ref="select" placeholder="请选择供热公司" v-model:value="queryParam.companyCompanyId"
                style="width: 150px" @focus="focus" @change="handleChange1">
                <a-select-option :value="item.companyId" v-for="item in thermalcompany"
                  :key="item.id">{{ item.companyName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="4">
            <a-form-item label="锅炉房">
              <a-select ref="select" placeholder="请选择锅炉房" v-model:value="queryParam.sourceSourceId" style="width: 150px"
                @focus="focus" @change="handleChange2">
                <a-select-option :value="item.sourceId" v-for="item in heatsource"
                  :key="item.id">{{ item.sourceName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
<!--          <a-col :lg="5">-->
<!--            <a-form-item label="换热站">-->
<!--              <a-select ref="select" placeholder="请选择换热站" v-model:value="queryParam.stationStationId"-->
<!--                style="width: 250px" @focus="focus" @change="handleChange3">-->
<!--                <a-select-option :value="item.stationId" v-for="item in heatsourcestation"-->
<!--                  :key="item.id">{{ item.stationName }}</a-select-option>-->
<!--              </a-select>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :lg="5">
            <span style="overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
              <a-button preIcon="ant-design:sync-outlined" @click="searchReset" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'heating:bl_simconfig:add'" @click="handleAdd"
          preIcon="ant-design:plus-outlined"> 新增</a-button>
        <!--          <a-button  type="primary" v-auth="'heating:bl_simconfig:exportXls'" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button type="primary" v-auth="'heating:bl_simconfig:importExcel'" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>-->
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-auth="'heating:bl_simconfig:deleteBatch'">批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <SimconfigJxModal ref="registerModal" @success="handleSuccess"></SimconfigJxModal>
  </div>
</template>

<script lang="ts" name="heating-simconfig" setup>
import { ref, reactive, computed, unref, onMounted } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage'
import SimconfigJxModal from './components/SimconfigJxModal.vue'
import { columnsJx, searchFormSchema } from './Simconfig.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, companylist, heatsourcelist, heatsourcestationlist } from './Simconfig.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import { useUserStore } from '/@/store/modules/user';
const queryParam = ref<any>({});
const checkedKeys = ref<Array<string | number>>([]);
const userStore = useUserStore();
const registerModal = ref();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '设备信息管理',
    api: list,
    defSort: {
      column: '',
      order: '',
    },
    columns: columnsJx,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [
      ],
      fieldMapToTime: [
      ],
    },
    actionColumn: {
      width: 140,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      queryParam.value.regionType = '郊县'
      return Object.assign(params, queryParam.value);
    },
  },
  exportConfig: {
    name: "设备信息管理",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext
const labelCol = reactive({
  xs: { span: 24 },
  sm: { span: 7 },
});
const wrapperCol = reactive({
  xs: { span: 24 },
  sm: { span: 16 },
});
/**
 * 新增事件
 */
function handleAdd() {
  registerModal.value.disableSubmit = false;
  registerModal.value.add();
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  registerModal.value.disableSubmit = false;
  registerModal.value.edit(record);
}
/**
 * 详情
*/
function handleDetail(record: Recordable) {
  registerModal.value.disableSubmit = true;
  registerModal.value.edit(record);
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
   * 操作栏
   */
function getTableAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      auth: 'heating:bl_simconfig:edit'
    }, {
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
        placement: 'topLeft',
      },
      auth: 'heating:bl_simconfig:delete'
    }
  ]
}


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

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

const thermalcompany = ref();
async function getThermalcompany() {
  let params = {regionType:'郊县'};
  thermalcompany.value = await companylist(params);
}

const heatsource = ref();
async function getHeatsource() {
  let params = {regionType:'郊县'};
  heatsource.value = await heatsourcelist(params);
}

// const heatsourcestation = ref();
// async function getHeatsourcestation() {
//   heatsourcestation.value = await heatsourcestationlist();
// }

async function handleChange1(record) {
  let params = { companyCompanyId: record,regionType:'郊县' };
  heatsource.value = await heatsourcelist(params);
  // heatsourcestation.value = [];
  queryParam.value.sourceSourceId = '';
  queryParam.value.stationStationId = '';
}
async function handleChange2(record) {
//   let params = { sourceSourceId: record };
//   heatsourcestation.value = await heatsourcestationlist(params);
//   queryParam.value.stationStationId = '';
// }
// function handleChange3(record) {
}

onMounted(() => {
  getThermalcompany();
  getHeatsource();
  // getHeatsourcestation();
});

</script>

<style lang="less" scoped>
:deep(.ant-picker),
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.jeecg-basic-table-form-container) {
  padding: 0px;
}

:deep(.ant-form-item) {
  margin-bottom: 8px;
}
</style>
