<template>
  <Page>
    <ElForm v-show="showSearchForm" inline @submit.prevent="paging">
      <!-- <ElFormItem label="主体类型，GeTiHu 个体户、QiYe 企业">
        <ElInput
          v-model="searchForm.orgCategory"
          placeholder="主体类型，GeTiHu 个体户、QiYe 企业"/>
      </ElFormItem> -->
      <ElFormItem label="统一社会信用代码">
        <ElInput v-model="searchForm.uscc" placeholder="统一社会信用代码" />
      </ElFormItem>
      <ElFormItem label="组织名称">
        <ElInput v-model="searchForm.orgName" placeholder="组织名称" />
      </ElFormItem>
      <ElFormItem label="法人名称">
        <ElInput v-model="searchForm.legalRepresentative" placeholder="法人名称" />
      </ElFormItem>
      <ElFormItem label="市">
        <ElInput v-model="searchForm.cityName" placeholder="市" />
      </ElFormItem>
      <ElFormItem label="区县">
        <ElInput v-model="searchForm.areaName" placeholder="区县" />
      </ElFormItem>
      <ElFormItem label="乡镇街道">
        <ElInput v-model="searchForm.townName" placeholder="乡镇街道" />
      </ElFormItem>
      <ElFormItem label="详细地址">
        <ElInput v-model="searchForm.address" placeholder="详细地址" />
      </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">
      <div></div>
      <!-- <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">
      <!-- ，GeTiHu-个体户、QiYe--企业 -->
      <ElTableColumn label="主体类型" prop="orgCategoryTxt" />
      <!-- <ElTableColumn label="统一社会信用代码" prop="uscc" /> -->
      <ElTableColumn label="组织名称" prop="orgName"  width="140"/>
      <ElTableColumn label="营业执照" width="100px">
        <template #default="{ row }">
          <el-image :preview-src-list="[AppApi.fileUrl(row.businessLicense)]" :src="AppApi.fileUrl(row.businessLicense)" preview-teleported show-progress style="width: 60px; height: 60px" />
        </template>
      </ElTableColumn>
      <ElTableColumn label="法人名称" prop="legalRepresentative" />
      <!-- <ElTableColumn label="法人身份证号" prop="idcard" />
       <ElTableColumn label="身份证有效期" prop="idcardStartTime"/>
        <ElTableColumn label="身份证有效期" prop="idcardEndTime"/> -->
      <ElTableColumn label="身份证正面" prop="idcardFront" width="100">
        <template #default="{ row }">
          <el-image :preview-src-list="[AppApi.fileUrl(row.idcardFront)]" :src="AppApi.fileUrl(row.idcardFront)" preview-teleported show-progress style="width: 60px; height: 60px" />
        </template>
      </ElTableColumn>
      <ElTableColumn label="身份证反面" prop="idcardBack" width="100">
        <template #default="{ row }">
          <el-image :preview-src-list="[AppApi.fileUrl(row.idcardBack)]" :src="AppApi.fileUrl(row.idcardBack)" preview-teleported show-progress style="width: 60px; height: 60px" />
        </template>
      </ElTableColumn>
      <!-- <ElTableColumn label="省" prop="provinceName"/>
        <ElTableColumn label="市" prop="cityName"/> -->
      <ElTableColumn label="区县" prop="areaName" />
      <ElTableColumn label="乡镇街道" prop="townName" />
      <ElTableColumn label="详细地址" prop="address" width="180" />
      <!-- <ElTableColumn label="经度" prop="lng" />
      <ElTableColumn label="纬度" prop="lat" /> -->
      <ElTableColumn label="创建时间" prop="createTime" width="140" />
      <ElTableColumn label="修改时间" prop="modifyTime" width="140" />
      <ElTableColumn label="操作" width="180" fixed="right">
        <template #default="scope">
          <div class="action-btn">
            <ElPopconfirm confirm-button-text="是" cancel-button-text="否" confirm-button-type="danger" cancel-button-type="primary" placement="top" title="是否删除当前数据?" width="180" @confirm="delHandler(scope)">
              <template #reference>
                <ElButton text type="danger" :loading="deling">删除</ElButton>
              </template>
            </ElPopconfirm>
            <!-- <ElButton text type="primary" @click="modifyHandler(scope)">修改</ElButton> -->
          </div>
        </template>
      </ElTableColumn>
    </ElTable>
    <ElPagination layout="->, sizes, total, prev, pager, next" v-model:current-page="searchForm.current" v-model:page-size="searchForm.size" :total="totalCount" :page-sizes="[10, 20, 50, 100, 500]" :hide-on-single-page="false" :teleported="false" @change="paging" />
    <OrgForm ref="orgForm" @edit-succ="paging" />
  </Page>
</template>

<script lang="ts" setup>
import OrgApi from "@/pages/cst/org/org-api.ts";
import OrgForm from "@/pages/cst/org/OrgForm.vue";
import Page from "@/components/page/Page.vue";
import { elIcons } from "@/common/element/element.ts";
import Utils from "@/common/utils";
import AppApi from "@/common/app/app-api.ts";

const totalCount = ref(0);
const tableData = Utils.resetAble(reactive<OrgTypes.SearchOrgResult[]>([]));
const searchForm = Utils.resetAble(
  reactive<OrgTypes.SearchOrgParam>({
    current: 1,
    size: 20,
  })
);
const searching = ref(false);
const deling = ref(false);
const showSearchForm = ref(true);
// const orgFormIns = useTemplateRef<InstanceType<typeof OrgForm>>("orgForm");

// function showDialog(data?: OrgTypes.SearchOrgResult) {
//   orgFormIns.value?.open(data);
// }

function delHandler({ row }: { row: OrgTypes.SearchOrgResult }) {
  deling.value = true;
  OrgApi.del([row.id!])
    .then(() => {
      ElMessage.success("删除成功");
      paging();
    })
    .finally(() => {
      deling.value = false;
    });
}

// function modifyHandler({ row }: { row: OrgTypes.SearchOrgResult }) {
//   showDialog(row);
// }

// function addHandler() {
//   showDialog();
// }

function reset() {
  searchForm.$reset();
  paging();
}

function paging() {
  searching.value = true;
  OrgApi.paging(searchForm)
    .then((res) => {
      totalCount.value = res.data?.total ?? 0;
      tableData.$reset(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
}
</style>
