<template>
    <div>
        <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
            :close-on-press-escape="false" width="30%">
            <el-form ref="formRef" :model="form" size="default" label-width="80px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <p>默认分成比例</p>
                        <p>修改所有项目默认的分成比例（抽成金额精确到小数点后一位）</p>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="分成比例" prop="drawRatio"
                            :rules="[{ required: true, message: '请输入分成比例', trigger: ['blur', 'change'] }]">
                            <el-input v-model="form.drawRatio" autocomplete="off" >
                                <template v-slot:append>%</template>
                            </el-input>
                        </el-form-item>
                    </el-col>                    
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">                        
                        <el-form-item label="生效日期" prop="effectDate" :rules="[{ required: true, message: '请输入生效日期', trigger: ['blur', 'change'] }]">                            
                            <el-date-picker type="date" v-model="form.effectDate" placeholder="选择日期" format="YYYY-MM-DD"
                            class="w100">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>                    
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="default">取 消</el-button>
                    <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="admin/projectprice/companyform">

// import { stat } from 'fs'
import { reactive, toRefs, ref } from 'vue'
import { CompanyDrawInput } from '/@/api/admin/data-contracts'
import { ProjectPriceApi } from '/@/api/admin/ProjectPrice'
import eventBus from '/@/utils/mitt'
import { formatDate } from '/@/utils/formatTime'


defineProps({
    title: {
        type: String,
        default: '',
    },
})

const formRef = ref()
const state = reactive({
    value2:'',
    showDialog: false,
    sureLoading: false,
    form: {} as CompanyDrawInput    
})
const { form } = toRefs(state)


// 打开对话框
const open = async () => {    
    state.form = {} as CompanyDrawInput    
    state.showDialog = true
}


// 取消
const onCancel = () => {
    state.showDialog = false
}

// 确定
const onSure = () => {
    formRef.value.validate(async (valid: boolean) => {
        if (!valid) return          
        
        let dn= formatDate(state.form.effectDate,"YYYY-mm-dd");                
        state.form.effectDate=dn    

        state.sureLoading = true
        let res = {} as any        
            res = await new ProjectPriceApi().updateCompany(state.form, { showSuccessMessage: true }).catch(() => {
                state.sureLoading = false
            })
        
        state.sureLoading = false

        if (res?.success) {
            eventBus.emit('refreshCompanyDraw')
            state.showDialog = false
        }
    })
}

defineExpose({
    open,
})
</script>
