<template>
  <Page>
    <ElForm v-show="showSearchForm" inline @submit.prevent="paging">
      <ElFormItem label="模板名称">
        <ElInput
          v-model="searchForm.tplName"
          placeholder="模板名称"/>
      </ElFormItem>
      <ElFormItem label="前端/后端">
        <ElSelect
          v-model="searchForm.lang"
          placeholder="前端/后端"
          style="width: 180px"
        >
          <ElOption
            label="前端"
            value="ts"/>
          <ElOption
            label="后端"
            value="java"/>
        </ElSelect>
      </ElFormItem>

      <ElFormItem>
        <ElButton :icon="elIcons.Search" :loading="searching" native-type="submit" type="primary">搜索</ElButton>
        <ElButton :icon="elIcons.Refresh" :loading="searching" @click="reset">重置</ElButton>
      </ElFormItem>
    </ElForm>
    <div class="tool-bar">
      <ElButton :icon="elIcons.Plus" type="primary" @click="addHandler">新建</ElButton>
      <ElButton :icon="elIcons.Filter" type="default" @click="showSearchForm = !showSearchForm"/>
    </div>
    <ElTable v-loading="searching" :data="tableData"
      cell-class-name="table-cell"
      class="table-list"
      empty-text="暂无数据"
      header-row-class-name="table-header"
      row-key="id">
      <ElTableColumn label="模板名称" prop="tplName"/>

      <ElTableColumn label="前端/后端" prop="lang">
        <template #default="scope">
          <span>{{ scope.row.lang === 'java' ? '后端' : '前端' }}</span>
        </template>
      </ElTableColumn>

      <ElTableColumn label="模板内容" prop="tpl">
        <template #default="{row}:{row:TplTypes.SearchTplResult}">
          <span>{{ (row.tpl != null && (row.tpl?.content?.length ?? 0) > 20) ? row.tpl!.content!.substring(0, 19) + '...' : row.tpl?.content }}</span>
        </template>
      </ElTableColumn>

      <ElTableColumn label="操作" width="180">
        <template #default="scope">
          <div class="action-btn">
            <el-popconfirm
              cancel-button-text="否"
              cancel-button-type="primary"
              confirm-button-text="是"
              confirm-button-type="danger"
              placement="top"
              title="是否删除当前数据?"
              width="180"
              @confirm="delHandler(scope)">
              <template #reference>
                <ElButton :loading="deling" text type="danger">删除</ElButton>
              </template>
            </el-popconfirm>
            <ElButton text type="primary" @click="modifyHandler(scope)">修改</ElButton>
          </div>
        </template>
      </ElTableColumn>
    </ElTable>
    <ElPagination
      v-model:current-page="searchForm.current"
      v-model:page-size="searchForm.size"
      :hide-on-single-page="false"
      :page-sizes="[10, 20, 50, 100, 500]"
      :teleported="false"
      :total="totalCount"
      layout="->, sizes, total, prev, pager, next"
      @change="paging"/>
    <TplForm ref="tplForm" @edit-succ="paging"/>
  </Page>
</template>
<script lang="ts" setup>
import TplApi from '@/pages/sys/gen/tpl/tpl-api.ts'
import Page from '@/components/page/Page.vue'
import { elIcons } from '@/common/element/element.ts'
import TplForm from '@/pages/sys/gen/tpl/TplForm.vue'

const totalCount = ref(0)
const tableData = ref<TplTypes.SearchTplResult[]>([])
const searchForm = ref<TplTypes.SearchTplParam>({
  current: 1,
  size: 20,
  orders: 'lang,tpl_name',
  tpl: {},
})
const searching = ref(false)
const deling = ref(false)
const showSearchForm = ref(true)
const tplFormIns = useTemplateRef<InstanceType<typeof TplForm>>('tplForm')

function showDialog(data?: TplTypes.SearchTplResult) {
  tplFormIns.value?.open(data)
}

function delHandler({row}: { row: TplTypes.SearchTplResult }) {
  deling.value = true
  TplApi.del([ row.id! ])
    .then(() => {
      ElMessage.success('删除成功')
      paging()
    })
    .finally(() => {
      deling.value = false
    })
}

function modifyHandler({row}: { row: TplTypes.SearchTplResult }) {
  showDialog(row)
}

function addHandler() {
  showDialog()
}

function reset() {
  searchForm.value = {
    current: 1,
    size: 20,
    orders: 'lang,tpl_name',
    tpl: {},
  }
  paging()
}

function paging() {
  searching.value = true
  TplApi.paging(searchForm.value)
    .then(res => {
      totalCount.value = res.data?.total ?? 0
      tableData.value = res.data?.records ?? []
    })
    .finally(() => {
      searching.value = false
    })
}

onMounted(() => {
  paging()
})
</script>
<style lang="stylus" scoped>
.table-list {
  flex 1;
  margin 0 0 20px 0
  width 100%

  :deep(.table-header) {
    color #454C59

    th {
      background-color #EDF1F7
      font-weight 500
      position relative

      & > div {
        display flex
        gap 5px
        align-items center
      }

      &:not(:first-child) > div::before {
        position: absolute;
        top: 50%;
        left: 1px;
        width: 1px;
        background-color: #D3D7DE;
        transform: translateY(-50%);
        content: "";
        height 50%
      }
    }
  }

  :deep(.table-cell) {
    color #2F3540
  }

  .action-btn {
    width 100%
    display flex
    flex-wrap wrap

    & > button {
      margin 0
    }
  }
}

.tool-bar {
  display flex
  justify-content space-between
  margin 0 0 20px 0
}

.pagination {
  justify-content: end;
  margin: 8px;
}
</style>
