<script setup lang="ts">
    import { reactive } from 'vue'
    import * as NongjifuwuAPI from '@/api/model/nongjifuwu'
    import { useUserStore } from '@/store/modules/user'

    const { getUserInfo } = useUserStore()

    const pageData = reactive({
        show: false,
        content: '',
        form: {
            machineId: '',
            serviceType: '',
            userId: '',
            time: '',
            startTime: '',
            endTime: '',
            scope: '',
            address: '',
            phone: '',
            demand: '',
        },
        rules: [
            {
                name: 'startTime',
                rule: ['required'],
                msg: ['请选择开始时间'],
            },
            {
                name: 'endTime',
                rule: ['required'],
                msg: ['请选择结束时间'],
            },
            {
                name: 'scope',
                rule: ['required'],
                msg: ['请填写地区'],
            },
            {
                name: 'address',
                rule: ['required'],
                msg: ['请填写详细地址'],
            },
            {
                name: 'phone',
                rule: ['required'],
                msg: ['请填写联系方式'],
            },
            {
                name: 'demand',
                rule: ['required'],
                msg: ['请填写需求'],
            },
        ],
    })
    const dict = reactive({
        show: {
            time: false,
        },
    })

    function handleTimeChange(e) {
        pageData.form.startTime = e.startDate.result
        pageData.form.endTime = e.endDate.result
        pageData.form.time = `${e.startDate.result} 至 ${e.endDate.result}`
        dict.show.time = false
    }
    function handleTimeCancel() {
        dict.show.time = false
    }

    function open(params) {
        pageData.form.machineId = params.id
        pageData.form.serviceType = params.serviceType
        pageData.form.userId = getUserInfo.id
        pageData.show = true
    }
    function close() {
        pageData.show = false
    }
    function reset() {
        pageData.form = {
            machineId: '',
            serviceType: '',
            userId: '',
            time: '',
            startTime: '',
            endTime: '',
            scope: '',
            address: '',
            phone: '',
            demand: '',
        }
    }
    const formRef = ref()
    function submit() {
        formRef.value.validator(pageData.form, pageData.rules, true).then((res) => {
            if (res.isPassed) {
                NongjifuwuAPI.farmMachineRegister(pageData.form).then(() => {
                    reset()
                    close()
                    uni.showToast({
                        title: '提交成功',
                        icon: 'success',
                    })
                })
            }
        })
    }
    defineExpose({
        open,
        close,
    })
</script>

<template>
    <fui-dialog title="" :buttons="[]" :show="pageData.show" maskClosable>
        <view class="title flex justify-center">
            <view class="flex-1">我有需要</view>
            <fui-icon class="close flex-basis" name="close" size="46" color="#999999" @click="close" />
        </view>
        <view class="text-left">
            <fui-form ref="formRef">
                <fui-input marginTop="30" size="24" placeholder="请填写预留手机" v-model="pageData.form.phone" />
                <fui-input marginTop="30" size="24" placeholder="请填写作业地区" v-model="pageData.form.scope" />
                <fui-input marginTop="30" size="24" placeholder="请填写作业详细地址" v-model="pageData.form.address" />
                <fui-input
                    marginTop="30"
                    size="24"
                    placeholder="请选择作业时间"
                    v-model="pageData.form.time"
                    @click="dict.show.time = true"
                />
                <fui-textarea v-model="pageData.form.demand" :marginTop="30" size="24" placeholder="简要说明作业需求" />
                <view style="margin-top: 30rpx">
                    <fui-button type="warning" text="确定" bold radius="96rpx" @click="submit" />
                </view>
            </fui-form>
        </view>
    </fui-dialog>

    <fui-date-picker :show="dict.show.time" type="3" range @change="handleTimeChange" @cancel="handleTimeCancel" />
</template>

<style lang="scss" scoped>
    :deep(.fui-dialog__body) {
        background: linear-gradient(0deg, rgba(93, 182, 111, 0) 0%, rgba(93, 182, 111, 0.25) 100%);
        margin: 0;
    }
    :deep(.fui-dialog__footer) {
        display: none;
    }
    :deep(.fui-input) {
        margin-top: 40rpx;
    }
</style>
