<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { statusList, inv_type_list } from "./status";
import { useRouter } from "vue-router";
import IntervalTime from "/@/components/IntervalTime";
import NoAuth from "/@/components/NoAuth/NoAuth.vue";
import contentConfig from "./config/content.config";
import { PageContent } from "/@/components/PageContent";
import { usePermission } from "/@/hooks";

defineOptions({
  name: "invoiceApply"
});

const initform = {
  customer: "", //客户公司
  inv_out: "", //业务企业公司
  apply_name: "", //申请人
  // apply_id:'',//申请人ID
  status: "",
  inv_type: "",
  start: "",
  end: "",
  page: 1,
  size: 15
};

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

const { push } = useRouter();
const pageContentRef = ref<InstanceType<typeof PageContent>>(null);
const loading = ref(false);

async function resetSearch() {
  form.page = 1;
  (pageContentRef.value as any).getPageData(form);
}
//新建/详情页面
const toDetail = (id?: string) => {
  push({
    path: "/InvoiceSales/invoiceApplyDetail",
    query: {
      ...(id ? { id } : {})
    }
  });
};

async function resetForm() {
  Object.keys(form).forEach(key => {
    form[key] = initform[key];
  });
  (pageContentRef.value as any).getPageData(form);
}
const { permission, contentConfigRef } = usePermission({
  pageName: "invoiceApply",
  contentConfig,
  callback: powers => (contentConfig.powers = powers)
});
</script>

<template>
  <div class="main">
    <div v-if="permission.list">
      <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"
          /></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.customer"
              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>

      <PageContent
        ref="pageContentRef"
        :content-config="contentConfigRef"
        @preview-btn-click="({ invNo }) => toDetail(invNo)"
        @create-btn-click="() => toDetail()"
      />
    </div>
    <NoAuth v-else />
  </div>
</template>

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