<template>
  <PageWrapper>
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm
        @register="register"
        @submit="handleSubmit"
        @reset="handleReset"
        @advanced="formAdvanced"
      >
        <template #statusSlot="{ model, field }">
          <el-input v-model="model[field]" placeholder="请输入" />
        </template>
      </BasicForm>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        :actionColumn="actionColumn"
        @checked-row-change="onCheckedRow"
        :scroll-x="1090"
      >
        <template #tableTitle>
          <el-button type="primary" @click="addTable">
            <template #icon>
              <el-icon class="el-input__icon">
                <PlusOutlined />
              </el-icon>
            </template>
            新建
          </el-button>
        </template>

        <template #toolbar>
          <el-button type="primary" @click="reloadTable">刷新数据</el-button>
        </template>
      </BasicTable>
    </el-card>
    <el-dialog v-model="showModal" title="新建" width="30%" draggable>
      <el-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="80"
        class="py-4"
      >
        <el-form-item label="名称" prop="name">
          <el-input placeholder="请输入名称" v-model="formParams.name" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" placeholder="请输入地址" v-model="formParams.address" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker type="datetime" placeholder="请选择日期" v-model="formParams.date" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-space>
          <el-button @click="() => (showModal = false)">取消</el-button>
          <el-button type="primary" :loading="formBtnLoading" @click="confirmForm">确定</el-button>
        </el-space>
      </template>
    </el-dialog>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { ColProps, ElMessage, FormRules } from 'element-plus';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, useForm, FormSchema } from '@/components/Form/index';
  import { getTableList } from '@/api/table/list';
  import { columns } from './columns';
  import { PlusOutlined, DeleteOutlined, FormOutlined } from '@vicons/antd';
  import { useRoute } from 'vue-router';
  import { renderIcon } from '@/utils';

  const rules: FormRules = {
    name: [
      {
        required: true,
        trigger: 'blur',
        message: '请输入名称',
      },
    ],
    address: [
      {
        required: true,
        trigger: 'blur',
        message: '请输入地址',
      },
    ],
    date: [
      {
        required: true,
        trigger: ['blur', 'change'],
        message: '请选择日期',
      },
    ],
  };

  const schemas: FormSchema[] = [
    {
      field: 'name',
      labelMessage: '这是一个提示',
      component: 'Input',
      label: '姓名',
      componentProps: {
        placeholder: '请输入姓名',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      // rules: [{ required: true, message: '请输入姓名', trigger: ['blur'] }],
    },
    {
      field: 'mobile',
      component: 'Input',
      label: '手机',
      componentProps: {
        placeholder: '请输入手机号码',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'type',
      component: 'Select',
      label: '类型',
      componentProps: {
        placeholder: '请选择类型',
        options: [
          {
            label: '舒适性',
            value: 1,
          },
          {
            label: '经济性',
            value: 2,
          },
        ],
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeDate',
      component: 'DatePicker',
      label: '预约时间',
      defaultValue: 1183135260000,
      componentProps: {
        type: 'date',
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeTime',
      component: 'TimePicker',
      label: '停留时间',
      componentProps: {
        clearable: true,
        onChange: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'status',
      label: '状态',
      //插槽
      slot: 'statusSlot',
    },
  ];

  const route = useRoute();
  const formRef: any = ref(null);

  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    name: '',
    address: '',
    date: '',
  });

  const params = ref({
    name: '',
    id: route.params.id,
  });

  const actionColumn = reactive({
    width: 300,
    label: '操作',
    prop: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '删除',
            type: 'danger',
            size: 'default',
            icon: renderIcon(DeleteOutlined),
            //Popconfirm 气泡确认框 配置
            isConfirm: true,
            popConfirm: {
              onConfirm: handleDelete.bind(null, record),
              onCancel: handleNegative.bind(null, record),
              title: '您真的，确定要删除吗？',
              confirmButtonText: '确定',
              cancelButtonText: '取消',
            },
          },
          {
            label: '编辑',
            type: 'warning',
            size: 'default',
            icon: renderIcon(FormOutlined),
            onClick: handleEdit.bind(null, record),
          },
        ],
        dropDownProps: {
          label: '更多',
          type: 'primary',
          size: 'default',
          icon: renderIcon(DeleteOutlined),
          // iconPlacement: 'left',
        },
        dropDownActions: [
          {
            label: '启用',
            size: 'medium',
            key: 'enabled',
            // 根据业务控制是否显示: 非enable状态的不显示启用按钮
            ifShow: () => {
              return true;
            },
          },
          {
            label: '禁用',
            key: 'disabled',
            ifShow: () => {
              return true;
            },
          },
        ],
        select: (key) => {
          ElMessage.success(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  const [register, {}] = useForm({
    layout: 'horizontal',
    colProps: { span: 6 } as ColProps,
    schemas,
  });

  function formAdvanced(status) {
    console.log(status);
    actionRef.value.redoHeight();
  }

  function addTable() {
    showModal.value = true;
  }

  const loadDataTable = async (res) => {
    return await getTableList({ ...formParams, ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys.value);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function confirmForm() {
    if (!formRef.value) return;
    formBtnLoading.value = true;
    formRef.value.validate((valid) => {
      if (valid) {
        ElMessage.success('新建成功');
        setTimeout(() => {
          showModal.value = false;
          reloadTable();
        });
      } else {
        formBtnLoading.value = false;
        ElMessage.error('请填写完整信息');
      }
    });
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    ElMessage.success('点击了编辑');
    //router.push({ name: 'BasicInfo', params: { id: record.id } });
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
    ElMessage.success('点击了删除');
  }

  function handleNegative(record: Recordable) {
    console.log('点击了取消', record);
    ElMessage.success('点击了取消');
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    params.value = Object.assign(formParams, values) as any;
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="scss" scoped></style>
