<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-row :gutter="10">
                    <el-col :span="24">
                        <el-form-item label="组织名称" prop="org_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select v-model="formData.org_id" clearable placeholder="请选择组织" @change="deptrmt"
                                class="flex-1">
                                <el-option v-for="(item, index) in list1" :key="index" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="部门名称" prop="dept_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select v-model="formData.dept_id" clearable placeholder="请选择部门" class="flex-1">
                                <el-option v-for="( item, index ) in  list2 " :key="index" :label="item.name"
                                    :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="账户编码">
                            <el-input v-model="bank_no" clearable disabled placeholder="系统自动生成" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="开户银行" prop="deposit_bank"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.deposit_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="开户名称" prop="account_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.account_name" clearable placeholder="请输入开户名称" />
                        </el-form-item>
                    </el-col>


                    <el-col :span="24">
                        <el-form-item label="账号" prop="account"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.account" clearable placeholder="请输入账号" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="开户日期" prop="account_opening_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.account_opening_date" clearable
                                type="date" value-format="YYYY-MM-DD" placeholder="选择开户日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="期初金额" prop="opening_amount"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.opening_amount" type="number" clearable placeholder="请输入期初金额" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>



                </el-row>

                <!-- <el-dialog v-model="showDialog3" title="选择收款人信息" width="70%">
                    <bankaccountDialog @customEvent="customEvent3"></bankaccountDialog>
                </el-dialog> -->



            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import porjectDialog from '@/components/project/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { bankaccountAdd, bankaccountEdit, bankaccountDetail } from '@/api/bank_account'
import { toChinesNum } from "@/utils/util";
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 list1 = reactive([])
const list2 = reactive([])
const showDialog = ref(false)
const bank_no = ref('')
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'


const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDialog3 = ref(false)


//获取所有组织
const getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)

    })
}
//获取部门
const deptrmt = (e: any) => {
    formData.dept_id = ''
    getlist1(e)

}
//获取所有部门
const getlist1 = (id: any) => {
    deptAll({ 'org_id': id }).then((res) => {
        list2.splice(0, list2.length, ...res)
    })
}

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑质量目标表' : '新增质量目标表'
})

// 表单数据
const formData = reactive({
    id: '',
    org_id: '',
    dept_id: '',
    deposit_bank: '',
    account_name: '',
    account: '',
    account_opening_date: '',
    opening_amount: '',
    remark: ''

})

const customEvent3 = (e: any) => {
    formData.deposit_bank = e.deposit_bank
    formData.account_name = e.account_name
    formData.account = e.account
    showDialog3.value = false
};
// 表单验证
const formRules = reactive<any>({

})


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


    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    if (data.dept_id) {
        getlist1(data.org_id)
    }

    bank_no.value = data.account_sn



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


// 提交按钮
const handleSubmit = async () => {

    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await bankaccountEdit(data)
        : await bankaccountAdd(data)
    popupRef.value?.close()
    emit('success')
}

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


}

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



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