<!--
  * 批量调整
  *
  * @Author:    霍锦
  * @Date:      2024-11-18 14:17:31
  * @Copyright  友仓
-->
<template>
  <a-modal
      :title="title"
      :width="600"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 4 }">
      <a-form-item label="库位" name="locationIds">
        <LocationSelect style="width: 100%" v-model:value="form.locationIds" @change="changeLocationSelect"
                        mode="tags" placeholder="支持批量选择"/>
      </a-form-item>

      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item
              label="状态"
              name="status"
              :label-col="{ span: 8 }"
          >
            <a-switch
                v-model:checked="form.status"
                checked-children="空闲"
                un-checked-children="占用"
                style="width: 80px;"
            />
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item
              label="是否启用"
              name="disabledFlag"
              :label-col="{ span: 8 }"
          >
            <a-switch
                v-model:checked="form.disabledFlag"
                checked-children="启用"
                un-checked-children="禁用"
                style="width: 80px;"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup lang="ts">
import {nextTick, reactive, ref} from 'vue';
import {message} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import {locationApi} from '/@/api/business/base/location/location-api';
import {smartSentry} from '/@/lib/smart-sentry';
import LocationSelect from "/@/views/business/base/location/location-select.vue";


const title = ref('批量调整');

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
const visibleFlag = ref(false);

function showMultipleAdjust() {
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

//关闭
function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  locationIds: [] as number[], //库位ID
  status: true, //状态
  disabledFlag: true, //是否禁用
};

let form = reactive({...formDefault});

const rules = {
  locationIds: [{required: true, message: '库位 必填'}],
};


//多选库位
function changeLocationSelect(selectValue: []) {
  if (Array.isArray(selectValue)) {
    form.locationIds = selectValue.map((item: any) => item.locationId);
  }
}

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    console.log(form);
    await locationApi.multipleAdjust(form);
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  showMultipleAdjust,
});
</script>
