<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="160px" :rules="formRules">
                <div style="display: flex; flex-direction: row-reverse; justify-content: flex-start;margin-bottom: 30px;">

                    <el-select class="w-[180px]" v-model="formData.dept_id" clearable placeholder="请选择部门">
                        <el-option v-for="(item, index) in list2" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                    <el-select class="w-[180px]" v-model="formData.org_id" clearable placeholder="请选择组织" @change="deptrmt">
                        <el-option v-for="(item, index) in list1" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                </div>

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_id" @click="showDialog = true" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="contract_name" readonly clearable placeholder="请选择项目合同" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="合同编号" prop="contract_id" @click="showDialog = true" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="contract_code" clearable placeholder="请选择合同" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="项目名称" prop="contract_id" @click="showDialog = true">
                            <el-input v-model="project_name" clearable placeholder="请选择合同" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="客户名称" prop="contract_id" @click="showDialog = true">
                            <el-input v-model="custom_name" clearable placeholder="请选择合同" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item></el-col>

                    <el-col :span="8">
                        <el-form-item label="合同类型" prop="contract_id" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select v-model="contract_type" clearable placeholder="请选择合同类型">
                                <el-option v-for="(item, index) in dictData.contract_type" :key="index" :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同计价方式" prop="contract_id" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="contract_pricing_method" clearable placeholder="请选择合同计价方式">
                                <el-option v-for="(item, index) in dictData.contract_pricing_method" :key="index" :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="业务负责人" prop="contract_id" @click="showDialog = true">
                            <el-input v-model="business_director" readonly clearable placeholder="请选择人员" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" />
                        </el-form-item></el-col><el-col :span="8">

                        <el-form-item label="审批id" prop="approve_id">
                            <el-input v-model="formData.approve_id" clearable placeholder="请输入审批id" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商单名称" prop="negotiation_name">
                            <el-input v-model="formData.negotiation_name" clearable placeholder="请输入洽商单名称" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商编号" prop="negotiation_no">
                            <el-input v-model="formData.negotiation_no" clearable placeholder="请输入洽商编号" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商金额" prop="negotiation_amount">
                            <el-input v-model="formData.negotiation_amount" clearable placeholder="请输入洽商金额" />
                        </el-form-item></el-col><el-col :span="24">
                        <el-form-item label="洽商类别" prop="negotiation_type">
                            <el-radio-group v-model="formData.negotiation_type" placeholder="请选择洽商类别">
                                <el-radio v-for="(item, index) in dictData.negotiation_type" :key="index" :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="成本金额（人工）" prop="labor_costs">
                            <el-input v-model="formData.labor_costs" clearable placeholder="请输入成本金额（人工）" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="成本金额（材料" prop="material_costs">
                            <el-input v-model="formData.material_costs" clearable placeholder="请输入成本金额（材料" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商质保金额" prop="warranty_amount">
                            <el-input v-model="formData.warranty_amount" clearable placeholder="请输入洽商质保金额" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商质保到期时间" prop="Warranty_expire_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.Warranty_expire_date" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择洽商质保到期时间">
                            </el-date-picker>
                        </el-form-item></el-col><el-col :span="8">

                        <el-form-item label="利润" prop="profit">
                            <el-input v-model="formData.profit" clearable placeholder="请输入利润" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="利润率" prop="profit_rate">
                            <el-input v-model="formData.profit_rate" clearable placeholder="请输入利润率" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商内容" prop="negotiation_content">
                            <el-input v-model="formData.negotiation_content" clearable placeholder="请输入洽商内容" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="具体说明" prop="negotiation_detail">
                            <el-input v-model="formData.negotiation_detail" clearable placeholder="请输入具体说明" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="签证报价表" prop="negotiation_quotation">
                            <el-input v-model="formData.negotiation_quotation" clearable placeholder="请输入签证报价表" />
                        </el-form-item></el-col><el-col :span="8">
                        <el-form-item label="洽商依据" prop="negotiation_basis">
                            <el-input v-model="formData.negotiation_basis" clearable placeholder="请输入洽商依据" />
                        </el-form-item></el-col>
                </el-row>
            </el-form>
        </popup>
        <el-dialog v-model="showDialog" title="选择合同" width="70%">
            <proontractTable @customEvent="customEvent"></proontractTable>

        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="contractNegotiationEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiContractNegotiationAdd, apiContractNegotiationEdit, apiContractNegotiationDetail } from '@/api/contract_negotiation'
import { timeFormat } from '@/utils/util'
import proontractTable from "@/components/project_contract/index.vue"
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 showDialog = ref(false)
const contract_name = ref('')
const contract_code = ref('')
const project_name = ref('')
const custom_name = ref('')
const business_director = ref('')
const contract_type = ref('')
const contract_pricing_method = ref('')
const list1 = reactive([])
const list2 = reactive([])
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑合同洽商' : '新增合同洽商'
})

// 表单数据
const formData = reactive({
    id: '',
    dept_id: '',
    org_id: '',
    contract_id: '',
    approve_id: '',
    negotiation_name: '',
    negotiation_no: '',
    negotiation_amount: '',
    negotiation_type: '',
    labor_costs: '',
    material_costs: '',
    warranty_amount: '',
    Warranty_expire_date: '',
    profit: '',
    profit_rate: '',
    negotiation_content: '',
    negotiation_detail: '',
    negotiation_quotation: '',
    negotiation_basis: '',
})

//获取值
const customEvent = (e: any) => {
    formData.contract_id = e.id;
    contract_name.value = e.contract_name;
    contract_code.value = e.contract_code
    custom_name.value = e.customer_name
    business_director.value = e.business_director
    business_director.value = e.business_director
    contract_type.value = e.contract_type
    project_name.value = e.project_name
    contract_pricing_method.value = e.contract_pricing_method

    showDialog.value = false;
};
// 表单验证
const formRules = reactive<any>({
})

//获取所有组织
const getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)
        if (res.length > 0 && !formData.org_id) {
            formData.org_id = res[0].id
            deptAll({ 'org_id': res[0].id }).then((res) => {
                if (res.length > 0) {
                    Object.assign(list2, res)
                    formData.dept_id = res[0].id
                }

            })
        }
    })
}
//获取部门
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 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)
    }
    business_director.value = data.business_director
    contract_name.value = data.contract_name
    contract_code.value = data.contract_code
    project_name.value = data.project_name
    custom_name.value = data.custom_name
    contract_type.value = data.contract_type
    contract_pricing_method.value = data.contract_pricing_method
    //@ts-ignore
    formData.Warranty_expire_date = timeFormat(formData.Warranty_expire_date, 'yyyy-mm-dd hh:MM:ss')
}

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


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

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

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



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