<style scoped lang="scss">
.system-container {

  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }

  }
}

.page-content {
  margin-bottom: 20px;
}

.page-pager {
  background-color: #fff;
  height: 24px;
}
</style>
<template>
  <div class="system-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">


      <el-form
          :model="state.formQuery"
          ref="queryRef"
          size="default" label-width="0px" class="mt5 mb5">

        <ext-select
            v-model="state.formQuery.stationId"
            placeholder="站点"
            clearable
            url="station/listStation"
            urlMethod="get"
            data-key=""
            label-key="stationName"
            value-key="stationId"
            @on-change="loadData(true)"
            class="wd150 mr10">
        </ext-select>

        <el-button v-auth="'station.list'"    class="ml10"  plain size="default" type="success" @click="loadData(true)">
          <SvgIcon name="ele-Search"/>
          查询
        </el-button>

<!--        <el-button  v-auth="'station.add'"   size="default" plain  type="success" class="ml10" @click="onRowClick('add',null)">-->
<!--          <SvgIcon name="ele-FolderAdd"/>-->
<!--          新增-->
<!--        </el-button>-->

        <el-button class="ml10" plain size="default" type="success" @click="handleUploadVisible">
          <SvgIcon name="ele-Upload"/>
          导入站点
        </el-button>

      </el-form>

<!--      <el-card class="w100">
        <div class="order-summary">
          <span class="ml5">电量:</span>
          <el-tag type="success">{{state.extraData.totalPower}}Kwh</el-tag>
          <span class="ml5">实付服务费:</span>
          <el-tag type="success">{{u.fmt.fmtMoney(state.extraData.serviceMoney)}}元</el-tag>
        </div>

      </el-card>-->

      <el-table
          border
          stripe="stripe"
          :height="state.tableData.height"
          highlight-current-row
          current-row-key="id"
          row-key="id"
          :data="state.tableData.data"
          v-loading="state.tableData.loading"
          @selection-change="handleTableSelectionChange"
          @sort-change="handleTableSortChange">
        <template #empty>
          <el-empty></el-empty>
        </template>
        <el-table-column
            v-for="field in state.tableData.columns"
            :key="field.prop"
            :label="field.label"
            :column-key="field.prop"
            :width="field.width"
            :min-width="field.minWidth"
            :fixed="field.fixed"
            :sortable="field.sortable"
            :show-overflow-tooltip="!field.fixed&&field.width>150"
        >
          <template #default="{row}">
            <template v-if="field.prop==='pictures'">
              <ext-image :src-list="row[field.prop]?.split(';')" ></ext-image>
            </template>
            <template v-else-if="'stationType'===field.prop">
              <ext-d-label type="Station.type" v-model="row[field.prop]"/>
            </template>
            <template v-else-if="'stationStatus'===field.prop">
              <ext-d-label type="Station.status" v-model="row[field.prop]"/>
            </template>
            <template v-else-if="'construction'===field.prop">
              <ext-d-label type="Station.construction" v-model="row[field.prop]"/>
            </template>
            <template v-else-if="'action'===field.prop">
             <el-button link type="primary" @click="handleGotoEndpoint(row)">查看电桩</el-button>
              <el-button  size="small" plain type="warning" @click="onRowClick('edit',row)">编辑</el-button>
            </template>
            <template v-else>
              <div>{{row[field.prop]}}</div>
            </template>

          </template>
        </el-table-column>
      </el-table>

      <ext-page class="page-pager" v-model="state.pageQuery" @change="loadData(false)"/>
    </el-card>
  </div>
  <StationDialog ref="stationDialogRef" @refresh="loadData(true)"/>
  <SettleDialog ref="settleDialogRef" />
  <upload ref="upload_ref"></upload>
</template>

<script setup lang="ts" name="StationList">
import {defineAsyncComponent, reactive, onMounted, onBeforeMount, ref, getCurrentInstance, nextTick, onBeforeUnmount} from 'vue';
import {$body,$get} from "/@/utils/request";
import {Msg} from "/@/utils/message";
import u from "/@/utils/u"
import ExtPage from '/@/components/form/ExtPage.vue'

import mittBus from '/@/utils/mitt';
import ExtDLabel from "/@/components/form/ExtDLabel.vue";
import {useRouter} from "vue-router";
import ExtImage from "/@/components/form/ExtImage.vue";
import ExtSelect from "/@/components/form/ExtSelect.vue";
const router = useRouter();
const StationDialog = defineAsyncComponent(() => import("/@/views/admin/station/list/dialog.vue"));
const Upload = defineAsyncComponent(() => import("/@/views/admin/station/list/upload.vue"));


//定义引用
const queryRef = ref();
const stationDialogRef = ref();
const settleDialogRef = ref();
const upload_ref = ref();

