<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: var(--el-color-white);
  height: 24px;
}
</style>
<template>
  <div class="system-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <ext-query-form
          class="page-search"
          ref="queryRef"
          v-model="state.formQuery"
          :columns="state.columns"
          @on-change="loadData(true)">
        <!--  <template #extraQuery></template>
          <template #extraLeft></template>
         -->
        <template #extQuery>
          <el-button  v-auth="'washStation.add'"   size="default" plain  type="success" class="ml10" @click="handleRowClick('add',null)">
            <SvgIcon name="ele-FolderAdd"/>
            新增
          </el-button></template>
      </ext-query-form>

      <el-table
          border
          stripe="stripe"
          :height="state.tableData.height"
          highlight-current-row
          current-row-key="id"
          row-key="id"
          @on-row-click="handleRowClick('view',$event)"
          :data="state.tableData.data"
          v-loading="state.tableData.loading">
        <template #empty>
          <el-empty></el-empty>
        </template>
        <el-table-column
            v-for="field in state.columns"
            :key="field.prop"
            :type="field.type"
            :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==='stationStatus'">
              <ext-d-label type="WashStation.status" :model-value="row[field.prop]"></ext-d-label>
            </template>
            <template v-else-if="field.prop==='stationType'">
              <ext-d-label type="WashStation.type" :model-value="row[field.prop]"></ext-d-label>
            </template>
            <template v-else-if="field.prop==='stationName'">
              <div class="text-align-center">
                {{ row.stationId }}
                <hr>
                {{ row.stationName }}
              </div>
            </template>
            <template v-else-if="field.prop==='pictures'">
              <ext-image :srcList="row[field.prop]"></ext-image>
            </template>
            <template v-else-if="field.prop==='createTime'">
              {{ u.fmt.fmtDateTime(row[field.prop]) }}
            </template>
            <template v-else-if="field.prop==='updateTime'">
              {{ u.fmt.fmtDateTime(row[field.prop]) }}
            </template>
            <template v-else-if="field.prop==='parkingQrCode'">
              <Qrcode :link="row[field.prop]" text="停车二维码"></Qrcode>
            </template>
            <template v-else-if="field.prop==='action'">
              <el-button  v-auth="'washStation.modify'" size="small" plain type="warning" @click="handleRowClick('edit',row)">编辑</el-button>
              <el-button  v-auth="'washStation.modify'" size="small" plain type="danger" @click="handleRowDelete(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:value="state.pageQuery" @change="loadData(false)"/>
    </el-card>
  </div>
  <WashStationDialog ref="washStationDialogRef" @refresh="loadData(true)"/>
</template>

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

const {proxy}: any = getCurrentInstance();

import ExtPage from '/@/components/form/ExtPage.vue'
import ExtQueryForm from "/@/components/form/ExtQueryForm.vue";


import {ElButton} from 'element-plus'
import ExtDLabel from "/@/components/form/ExtDLabel.vue";
import ExtImage from "/@/components/form/ExtImage.vue";
import Qrcode from "/@/components/qrcode/index.vue";


const WashStationDialog = defineAsyncComponent(() => import("/@/views/admin/station/list/dialog.vue"));

//定义引用
const queryRef = ref();
const washStationDialogRef = ref();

//定义变量

const state = reactive({
  formQuery: {},
  pageQuery: {
    pageNum: 1,
    pageSize: 10,
    total: 0
  },
  tableData: {
    height: 500,
    data: [] as Array<any>,
    loading: false
  },
  importConfig: {},
  exportConfig: {},
  columns: [
    // {type: 'selection', width: 60, align: 'center', fixed: 'left'},
    {label: '站点名称', prop: 'stationName', query: true, type: 'text', resizable: true},
    {label: '站点照片', prop: 'pictures', query: false, type: 'text', resizable: true},
    {label: '站点状态', prop: 'stationStatus', query: true, type: 'dict', conf:{dict:'WashStation.status'},resizable: true},
    {label: '站点类型', prop: 'stationType', query: true, type: 'dict',conf:{dict:'WashStation.type'}, resizable: true},
    {label: '运营主体', prop: 'operationEntity', query: true, type: 'text', resizable: true},
    {label: '信用代码', prop: 'creditCode', query: true, type: 'text', resizable: true},
    {label: '场站联系人', prop: 'contactPerson', query: true, type: 'text', resizable: true},
    {label: '地址', prop: 'address', query: true, type: 'text', resizable: true},
    {label: '服务电话', prop: 'serviceTel', query: true, type: 'text', resizable: true},
    {label: '营业时间', prop: 'businessHours', query: false, type: 'text', resizable: true},
    {label: '工位数量', prop: 'parkingNum', query: false, type: '', resizable: true},
    {label: '停车费用', prop: 'parkingFee', query: false, type: '', resizable: true},
    {label: '停车减免二维码', width:200,prop: 'parkingQrCode', query: false, type: '', resizable: true},
    {label: '创建时间 ', prop: 'createTime', query: false, sortable: 'custom', type: 'datetime', resizable: true, conf: {format: (val: any) => u.fmt.fmtDate(val)}},
    {label: '更新时间', prop: 'updateTime', query: false, sortable: 'custom', type: 'datetime', resizable: true, conf: {format: (val: any) => u.fmt.fmtDate(val)}},
    {
      label: '操作', prop: 'action', type: 'render', width: 180, align: 'center', fixed: 'right',
    }
  ],
})


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

//生命周期钩子

onMounted(() => {
  loadData();

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

});


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

// 打开详情页弹窗
const handleRowClick = (type: string, row: any) => {
  washStationDialogRef.value.open(type, row);
};
// 删除点击
const handleRowDelete = (row: any) => {
  Msg.confirm(`此操作将永久删除：『${row.name}』，是否继续?`).then(() => {
    $get(`/washStation/delete/${row.id}`).then(() => {
      Msg.message("删除成功", 'success')
    }).catch(() => {
      Msg.message("删除失败", 'error')
    })
  });
};

//endregion


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