<!-- 统一清台设置 -->
<template>
  <el-dialog title="清台设置" width="600px" v-model="visible">
    <el-form ref="formRef" :model="form" :rules="rules" label-position="right" label-width="120">
      <el-form-item label="清台方式">
        <el-radio-group v-model="form.autoClear">
          <el-radio label="手动清台" :value="0"></el-radio>
          <el-radio label="自动清台" :value="1"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="自动清台时间" prop="autoClearTime" v-if="form.autoClear === 1">
        <div class="column">
          <div>
            <el-input v-model="form.autoClearTime" style="width: 220px;" placeholder="请输入"
              @input="e => form.autoClearTime = filterNumberInput(e, 0)">
              <template #append>分钟后自动清台</template>
            </el-input>
          </div>
          <div class="tips">输入0时，默认支付后既自动清台</div>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitHandle" :loading="confirmLoading">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import tableApi from "@/api/account/table";
import { filterNumberInput } from '@/utils'

const visible = ref(false)
const formRef = ref(null)
const form = ref({
  autoClear: 1, // 自动清台 0手动 1自动
  autoClearTime: 0, // 自动清台时间 单位分钟 默认10
})

const rules = ref({
  autoClearTime: [
    {
      validator: (rule, value, callback) => {
        if (form.value.autoClearTime === '') {
          callback(new Error('请输入自动清台时间'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

// 提交配置
const confirmLoading = ref(false)
function submitHandle() {
  formRef.value.validate(async vaild => {
    try {
      if (vaild) {
        confirmLoading.value = true
        await tableApi.shopTabBatch(form.value)
        visible.value = false
        ElNotification({
          title: '注意',
          message: '设置成功',
          type: 'success'
        })
      }
    } catch (error) {
      console.log(error);
    }
    setTimeout(() => {
      confirmLoading.value = false
    }, 500);
  })
}

// 获取清台配置信息
async function tableCurrentStateAjax() {
  try {
    const res = await tableApi.tableCurrentState()
    form.value.autoClear = res.autoClear
    form.value.autoClearTime = res.autoClearTime || 0
  } catch (error) {
    console.log(error);
  }
}

function show() {
  visible.value = true
  tableCurrentStateAjax()
}

defineExpose({
  show
})
</script>

<style scoped lang="scss">
.column {
  display: flex;
  flex-direction: column;

  .tips {
    color: #999;
  }
}
</style>