<!-- 我的仓库-编辑仓库 -->
<template>
    <app-drawer :title="selectedRow?.warehousecode ? '修改仓库' : '新增仓库'" :width="880" v-model:show="show"
        :loading="loading" :refresh="refresh">
        <el-form ref="formRef" class="el-form--horizontal" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item label="仓库代码" prop="warehousecode">
                <el-input placeholder="请输入" v-model="formData.warehousecode" />
            </el-form-item>
            <el-form-item label="所属机构">
                <span>{{ accountName }}</span>
            </el-form-item>
            <el-form-item label="仓库名称" prop="warehousename">
                <el-input placeholder="请输入" v-model="formData.warehousename" />
            </el-form-item>
            <el-form-item label="仓库类型" prop="warehousetype">
                <span>自有库</span>
            </el-form-item>
            <el-form-item label="联系人" prop="contactname">
                <el-input placeholder="请输入" v-model="formData.contactname" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactnum">
                <el-input placeholder="请输入" v-model="formData.contactnum" />
            </el-form-item>
            <el-form-item class="el-form-item--row" label="仓库地址" prop="area">
                <app-region class="el-form-item--col" v-model:province="formData.provinceid"
                    v-model:city="formData.cityid" v-model:district="formData.districtid" />
            </el-form-item>
            <el-form-item class="el-form-item--row" prop="address">
                <el-input placeholder="请输入" v-model="formData.address" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)" plain>取消</el-button>
            <el-button type="primary" @click="onSubmit">提交</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useWarehouseForm } from '@/business/warehouse'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppRegion from '@pc/components/base/region/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.WarehouseInfoRsp>,
        default: () => ({})
    }
})

const { loading, formData, accountName, addOrUpdate } = useWarehouseForm(props.selectedRow)
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    warehousecode: [{ required: true, message: '请输入仓库代码', trigger: 'blur' }],
    warehousename: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
    contactname: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
    contactnum: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
    address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }],
    area: [
        {
            required: true,
            validator: (rule, value, callback) => {
                if (formData.provinceid && formData.cityid && formData.districtid) {
                    callback()
                } else {
                    callback(new Error('请选择仓库地区'))
                }
            }
        }
    ],
}

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            addOrUpdate().then(() => {
                ElMessage.success('提交成功')
                onCancel(true)
            }).catch((err) => {
                ElMessage.error('提交失败：' + err)
            })
        }
    })
}
</script>