<script lang="ts" setup>
import { useVModel,  } from '@vueuse/core';
import { ElButton, ElForm, ElFormItem } from 'element-plus';
import { computed, reactive, ref, watch } from 'vue';
import { useUserInfo } from '/@/hooks/core/useUser';
import { useCompany } from '/@/hooks/core/useCompany';
import { httpRequsetExport } from '/@/utils/export';
import dayjs from 'dayjs';

// import { httpDetailExport } from "/@/api/InvoiceSales/invoiceApply";



const props = defineProps<{ visible: boolean }>()
const { isSuperUser, userInfo } = useUserInfo();

const { currentCompany } = useCompany()



const formData = reactive({
    month: '',
    companyNo: ''
})

const { companyList } = useCompany();


const rules = {
    month: [{ required: true, trigger: 'change', message: '请选择月份' }],
    companyNo: [{ required: true, trigger: 'change', message: '想选择业务公司' }],
}


const visible = useVModel(props, 'visible')


const formRef = ref<InstanceType<typeof ElForm> | null>(null)

const loading = ref(false)

async function handleExport(){
    const { month, companyNo } = formData
    const days = dayjs(month).daysInMonth()
    const start = month + '-' + '01'
    const end = month + '-' + days

    const params = {
        companyNo,
        start,
        end,
        is_show_cgd: '1'
    }

    if (userInfo.value.itemid == '52' && currentCompany.value.companyNo === 'GS2302231125079621'){
        params.is_show_cgd = '0'
    }

    await httpRequsetExport({
        url: "http://rep.report.caixiao365.com/admin/Everyday/EveryInvByQrd",
        name: "发票申请订单明细导出(业务公司)",
        noBaseUrl: true,
        onStart: () => (loading.value = true),
        onSuccess: () => {
            loading.value = false; 
            visible.value = false;
        },
        onFail: () => (loading.value = false),
        params
    });
}

function handleClose(){
    formData.companyNo = ''
    formData.month = ''
}

const companies = ref([])

watch(() => props.visible, () => {

    companies.value = [
        { "companyNo": "GS2402020907088731", "companyName": "北京泰文创供应链管理有限公司", },
        { "companyNo": "GS2302231125079621", "companyName": "北京百辰荣达国际科贸有限公司", },
        { "companyNo": "GS2302231124114965", "companyName": "普润&锦兴&知事", },
    ]
    // if(isSuperUser.value){
    //     companies.value = [
    //         { "companyNo": "GS2402020907088731", "companyName": "北京泰文创供应链管理有限公司", },
    //         { "companyNo": "GS2302231125079621", "companyName": "北京百辰荣达国际科贸有限公司", },
    //         { "companyNo": "GS2302231124114965", "companyName": "普润&锦兴&知事", },
    //     ]
    // } else {
    //     const _compaines =[]

    //     const list = ['北京泰文创供应链管理有限公司', '北京百辰荣达国际科贸有限公司']
    //     const prs = ['北京普润心堂商贸有限公司', '北京锦兴弘昌科技有限公司', '北京知事文化产业发展有限公司']
    //     let isHas = false

    //     for(const company of companyList.value){

    //         if (list.includes(company.companyName)){
    //             _compaines.push(company)
    //         }


    //         if (prs.includes(company.companyName) && !isHas){
    //             _compaines.push({ "companyNo": "GS2302231124114965", "companyName": "普润&锦兴&知事" })
    //             isHas = true
    //         }

    //     }

    //     companies.value = _compaines
    // }
})

watch(() => visible.value, () => {
    if(!visible.value) return

    if (currentCompany.value.companyNo === 'GS2304031312553746' || currentCompany.value.companyNo === 'GS2401181650538135'){
        formData.companyNo = 'GS2302231124114965'
    } else {
        formData.companyNo = currentCompany.value.companyNo
    }

})
</script>

<template>
    <ElDialog title="发票申请订单明细导出(业务公司)" v-model="visible" center @close="handleClose" class="custom-dialog">
        <ElForm label-width="100px" :model="formData" :rules="rules" ref="formRef" v-loading="loading">
            <ElFormItem label="月份" prop="month">
                <ElDatePicker 
                    v-model="formData.month" 
                    style="width: 100%" 
                    placeholder="月份" 
                    value-format="YYYY-MM"
                    type="month" :disabledDate="time => time.getTime() > Date.now()" 
                />
            </ElFormItem>

            <ElFormItem label="业务公司" prop="companyNo">
                <ElSelect v-model="formData.companyNo" placeholder="请选择公司" style="width: 100%" :disabled="!isSuperUser">
                    <ElOption v-for="c in companies" :key="c.companyNo" :label="c.companyName" :value="c.companyNo" />
                </ElSelect>
            </ElFormItem>


            <ElFormItem>
                <div style="width: 100%;display: flex;justify-content: flex-end;">
                    <ElButton type="primary" @click="handleExport">提交</ElButton>
                </div>
            </ElFormItem>
        </ElForm>
    </ElDialog>
</template>