<!-- 用户编辑弹窗 -->
<template>
  <div class="page">
    <div class="ele-body">
      <a-card :bordered="false">
        <a-form
          ref="formRef"
          :label-col="{ md: { span: 8 }, sm: { span: 24 } }"
          :wrapper-col="{ md: { span: 24 }, sm: { span: 24 } }"
          layout="vertical"
          :model="form"
          :rules="rules"
        >
          <a-form-item label="标题" name="title">
            <a-input
              allow-clear
              placeholder="请输入标题"
              :disabled="isUpdate"
              v-model:value="form.title"
            />
          </a-form-item>
          <a-form-item label="发送范围" name="channel">
            <a-input
              allow-clear
              placeholder="请输入用户名"
              v-model:value="form.channel"
            />
          </a-form-item>
          <a-form-item label="内容" name="comments">
            <a-textarea
              :rows="4"
              :maxlength="200"
              placeholder="请输入内容"
              v-model:value="form.comments"
            />
            <a-space>
              <a-button
                type="primary"
                class="ele-btn-icon"
                style="margin-top: 10px"
                @click="save"
              >
                <span>保存</span>
              </a-button>
              <a-button
                class="ele-btn-icon"
                style="margin-top: 10px"
                @click="send"
              >
                <span>发送</span>
              </a-button>
            </a-space>
          </a-form-item>
        </a-form>
      </a-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import type { BcEquipment } from '@/api/apps/bc/equipment/model';
  import {
    addBcEquipment,
    updateBcEquipment,
    addSend
  } from '@/api/apps/bc/equipment';
  import { FormInstance, Rule } from 'ant-design-vue/es/form';
  import useFormData from '@/utils/use-form-data';
  import { Notice } from '@/api/oa/notice/model';

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: BcEquipment | null;
  }>();
  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 是否是修改
  const isUpdate = ref(false);

  // 提交状态
  const loading = ref(false);

  const formRef = ref<FormInstance | null>(null);

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

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<Notice>({
    title: '通知',
    channel: 'ZhaiLing',
    comments: '您的申请已通过',
    status: 0
  });

  // 表单验证规则
  const rules = reactive<Record<string, Rule[]>>({
    title: [
      {
        required: true,
        message: '请输入设备名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    channel: [
      {
        required: true,
        type: 'string',
        message: '请选择发布范围',
        trigger: 'blur'
      }
    ],
    comments: [
      {
        required: true,
        type: 'string',
        message: '请填写内容',
        trigger: 'blur'
      }
    ],
    status: [
      {
        required: true,
        type: 'number',
        message: '请选择设备状态',
        trigger: 'blur'
      }
    ],
    sortNumber: [
      {
        required: true,
        type: 'number',
        message: '请输入排序号',
        trigger: 'blur'
      }
    ]
  });

  /* 控制放店开关 */
  const editStatus = () => {
    if (form.status == 0) {
      form.status = 1;
    } else {
      form.status = 0;
    }
    updateBcEquipment(form)
      .then(() => {
        message.success('操作成功');
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  const onClose = () => {
    updateVisible(false);
  };

  const send = () => {
    addSend(form).then();
  };

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const equipmentForm = {
          ...form
        };
        console.log(equipmentForm, 'equipmentForm');
        const saveOrUpdate = isUpdate.value
          ? updateBcEquipment
          : addBcEquipment;
        saveOrUpdate(equipmentForm)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          assignFields({
            ...props.data
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate();
      }
    }
  );
</script>
<style lang="less">
  .tab-pane {
    min-height: 300px;
  }
  .ml-10 {
    margin-left: 5px;
  }
</style>
