<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="500"
    :visible="visible"
    :confirm-loading="loading"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '发货' : '发货'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-space>
      <a-form>
        <a-form-item label="设备编码" v-bind="validateInfos.equipmentCode">
          <a-input
            allow-clear
            :maxlength="30"
            placeholder="请输入设备编码"
            v-model:value="form.equipmentCode"
            @blur="
                  validate('equipmentCode', { trigger: 'blur' }).catch(() => {})
                "
          />
        </a-form-item>
<!--        <a-row :gutter="16">-->
<!--          <a-col :md="12" :sm="24" :xs="24">-->
<!--            <a-form-item label="选择设备" v-bind="validateInfos.customerName">-->
<!--              <a-input-->
<!--                allow-clear-->
<!--                :maxlength="30"-->
<!--                placeholder="请选择设备"-->
<!--                v-model:value="form.customerName"-->
<!--                @blur="-->
<!--                  validate('customerName', { trigger: 'blur' }).catch(() => {})-->
<!--                "-->
<!--              />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :md="12" :sm="24" :xs="24">-->
<!--            <a-form-item label="手机号码" v-bind="validateInfos.customerMobile">-->
<!--              <a-input-->
<!--                allow-clear-->
<!--                :maxlength="20"-->
<!--                placeholder="请填写联系人手机号码"-->
<!--                v-model:value="form.customerMobile"-->
<!--              />-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--        </a-row>-->
      </a-form>
    </a-space>
  </ele-modal>
</template>

<script lang="ts" setup>
import {ref, reactive, watch, computed} from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import TypeSelect from './customer-edit/type-select.vue';
  import ProgressSelect from './customer-edit/progress-select.vue';
  import SourceSelect from './customer-edit/source-select.vue';
  import { bindEquipment } from '@/api/apps/equipment';
  import type { Customer } from '@/api/oa/customer/model';
  import { createCode } from '@/utils/common';
  import { uploadFile } from '@/api/system/file';
  import type { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FILE_SERVER } from '@/config/setting';
  import { useUserStore } from '@/store/modules/user';
import { Equipment } from "@/api/apps/equipment/model";
import { Order } from "@/api/order/model";

  const userStore = useUserStore();
  // 当前用户信息
  const loginUser = computed(() => userStore.info ?? {});
  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: Order | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);

  // 表单数据
  const form = reactive<Equipment>({
    equipmentCode: '',
    orderId: undefined
  });

  // 已上传数据, 可赋初始值用于回显
  const images = ref(<any>[]);

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive({
    equipmentCode: [
      {
        required: true,
        type: 'string',
        message: '请输入设备编码',
        trigger: 'blur'
      }
    ]
  });

  const { resetFields, validate, validateInfos } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        // 去除空格
        const data = {
          ...form,
          orderId: props.data?.orderId,
          userId: props.data?.userId
        };
        // 转字符串
        bindEquipment(data)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  const onUpload = (d: ItemType) => {
    uploadFile(<File>d.file)
      .then((result) => {
        form.customerAvatar = result.path;
        message.success('上传成功');
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          // 头像赋值
          images.value = [];
          if(props.data.customerAvatar){
            images.value.push({ uid:1, url: FILE_SERVER + props.data.customerAvatar, status: '' });
          }
          assignObject(form, props.data);
          isUpdate.value = true;
        } else {
          form.customerCode = createCode();
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less"></style>