//定义变量
const state = reactive({
  formQuery: {},
  pageQuery: {
    pageNum: 1,
    pageSize: 10,
    total: 0
  },
  tableData: {
    height: 500,
    data: [] as Array < any >,
    loading: false,
    columns: [
      {label: '充电站ID', prop: 'stationId',width:160, resizable: true,fixed:'left'},
      // {label: 'en+运营商id', prop: 'operatorId', resizable: true},
      // {label: '所属运营平台', prop: 'equipmentOwnerId', width:160,resizable: true,fixed:'left'},
      {label: '站点名称', prop: 'stationName',width:160, resizable: true},
      {label: '站点照片', prop: 'pictures',width:120, resizable: true},
      // {label: '充电中国家代码：CN', prop: 'countryCode', resizable: true},
      // {label: '充电站省市辖区编码', prop: 'areaCode', resizable: true},
      {label: '地址', prop: 'address',width:160, resizable: true},
      {label: '站点电话', prop: 'stationTel', width:120,resizable: true},
      {label: '服务电话', prop: 'serviceTel', width:140, resizable: true},
      {label: '站点类型', prop: 'stationType', width:120,resizable: true},
      {label: '站点状态', prop: 'stationStatus', width:120,resizable: true},
      {label: '充电车位数量', prop: 'parkingNum',width:160, resizable: true},
      // {label: '充电桩位置坐标', prop: 'location', resizable: true},
      // {label: '站点引导', prop: 'siteGuide', resizable: true},
      {label: '建设场所', prop: 'construction',  width:160, resizable: true},
      {label: '使用车型描述', prop: 'matchCars', width:160,resizable: true},
      {label: '车位楼层及数量描述', prop: 'parkInfo',width:160, resizable: true},
      {label: '营业时间描述', prop: 'businessHours', width:160,resizable: true},
      {label: '充电费描述', prop: 'electricityFee', width:160, resizable: true},
      {label: '服务费率描述', prop: 'serviceFee',  width:120,resizable: true},
      {label: '停车费', prop: 'parkFee', width:120, resizable: true},
      {label: '支付方式', prop: 'payment',  width:120,resizable: true},
      {label: '是否支持预约', prop: 'supportOrder',  width:120,resizable: true},
      {label: '备注', prop: 'remark', width:160,resizable: true},
      {
        label: '操作', prop: 'action', width: 180, align: 'center', fixed: 'right',
      }
    ],
  },
  extraData:{
    totalOrders:0,
    totalPower:0,
    elecMoney:0,
    serviceMoney:0,
    totalMoney:0,
  }
})


// 监听双向绑定 modelValue 的变化
// watch(
//         () => state.pageIndex,
//         () => {
//
//         }
// );

//生命周期钩子
onBeforeMount(() => {
})

onMounted(() => {
  loadData();

  nextTick(() => {
    let bodyHeight = document.body.clientHeight;
    let queryHeight = queryRef.value.$el.clientHeight;
    state.tableData.height = bodyHeight - queryHeight - 320
  })

  mittBus.on("station.refresh", () => {
    loadData();
  })
});

onBeforeUnmount(() => {
  mittBus.off("station.refresh")
})




//region 方法区
const handleGotoEndpoint = (row:any)=>{
  let {stationId} = row;
  if(!stationId){
    return;
  }
  let resolve = router.resolve(`/station/endpoint/${stationId}`);
  window.open(resolve.href, '_blank');
}


// 初始化表格数据
const loadData = (refresh: boolean = false) => {
  if (refresh) {
    state.pageQuery.pageNum = 1;
  }
  state.tableData.loading = true;
  $get(`/station/listStation`, {...state.formQuery, ...state.pageQuery}).then((res: any) => {
    let list= res;
    state.tableData.data = list;
    // state.pageQuery.total = count;
    state.tableData.loading = false;
  }).catch(e => {
    console.error(e)
    state.tableData.loading = false;
  })
};

// 打开修改用户弹窗
const onRowClick = (type: string, row: any) => {
  stationDialogRef.value?.open(type, row);
};

// 删除用户
const onRowDel = (row: any) => {
  Msg.confirm(`此操作将永久删除：『${row.name}』，是否继续?`).then(() => {
    $get(`/station/delete/${row.id}`).then(() => {
      Msg.message("删除成功", 'success')
    }).catch(() => {
      Msg.message("删除失败", 'error')
    })
  });
};

const handleTableSelectionChange = (selection: any) => {
  console.log("handleTableSelectionChange>>", selection)
  // emit("on-check-change", selection)
}

const handleTableSortChange = (column, prop, order) => {
  console.log("handleTableSortChange>>", column, prop, order)
  // emit("on-sort-change", column)
}

const handleSettleShow = () => {
  settleDialogRef.value?.open();
}

const handleUploadVisible = () => {
  upload_ref.value.open();
}


//endregion


// 暴露变量
// defineExpose({
//     loadData,
// });
</script>