<template>
  <Modal :maskClosable="false" :keyboard="false" v-model:open="data.isShowModal" :footer="null" 
         :title="$t('robot.runRecord.editTitle')" 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.runRecord.robotUuid')" name="robotUuid">
            <Input v-model:value="form.robotUuid" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.robotAccount')" name="robotAccount">
            <Input v-model:value="form.robotAccount" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.configType')" name="configType">
            <Select allow-clear showSearch :filterOption="selectFilterOption" v-model:value="form.configType" 
                    allowClear :placeholder="$t('common.placeholderIn')">
              <SelectOption v-for="(item, index) in data.configTypeOptions" :key="index" :value="item.value" 
                            :label="item.label" :text="item.label">{{ item.label }}
              </SelectOption>
            </Select>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.configName')" name="configName">
            <Input v-model:value="form.configName" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.configUuid')" name="configUuid">
            <Input v-model:value="form.configUuid" allowClear :placeholder="$t('common.placeholderIn')"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.executeStatus')" name="executeStatus">
            <Select allow-clear showSearch :filterOption="selectFilterOption" v-model:value="form.executeStatus" 
                    allowClear :placeholder="$t('common.placeholderIn')">
              <SelectOption v-for="(item, index) in data.executeStatusOptions" :key="index" :value="item.value" 
                            :label="item.label" :text="item.label">{{ item.label }}
              </SelectOption>
            </Select>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.executeStartTime')" name="executeStartTime">
            <DatePicker v-model:value="form.executeStartTime"  showTime format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" 
                       :placeholder="$t('common.placeholderIn')" style="width: 100%"/>
          </FormItem>
        </Col>
        <Col :span="12">
          <FormItem :label="$t('robot.runRecord.executeEndTime')" name="executeEndTime">
            <DatePicker v-model:value="form.executeEndTime"  showTime format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" 
                       :placeholder="$t('common.placeholderIn')" style="width: 100%"/>
          </FormItem>
        </Col>
        <Col :span="24">
          <FormItem :label-col="{ span: 3 }" :label="$t('robot.runRecord.executeDesc')" name="executeDesc">
            <TextArea v-model:value="form.executeDesc" allowClear :placeholder="$t('common.placeholderIn')" :rows="3"/>
          </FormItem>
        </Col>
        <Col :span="24">
          <FormItem :label-col="{ span: 3 }" :label="$t('robot.runRecord.executeLog')" name="executeLog">
            <TextArea v-model:value="form.executeLog" allowClear :placeholder="$t('common.placeholderIn')" :rows="4"/>
          </FormItem>
        </Col>
        <Col :span="24">
          <FormItem :label-col="{ span: 3 }" :label="$t('robot.runRecord.notes')" name="notes">
            <TextArea v-model:value="form.notes" allowClear :placeholder="$t('common.placeholderIn')" :rows="3"/>
          </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 } from 'ant-design-vue';
import { robotRunRecord } 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,
  configTypeOptions: [
    { label: $t('robot.runRecord.configTypeOptions.v1'), value: 'v1' },
    { label: $t('robot.runRecord.configTypeOptions.v2'), value: 'v2' },
    { label: $t('robot.runRecord.configTypeOptions.v3'), value: 'v3' }
  ],
  executeStatusOptions: [
    { label: $t('robot.runRecord.executeStatusOptions.v0'), value: 0 },
    { label: $t('robot.runRecord.executeStatusOptions.v1'), value: 1 },
    { label: $t('robot.runRecord.executeStatusOptions.v2'), value: 2 },
    { label: $t('robot.runRecord.executeStatusOptions.v3'), value: 3 },
    { label: $t('robot.runRecord.executeStatusOptions.v4'), value: 4 }
  ]
})

const form = reactive({
  uuid: '',
  robotUuid: '',
  robotAccount: '',
  configType: undefined,
  configName: '',
  configUuid: '',
  notes: '',
  executeStartTime: null,
  executeEndTime: null,
  executeStatus: undefined,
  executeDesc: '',
  executeLog: ''
})

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

// 表单规则
const rules = reactive({
  robotUuid: [{ required: true, message: $t('form.required') }],
  robotAccount: [{ required: true, message: $t('form.required') }],
  configType: [{ required: true, message: $t('form.required') }],
  configName: [{ required: true, message: $t('form.required') }],
  executeStatus: [{ required: true, message: $t('form.required') }]
})

const formRef = ref();

function handleSubmit() {
  data.loading = true;
  formRef.value.validate().then(() => {
    robotRunRecord.updateByUid(form).then((res) => {
      if (res.error) {
        return;
      }
      message?.success($t('common.editSuccess'));
      _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);
}

function _open(record: any) {
  data.isShowModal = true;
  Object.assign(form, record);
}

defineExpose({
  _open
})
</script>