<script setup lang="ts">
import { useColumns } from "./columns";
import { httpList } from "/@/api/InvoiceSales/invoiceApply";
import { reactive, ref, onMounted } from "vue";
import { TableProBar } from "/@/components/ReTable";
import { type PaginationProps } from "@pureadmin/table";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { useNav } from "/@/layout/hooks/nav";
import { statusList, inv_type_list } from "./status";
import { useRouter } from "vue-router";
import { responseHandle } from "/@/utils/responseHandle";
import IntervalTime from "/@/components/IntervalTime";
import { useUserStoreHook } from "/@/store/modules/user";
const { logout } = useNav();
defineOptions({
  name: "invoiceApply"
});
const powers = ref([]);
const initform = {
  inv_in: "", //客户公司
  inv_out: "", //业务企业公司
  apply_name: "", //申请人
  // apply_id:'',//申请人ID
  status: "",
  inv_type: "",
  start: "",
  end: "",
  page: 1,
  size: 15
};

const form = reactive({ ...initform });

const { push } = useRouter();
const dataList = ref([]);
const loading = ref(false);
const { columns } = useColumns();

const pagination = reactive<PaginationProps>({
  total: 0,
  pageSize: 15,
  currentPage: 1,
  background: true
});

async function handleCurrentChange(val: number) {
  form.page = val;
  await onSearch();
}

async function handleSizeChange(val: number) {
  form.size = val;
  form.page = 1;
  await onSearch();
}

function handleSelectionChange(val) {
  console.log("handleSelectionChange", val);
}
function timeChange(start, end) {
  console.log(start);
  console.log(end);
}
async function onSearch() {
  if (loading.value) return;
  loading.value = true;
  const { code, data, message } = await httpList(form);
  responseHandle({
    code,
    message,
    logout,
    handler: () => {
      const { list, count } = data;
      dataList.value = list ?? [];
      pagination.total = count ?? 0;
      pagination.pageSize = form.size;
      pagination.currentPage = form.page;
    }
  });

  loading.value = false;
}
async function resetSearch() {
  form.page = 1;
  await onSearch();
}
//新建/详情页面
function editItem(id) {
  push({
    path: "/InvoiceSales/invoiceApplyDeatil",
    query: {
      id
    }
  });
}
async function resetForm() {
  Object.keys(form.value).forEach(key => {
    form.value[key] = initform[key];
  });
  await onSearch();
}

onMounted(() => {
  powers.value = useUserStoreHook().getMenuActions("invoiceApply");
  if (powers.value.some(i => i == "001")) {
    onSearch();
  }
});
</script>

<template>
  <div class="main">
    <div v-if="powers.some(i => i == '001')">
      <div class="bg-white p-4">
        <el-row :gutter="10" class="pb-4">
          <el-col :span="5">
            <el-select
              v-model="form.status"
              style="width: 100%"
              placeholder="发票申请状态"
              clearable
            >
              <el-option
                v-for="(si, sii) in statusList"
                :key="'status' + si.value + sii"
                :label="si.label"
                :value="si.value"
              />
            </el-select>
          </el-col>
          <el-col :span="5">
            <el-select
              v-model="form.inv_type"
              style="width: 100%"
              placeholder="发票类型"
              clearable
            >
              <el-option
                v-for="(si, sii) in inv_type_list"
                :key="'status' + si.value + sii"
                :label="si.label"
                :value="si.value"
              />
            </el-select>
          </el-col>
          <el-col :span="10">
            <IntervalTime
              v-model:startValue="form.start"
              v-model:endValue="form.end"
              @timeChange="timeChange"
          /></el-col>
          <el-col :span="4">
            <el-input
              v-model="form.apply_name"
              placeholder="申请人"
              style="width: 100%"
              clearable
            />
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="10">
            <el-input
              v-model="form.inv_in"
              placeholder="企业客户名称"
              style="width: 100%"
              clearable
            />
          </el-col>

          <el-col :span="10">
            <el-input
              v-model="form.inv_out"
              placeholder="业务企业公司"
              style="width: 100%"
              clearable
            />
          </el-col>
          <el-col :span="4">
            <el-button
              type="primary"
              :icon="useRenderIcon('search')"
              :loading="loading"
              class="fl"
              @click="resetSearch"
            >
              搜索
            </el-button>
            <el-button
              :icon="useRenderIcon('refresh')"
              class="fl"
              @click="resetForm()"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
      </div>

      <TableProBar
        title="发票申请管理"
        :loading="loading"
        :dataList="dataList"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon('add')"
            @click="editItem('')"
          >
            新增发票申请
          </el-button>
        </template>
        <template v-slot="{ size, checkList }">
          <PureTable
            border
            align="left"
            showOverflowTooltip
            table-layout="auto"
            :size="size"
            :data="dataList"
            :columns="columns"
            :checkList="checkList"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{ background: '#fafafa', color: '#606266' }"
            @selection-change="handleSelectionChange"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="editItem(row.id)"
                :icon="useRenderIcon('eye-view')"
              />
            </template>
          </PureTable>
        </template>
      </TableProBar>
    </div>
    <NoAuth v-else />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
