<template>
  <div class="setting-container">
    <el-form ref="orderFormRef" :model="form" :rules="rules" label-width="160px" class="premium-setting-form">
      <el-form-item label="超时取消时间：" prop="timeoutCancelTime">
        <div class="input-with-unit">
          <el-input-number v-model="form.timeoutCancelTime" :min="1" :precision="0" step-strictly placeholder="请输入" class="config-input-number" />
          <span class="unit-text">分钟</span>
        </div>
        <div class="form-tip">订单派单以后，超时未接单的订单自动取消</div>
      </el-form-item>

      <el-form-item class="action-item">
        <el-button type="primary" class="save-btn" :loading="loading" @click="submitForm">保存修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { getOrderSetting, updateOrderSetting } from '@/api/order/setting';
// @ts-ignore
import { OrderSettingVO, OrderSettingForm } from '@/api/order/setting/types';
import { ref, onMounted, getCurrentInstance, ComponentInternalInstance } from 'vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const orderFormRef = ref<any>();

const form = ref<OrderSettingForm>({
  id: 1,
  timeoutCancelTime: 30
});

const rules = {
  timeoutCancelTime: [{ required: true, message: "时长不能为空", trigger: "blur" }]
};

const loadData = async () => {
  try {
    const res = await getOrderSetting(1);
    if (res.code === 200) {
      form.value = { ...res.data };
    }
  } catch (err) {}
};

const submitForm = () => {
  orderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      try {
        await updateOrderSetting(form.value);
        proxy?.$modal.msgSuccess("保存成功");
        loadData();
      } finally {
        loading.value = false;
      }
    }
  });
};

onMounted(() => {
  loadData();
});
</script>

<style scoped lang="scss">
.setting-container {
  padding-top: 10px;
}

.premium-setting-form {
  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #4e5969;
    padding-right: 24px;
  }

  .input-with-unit {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  .config-input-number {
    max-width: 180px;
    :deep(.el-input__wrapper) {
      box-shadow: 0 0 0 1px #e5e6eb inset;
      border-radius: 6px;
    }
  }

  .unit-text {
    color: #4e5969;
    font-size: 14px;
  }

  .form-tip {
    font-size: 13px;
    color: #86909c;
    margin-top: 8px;
    line-height: 1.4;
    display: block;
    width: 100%;
  }

  .action-item {
    margin-top: 40px;
  }

  .save-btn {
    padding: 12px 32px;
    height: auto;
    font-size: 15px;
    font-weight: 500;
    border-radius: 6px;
    box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
  }
}
</style>
