<template>
  <Modal :maskClosable="false" :keyboard="false" v-model:open="data.isShowModal" :footer="null" 
         :title="$t('robot.consumptionRecord.addTitle')" width="50%" @cancel="_close">
    <Form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 6 }" labelAlign="left" :labelWrap="true">
      <Row :gutter="24">
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.robotUuid')" name="robotUuid">
            <Input v-model:value="form.robotUuid" allowClear :placeholder="$t('robot.consumptionRecord.robotUuidRequired')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.robotAccount')" name="robotAccount">
            <Input v-model:value="form.robotAccount" allowClear :placeholder="$t('robot.consumptionRecord.robotAccountRequired')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.consumptionAmount')" name="consumptionAmount">
            <InputNumber v-model:value="form.consumptionAmount" allowClear :placeholder="$t('robot.consumptionRecord.consumptionAmountRequired')" style="width: 100%" :min="0" :precision="2"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.consumptionTime')" name="consumptionTime">
            <DatePicker v-model:value="form.consumptionTime" allowClear :placeholder="$t('robot.consumptionRecord.consumptionTimeRequired')" style="width: 100%"  showTime format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.consumptionMethod')" name="consumptionMethod">
            <Select allow-clear showSearch :filterOption="selectFilterOption" v-model:value="form.consumptionMethod" 
                    allowClear :placeholder="$t('robot.consumptionRecord.consumptionMethodRequired')">
              <SelectOption v-for="(item, index) in data.consumptionMethodOptions" :key="index" :value="item.value" 
                            :label="item.label" :text="item.label">{{ item.label }}
              </SelectOption>
            </Select>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.creator')" name="creator">
            <Input v-model:value="form.creator" allowClear :placeholder="$t('robot.consumptionRecord.creatorRequired')"/>
          </FormItem>
        </Col>
        <Col :span="24">
          <FormItem :label-col="{ span: 3 }" :label="$t('robot.consumptionRecord.consumptionInfo')" name="consumptionInfo">
            <TextArea v-model:value="form.consumptionInfo" allowClear :placeholder="$t('robot.consumptionRecord.consumptionInfoRequired')" :rows="3"/>
          </FormItem>
        </Col>
        <Col :span="24">
          <FormItem :label-col="{ span: 3 }" :label="$t('robot.consumptionRecord.notes')" name="notes">
            <TextArea v-model:value="form.notes" allowClear :placeholder="$t('common.placeholderIn')" :rows="2"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.sourceId')" name="sourceId">
            <Input v-model:value="form.sourceId" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.consumptionRecord.creatorUuid')" name="creatorUuid">
            <Input v-model:value="form.creatorUuid" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
      </Row>
      <FormItem style="text-align: right">
        <Button @click="_close" :disabled="data.loading">{{ $t('common.cancel') }}</Button>
        <Button danger style="margin-left: 10px" @click="handleResetForm" :disabled="data.loading">
          {{ $t('common.reset') }}
        </Button>
        <Button type="primary" style="margin-left: 10px" @click="handleSubmit" :loading="data.loading"
                :disabled="data.loading">{{ $t('common.confirm') }}
        </Button>
      </FormItem>
    </Form>
  </Modal>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { $t } from '@vben/locales';
import { Modal, Form, Input, Select, Row, Col, Button, message, DatePicker, InputNumber } from 'ant-design-vue';
import { robotConsumptionRecord } from '#/api';

const FormItem = Form.Item;
const SelectOption = Select.Option;
const TextArea = Input.TextArea;

interface Emits {
  (e: 'close', submit: Boolean): void
}

const emits = defineEmits<Emits>()

const data = reactive({
  isShowModal: false,
  loading: false,
  consumptionMethodOptions: [
    { label: $t('robot.consumptionRecord.consumptionMethodOptions.wechat'), value: '微信支付' },
    { label: $t('robot.consumptionRecord.consumptionMethodOptions.alipay'), value: '支付宝' },
    { label: $t('robot.consumptionRecord.consumptionMethodOptions.bankCard'), value: '银行卡' },
    { label: $t('robot.consumptionRecord.consumptionMethodOptions.cash'), value: '现金' },
    { label: $t('robot.consumptionRecord.consumptionMethodOptions.other'), value: '其他' }
  ]
})

const form = reactive({
  robotUuid: '',
  robotAccount: '',
  consumptionAmount: undefined,
  consumptionTime: '',
  consumptionMethod: undefined,
  consumptionInfo: '',
  notes: '',
  sourceId: '',
  creator: '',
  creatorUuid: ''
})

// 选择框过滤方法
const selectFilterOption = (input: string, option: any) => {
  return option.text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
}

// 表单规则
const rules = reactive({
  robotUuid: [{ required: true, message: $t('robot.consumptionRecord.robotUuidRequired') }],
  robotAccount: [{ required: true, message: $t('robot.consumptionRecord.robotAccountRequired') }],
  consumptionAmount: [{ required: true, message: $t('robot.consumptionRecord.consumptionAmountRequired') }],
  consumptionTime: [{ required: true, message: $t('robot.consumptionRecord.consumptionTimeRequired') }],
  consumptionMethod: [{ required: true, message: $t('robot.consumptionRecord.consumptionMethodRequired') }],
  consumptionInfo: [{ required: true, message: $t('robot.consumptionRecord.consumptionInfoRequired') }],
  creator: [{ required: true, message: $t('robot.consumptionRecord.creatorRequired') }]
})

const formRef = ref();

function handleSubmit() {
  data.loading = true;
  formRef.value.validate().then(() => {
    robotConsumptionRecord.create(form).then((res) => {
      if (res.error) {
        return;
      }
      message?.success($t('common.addSuccess'));
      _close(true);
    })
  }).catch(() => {
    // 验证失败
  }).finally(() => {
    data.loading = false;
  })
}

function handleResetForm() {
  formRef.value.resetFields();
}

function _close(submit = false) {
  data.isShowModal = false;
  formRef.value.resetFields();
  emits('close', submit);
}

defineExpose({
  _open() {
    data.isShowModal = true;
  }
})
</script>