<template>
    <div class="edit-popup">

        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">


            <el-form ref="formRef" :model="formData" label-width="auto" :rules="formRules">
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="出库流水表ID" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请选择出库流水" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="序列号" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入序列号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="出库日期" prop="invoicing_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">

                            <el-date-picker class="flex-1 !flex" v-model="formData.invoicing_date" clearable
                                value-format="YYYY-MM-DD " placeholder="选择出库日期">
                            </el-date-picker>

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="仓库" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入仓库" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="仓库编码" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入仓库编码" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="材料类别" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入材料类别" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="材料中类" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入材料中类" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="材料小类" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入材料小类" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="仓库名称" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入仓库名称" />
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="材料名称" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入材料名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="材料编码" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入材料编码" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="规格型号" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入规格型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="品牌" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入品牌" />
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="参数说明" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入参数说明" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="单位" prop="contract_id">
                            <el-input v-model="project_code" clearable placeholder="请输入单位" />
                        </el-form-item>
                    </el-col>


                </el-row>


                <el-dialog v-model="showDialog1" title="选择项目" width="70%">
                    <projectDialog @customEvent="customEvent1" contract_type="2"></projectDialog>
                </el-dialog>
            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { toChinesNum } from "@/utils/util";
import projectDialog from '@/components/project/index.vue'
import { apiinvoiceapplyAdd, apiinvoiceapplyEdit, apiinvoiceapplyDetail } from '@/api/InvoicingRequests'
import { getAllProjectTypes } from '@/api/projecttype'
import { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const protype = reactive([])
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const active = ref(0)
const formDataannex = reactive([])
const contract_name = ref('')
const project_name = ref('')
const project_code = ref('')
const project_amount = ref('')
const contract_no = ref('')
const custom_name = ref('')

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog = ref(false)
const showDialog1 = ref(false)

const customEvent = (e: any) => {
    formData.customer_id = e.id;
    custom_name.value = e.name;
    showDialog.value = false;
};
const customEvent1 = (e: any) => {
    formData.contract_id = e.id;
    project_name.value = e.project_name;
    project_code.value = e.project_code;
    contract_name.value = e.contract_name;
    contract_no.value = e.contract_code
    project_amount.value = e.amount

    showDialog1.value = false;
};
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑序列号出库表' : '新增序列号出库表'
})

// 表单数据
const formData = reactive({
    id: '',
    customer_id: '',
    contract_id: '',
    approve_id: '',
    invoicing_date: "",
    period: "",
    accumulate_amount: '',
    accumulated_payments_received: '',
    tax_rate: "",
    invoice_type: '',
    invoicing_amount: '',
    invoicing_amount_daxie: "",
    tax: '',
    amount_including_tax: '',
    content: "",
    invoice_no: "",
    remark: "",
    annex: [],
    invoicing_company_name: "",
    taxpayer_identification_number: "",
    deposit_bank: "",
    bank_accnout: "",
    address_phone: "",
    receiving_address: "",
    contacts: "",
    phone: "",
    mailing_time: "",
    mailing_type: "",
    mailing_no: ""
})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    if (data.annex && data.annex.length > 0) {

        const arry1 = data.annex.map((item: any, index: any) => {
            return {
                name: `文件${index + 1}`,
                uri: item
            };
        });
        Object.assign(formDataannex, arry1)

    }


    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    if (formData.invoicing_amount) {
        toChinesNum(formData.invoicing_amount)
    }
    custom_name.value = data.custom.name
    project_name.value = data.contract_name;
    project_code.value = data.contract_code
    project_amount.value = data.contract.amount
    contract_name.value = data.contract.contract_name;
    contract_no.value = data.contract.contract_code


}
const getDetail = async (row: Record<string, any>) => {
    const data = await apiinvoiceapplyDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item: any) => item.uri)
    }

    console.log(formRef.value?.validate(), '22222222')
    await formRef.value?.validate()


    const data = { ...formData }
    mode.value == 'edit'
        ? await apiinvoiceapplyEdit(data)
        : await apiinvoiceapplyAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()

    getAllProjectTypes().then((res) => {

        protype.splice(0, protype.length, ...res);
    })
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
