<template>
    <hc-new-dialog widths="30rem" :show="isShow" title="新增中期支付证书" @save="addModalSave" @close="addModalClose">
        <el-form ref="formRef" class="p-2" label-position="top" :model="formModel" :rules="formRules" size="large">
            <el-form-item label="计量期：" prop="contractPeriodId">
                <el-select v-model="formModel.contractPeriodId" block>
                    <el-option v-for="item in allPeriodData" :key="item.id" :label="item.periodNumber" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="证书编号：" prop="certificateNumber">
                <el-input v-model="formModel.certificateNumber" />
            </el-form-item>
            <el-form-item label="材料计量期：" prop="materialPeriodIds">
                <div v-if="meterPeriodData.length > 0" class="form-item-div">
                    <el-checkbox-group v-model="materialPeriodIds">
                        <template v-for="item in meterPeriodData" :key="item.id">
                            <el-checkbox v-if="item.periodNumber" :label="item.periodNumber">{{ item.periodName }}</el-checkbox>
                        </template>
                    </el-checkbox-group>
                </div>
                <div v-else class="form-item-div text-orange">无材料计量期可进行关联</div>
            </el-form-item>
            <el-form-item label="开工预付计量期：" prop="startPeriodIds">
                <div v-if="startPeriodData.length > 0" class="form-item-div">
                    <el-checkbox-group v-model="startPeriodIds">
                        <template v-for="item in startPeriodData" :key="item.id">
                            <el-checkbox v-if="item.periodNumber" :label="item.periodNumber">{{ item.periodName }}</el-checkbox>
                        </template>
                    </el-checkbox-group>
                </div>
                <div v-else class="form-item-div text-orange">无开工计量期可进行关联</div>
            </el-form-item>
            <el-form-item label="打印日期：">
                <el-date-picker v-model="formModel.printDate" class="block" format="YYYY-MM-DD" type="date" value-format="YYYY-MM-DD" />
            </el-form-item>
        </el-form>
    </hc-new-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useAppStore } from '~src/store'
import { formValidate, getArrValue } from 'js-fast-way'
import mainApi from '~api/debit-pay/admin/certificate'

//事件
const emit = defineEmits(['finish', 'close'])

//变量
const useAppState = useAppStore()
const projectId = ref(useAppState.getProjectId || '')
const contractId = ref(useAppState.getContractId || '')

//双向绑定
// eslint-disable-next-line no-undef
const isShow = defineModel('modelValue', {
    default: false,
})

//获取合同计量期
const allPeriodData = ref([])
const getAllPeriod = async () => {
    const { data } = await mainApi.getAllPeriod({
        projectId: projectId.value,
        contractId: contractId.value,
    })
    allPeriodData.value = getArrValue(data)
    formModel.value.contractPeriodId = allPeriodData.value[ allPeriodData.value.length - 1].id
}

//获取材料计量期
const meterPeriodData = ref([])
const materialPeriodIds = ref([])
const getMeterPeriod = async () => {
    const { data } = await mainApi.getMeterPeriod({
        projectId: projectId.value,
        contractId: contractId.value,
        type: 1,
    })
    meterPeriodData.value = getArrValue(data)
}

//获取开工计量期
const startPeriodData = ref([])
const startPeriodIds = ref([])
const getStartPeriod = async () => {
    const { data } = await mainApi.getMeterPeriod({
        projectId: projectId.value,
        contractId: contractId.value,
        type: 2,
    })
    startPeriodData.value = getArrValue(data)
}

//监听
watch(isShow, (val) => {
    if (val) {
        formModel.value = {}
        getAllPeriod()
        getMeterPeriod()
        getStartPeriod()
    }
})

//表单数据
const formRef = ref(null)
const formModel = ref({})
const formRules = {
    contractPeriodId: {
        required: true,
        trigger: 'blur',
        message: '请选择计量期',
    },

}

//保存
const addModalSave = async () => {
    const isValidate = await formValidate(formRef.value)
    if (!isValidate) return false
    const form = formModel.value
    form.projectId = projectId.value
    form.contractId = contractId.value
    form.materialPeriodIds = materialPeriodIds.value.join(',')
    form.startPeriodIds = startPeriodIds.value.join(',')
    const { error, code, msg } = await mainApi.add(form)
    if (!error && code === 200) {
        window.$message.success('保存成功')
        addModalClose()
        emit('finish')
    } else {
        window.$message.error(msg ?? '保存失败')
    }
}

//关闭弹窗
const addModalClose = () => {
    isShow.value = false
    emit('close')
}
</script>
