<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">
      <ext-query-form
          class="page-search"
          ref="queryRef"
          v-model="state.formQuery"
          :columns="state.columns"
          :import-config="state.importConfig"
          :export-config="state.exportConfig"
          @on-change="loadData(true)"
          @imported="loadData(true)">
        <!--  <template #extraQuery></template>
          <template #extraLeft></template>
          <template #extraRight></template>-->
      </ext-query-form>

      <ext-table
          class="page-content"
          :height="state.tableData.height"
          :data-list="state.tableData.data"
          :columns="state.columns"
          :border="true"
          :loading="state.tableData.loading">
      </ext-table>

      <ext-page class="page-pager" v-model:value="state.pageQuery" @change="loadData(false)"/>
    </el-card>
  </div>
  <ConfigDialog ref="configDialogRef" @refresh="loadData(true)"/>
</template>

<script setup lang="ts" name="ConfigList">
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 ExtTable from "/@/components/form/ExtTable.vue";

import mittBus from '/@/utils/mitt';

import {ElButton} from 'element-plus'


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

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

//定义变量
const state = reactive({
  formQuery: {},
  pageQuery: {
    pageIndex: 1,
    pageSize: 10,
    total: 0
  },
  tableData: {
    height: 500,
    data: [] as Array<any>,
    loading: false
  },
  importConfig: {},
  exportConfig: {},
  columns: [
    {label: '字段', prop: 'code', query: true, type: 'text', resizable: true},
    {label: '值', prop: 'value', query: true, type: 'text', resizable: true},
    {label: '状态', prop: 'status', sortable: 'custom', align: 'center', query: true, type: 'dict', conf: {dict: 'Common.status'}},
    {label: '描述', prop: 'remark', query: true, type: 'text', resizable: true},
    {label: '更新时间', prop: 'updateAt', query: true, 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',
      render: (h: any, row: any) => {
        return (
            h('div', null, [
              proxy.$auth('config.modify') ?
                  h(ElButton, {
                    type: 'warning',
                    text: true,
                    size: 'small',
                    onClick: () => {
                      onRowClick('edit', row)
                    }
                  }, () => '编辑') : '',
              proxy.$auth('config.remove') ?
                  h(ElButton, {
                    type: 'danger',
                    text: true,
                    size: 'small',
                    onClick: () => {
                      console.error("delxxxx", row)
                      onRowDel(row)
                    }
                  }, () => '删除') : '',
            ])
        )
      }
    }
  ],
})


// 监听双向绑定 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("config.refresh", () => {
    loadData();
  })
});

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


//region 方法区
// 初始化表格数据
const loadData = (refresh: boolean = false) => {
  if (refresh) {
    state.pageQuery.pageIndex = 1;
  }
  state.tableData.loading = true;
  $body(`/config/list`, {...state.formQuery, ...state.pageQuery}).then((res: any) => {
    let {list, count} = 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) => {
  configDialogRef.value.open(type, row);
};
// 删除用户
const onRowDel = (row: any) => {
  Msg.confirm(`此操作将永久删除：『${row.name}』，是否继续?`).then(() => {
    $get(`/config/delete/${row.id}`).then(() => {
      Msg.message("删除成功", 'success')
    }).catch(() => {
      Msg.message("删除失败", 'error')
    })
  });
};

//endregion


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