<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="auto" :rules="formRules">
                <el-form-item label="项目名称" prop="project_id" @click="showDialog = true"
                    :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" />
                </el-form-item>
                <el-form-item label="合同名称" prop="contract_id" @click="showDialog1 = true">
                    <el-input v-model="formData.contract_name" clearable readonly placeholder="点击选择合同" />
                </el-form-item>
                <el-form-item label="结算金额" prop="settlement_amount">
                    <el-input v-model="formData.settlement_amount" clearable placeholder="请输入结算金额" @input="amountinput"
                        type="number" />
                </el-form-item>
                <el-form-item label="结算金额大写" prop="settlement_amount_uppercase">
                    <el-input v-model="formData.settlement_amount_uppercase" readonly disabled placeholder="系统自动填写" />
                </el-form-item>
                <el-form-item label="结算差异">
                    <el-input v-model="formData.settlement_difference" placeholder="保存后自动结算" disabled />
                </el-form-item>
                <el-form-item label="结算日期" prop="settlement_date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.settlement_date" clearable type="date"
                        value-format="YYYY-MM-DD" placeholder="选择结算日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="结算明细附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>
                </el-form-item>
            </el-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectTable @customEvent="customEvent"></projectTable>
        </el-dialog>
        <div v-if="showDialog1">
            <el-dialog v-model="showDialog1" title="选择分包合同" width="70%">
                <!-- <contractTable @customEvent="customEvent1" :project_id="formData.project_id"></contractTable> -->
                <dialogTable :config="subcontracting_contract" :query="{ project_id: project?.id || '' }"
                    @customEvent="customEvent1">
                </dialogTable>
            </el-dialog>
        </div>
    </div>
</template>

<script lang="ts" setup name="customerDemandEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
import projectTable from "@/components/project/index.vue"
import { subcontracting_contract } from "@/components/dialogTable/dialogTableConfig"
import { computed } from "vue"


import { subcontractsettlementAdd, subcontractsettlementEdit, subcontractsettlementDetail } from '@/api/project_subcontract_settlement'
import { timeFormat, toChinesNum } from '@/utils/util'

import type { PropType } from 'vue'
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 orglist = reactive([])
const jobs = reactive([])
const project_name = ref('')
const project_code = ref('')
const custom_name
    = ref('')
import configs from "@/config"
const base_url = configs.baseUrl + configs.urlPrefix
import useUserStore from "@/stores/modules/user";

const userStore = useUserStore();

// dialog
const showDialog = ref(false)
const showDialog1 = ref(false)
const customEvent = (e: any) => {
    formData.project_id = e.id;
    formData.project_name = e.name;
    formData.contract_id = '';
    formData.contract_name = '';
    showDialog.value = false;
};
const customEvent1 = (e: any) => {
    formData.contract_id = e.id;
    formData.contract_name = e.contract_name;
    showDialog1.value = false;
};



// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑分包结算' : '新增分包结算'
})

// 表单数据
const formData = reactive({
    id: "",
    project_name: "",
    project_id: '',
    contract_id: '',
    contract_name: '',
    settlement_amount: '',
    settlement_amount_uppercase: '',
    remaining_amount: '',
    settlement_date: '',
    settlement_difference: "",
    remark: '',
    annex: [],
    approve_detail: {
        flow_type: 1,
        flow_path: 1,
        path: "asa/ssa"
    }
})



//监听输入
const amountinput = () => {
    formData.settlement_amount_uppercase = toChinesNum(formData.settlement_amount)
}
// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {

    project_name.value = data.project_name
    custom_name.value = data.custom_name
    project_code.value = data.project_code
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }

    //@ts-ignore
    formData.recording_time = timeFormat(formData.recording_time, 'YYYY-MM-DD ')
    amountinput()
}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await subcontractsettlementEdit(data)
        : await subcontractsettlementAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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


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