<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:8}" name="siteUserName">
            <a-input v-model:value="searchParams.siteUserName" placeholder=""/>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-form-item label="所属驿站" :label-col="{span:8}" name="siteID">
            <a-select
              ref="select"
              v-model:value="searchParams.siteID"
              :options="allSites"
              @change="loadData"
              :field-names="{ label: 'siteName', value: 'siteID' }"
              :allow-clear="true"
              style="width: 200px" >
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="6">

        </a-col>
      <a-col :span="6" style="text-align: left">
          <a-button type="primary" html-type="submit" @click="onSearch">查询</a-button>
          <a-button
          style="margin: 0 8px"
          @click="
              () => {
                formRef.resetFields();
                loadData();
              }
            ">重置</a-button>
        </a-col>
      </a-row>

      <a-row class="edit-operation">
        <a-col :span="24" style="text-align: right">
          <a-button type="primary" html-type="submit" functioncode="T01010302" @click='onAdd' >新增</a-button>
          <BExportExcel :title="'导出'" :filename="'驿站人员信息'" :url="'/userInfo/export'" :params="{...searchParams, rows:100000,siteUserIDList:formState.selectedRowKeys.join(',')}"></BExportExcel>
        </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.siteUserID"
               bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.key === 'operation'">
            <div class="table-operation">
              <a-button type="link" size="small"  functioncode="T01010303"  @click='onEdit(record.siteUserID)' >编辑</a-button>
              <a-button type="link" size="small"  functioncode="T01010304"  @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 {useTabsViewStore} from "@/store/modules/tabsView";
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 {getSiteUserList,delSiteUser} from '@/api/baseSettings/userInfo';
import BExportExcel from "@/components/basic/excel/exportExcel/exportExcel.vue";
import {getPaginationTotalTitle} from "@/utils/common";
import {message, Modal} from "ant-design-vue";
import {getSiteList} from "@/api/baseSettings/siteInfo";

export default defineComponent({
  name: 'UserInfoList',
  components: {DownOutlined, UpOutlined, BExportExcel},
  setup() {
    const formRef = ref<FormInstance>();
    const tabsViewStore = useTabsViewStore();
    const allSites = ref<any>([]);
    const searchParams = reactive({
      pageIndex: 1,
      pageSize: 20,
      siteUserName: '',
      siteID:''
    });
    const formState = reactive({
      total: 0,
      selectedRowKeys: [],
      loading: false
    });
    const columns: TableColumnsType = [
      {title: '序号', align: "center",key: 'siteUserID',customRender: item => `${searchParams.pageSize * (searchParams.pageIndex - 1) + item.index + 1}`},
      {title: '人员名称', dataIndex: 'siteUserName', key: 'siteUserName', align: "center"},
      {title: '用户类型', dataIndex: 'roleName', key: 'roleName',width:120, align: "center"},
      {title: '性别', dataIndex: 'genderName', key: 'genderName', align: "center"},
      {title: '联系电话', dataIndex: 'mobile', key: 'mobile', align: "center"},
      {title: '所属驿站', dataIndex: 'siteName', key: 'siteName', 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 getAllSites = () => {
      getSiteList(searchParams).then((result :any) => {
        allSites.value = result.list;
        console.log('allSites',allSites);
      })
    }
    const dataList = 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.siteUserID as never)
      }

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

      Modal.confirm({
        title: '确认删除选中的人员信息?',
        icon: createVNode(ExclamationCircleOutlined),
        content: '',
        okText: '确认删除',
        okType: 'danger',
        okButtonProps: {},
        cancelText: '取消',
        onOk() {
          delSiteUser(formState.selectedRowKeys).then(() => {
            loadData();
          });
        },
        onCancel() {

        },
      });
    };


    const loadData = async function () {
      formState.loading = true;
      await getAllSites();
      const result: any = await getSiteUserList(searchParams);
      dataList.value = result.list;
      formState.total = result.total;
      formState.loading = false;
    }

    const onAdd =()=>{
      tabsViewStore.addTabByPath('/baseSettings/user/add', {id:null,op:1});
    };

    const onEdit = (id: string) => {
      tabsViewStore.addTabByPath('/baseSettings/user/edit', {id:id,op:2});
    };

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