<template>
  <div class="card-search">
    <a-form ref="formRef" name="advanced_search"  class="ant-advanced-search-form" :model="searchParams" >
      <a-row :gutter="24">
        <a-col :span="6">
          <a-form-item label="驿站编号" :label-col="{span:6}" name="SiteCode">
            <a-input v-model:value="searchParams.siteCode" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="7">
          <a-form-item label="站点名称" :label-col="{span:7}" name="SiteName">
            <a-input v-model:value="searchParams.siteName" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="所属机构" :label-col="{span:6}" name="InstitutionName">
            <a-input v-model:value="searchParams.institutionName" placeholder=""/>
          </a-form-item>
        </a-col>
      <a-col :span="5" style="text-align: right">
          <a-button type="primary" html-type="submit" @click="onSearch">查询</a-button>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="6">
          <a-form-item label="所属县区" :label-col="{span:6}" name="RegionName">
            <a-input v-model:value="searchParams.regionName" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="7">
          <a-form-item label="所属街道" :label-col="{span:7}" name="StreetName">
            <a-input v-model:value="searchParams.streetName" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
        </a-col>
        <a-col :span="5" style="text-align: right">
        </a-col>
      </a-row>
      <a-row class="edit-operation">
        <a-col :span="24" style="text-align: right">
          <a-button type="primary" html-type="submit" @click='onOperates(null,"新增",false)' >新增</a-button>
          <a-button type="primary" >导出</a-button>
        </a-col>
      </a-row>
    </a-form>
    <div class="search-result-list">
      <a-table :columns="columns" :data-source="dataList" :scroll="{ x:'100%', y: 500 }" :pagination="pagination"
               :loading="formState.loading"
               @change="handleTableChange"
               :row-selection="{ selectedRowKeys: formState.selectedRowKeys, onChange: onSelectChange}"
               :row-key="record=>record.szkey"
               bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.key === 'operation'">
            <div class="table-operation">
              <a-button type="link" size="small" @click='onOperates(record.szkey.toString(),"修改",false)' >编辑</a-button>
              <a-button type="link" size="small" @click="onDel(record)" >删除</a-button>
            </div>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts">
import {reactive, ref, computed, defineComponent, createVNode} from 'vue';
import {DownOutlined, ExclamationCircleOutlined, UpOutlined} from '@ant-design/icons-vue';
import type {FormInstance} from 'ant-design-vue';
import type {TableColumnsType, TableProps} from 'ant-design-vue';
import {getList} from '@/api/baseSettings/siteInfo';
import BExportExcel from "@/components/basic/excel/exportExcel/exportExcel.vue";
import {getPaginationTotalTitle} from "@/utils/common";
import {message, Modal} from "ant-design-vue";

export default defineComponent({
  name: 'SiteList',
  components: {DownOutlined, UpOutlined, BExportExcel},
  setup() {
    const formRef = ref<FormInstance>();
    const searchParams = reactive({
      pageIndex: 1,
      pageSize: 20,
      primaryKey:'',
      siteCode: '',
      siteName: '',
      institutionName:'',
      regionName:'',
      streetName:'',
    });
    const formState = reactive({
      total: 0,
      selectedRowKeys: [],
      loading: false
    });
    const columns: TableColumnsType = [
      {title: '序号', align: "center",key: 'szkey',customRender: item => `${searchParams.pageSize * (searchParams.pageIndex - 1) + item.index + 1}`},
      {title: '驿站编号', dataIndex: 'szcode', key: 'szcode', align: "center"},
      {title: '站点名称', dataIndex: 'szname', key: 'szname',width:120, align: "center"},
      {title: '站点负责人', dataIndex: 'szusername', key: 'szusername', align: "center"},
      {title: '联系电话', dataIndex: 'szusermobile', key: 'szusermobile', align: "center"},
      {title: '所属县区', dataIndex: 'szregion', key: 'szregion', align: "center"},
      {title: '所属街道', dataIndex: 'szstreet', key: 'szstreet', align: "center"},
      {title: '详细地址', dataIndex: 'szaddress', key: 'szaddress', align: "center"},
      {title: '所属机构', dataIndex: 'CompanyName', key: 'CompanyName', align: "center"},
      {title: '驿站人员数量', key: 'UserNumber', fixed: 'right',width:170, align: "center"},
      {title: '操作', key: 'operation', fixed: 'right',width:170, align: "center"},
    ];
    const pagination = computed(() => ({
      total: formState.total,
      current: searchParams.pageIndex,
      pageSize: searchParams.pageSize,
      showSizeChanger: true,
      showTotal: total => getPaginationTotalTitle(total)
    }));

    const dataList = ref([]);
    const SiteEditModelRef = ref();

    const handleTableChange: TableProps['onChange'] = (pag: { pageSize: number; current: number },) => {
      searchParams.pageIndex = pag.current;
      searchParams.pageSize = pag.pageSize;
      loadData();
    };

    const onSelectChange = (selectedRowKeys: any) => {
      formState.selectedRowKeys = selectedRowKeys;
    };

    const onSearch = () => {
      loadData();
    }

    const onDel= (item: any) => {
      if (item) {
        formState.selectedRowKeys.push(item.szkey as never)
      }

      if (formState.selectedRowKeys.length <= 0) {
        message.warning('请选择需要删除的数据！');
        return false;
      }

      Modal.confirm({
        title: '确认删除选中的驿站站点?',
        icon: createVNode(ExclamationCircleOutlined),
        content: '',
        okText: '确认删除',
        okType: 'danger',
        okButtonProps: {},
        cancelText: '取消',
        onOk() {

        },
        onCancel() {

        },
      });
    };

    const onOperates = (id: string,operation:string,isDetail:boolean) => {
      SiteEditModelRef.value.show(id,operation,isDetail);
    };
    const loadData = async function () {
      formState.loading = true;
      const result: any = await getList(searchParams);
      dataList.value = result.list;
      formState.total = result.total;
      formState.loading = false;
    }



    return {
      formRef,
      searchParams,
      formState,
      columns,
      pagination,
      dataList,
      SiteEditModelRef,
      handleTableChange,
      onSelectChange,
      onSearch,
      onOperates,
      onDel,
      loadData
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    if (history.state.params?.reload)
      this.loadData();
  }
});
</script>
