<template>
  <div class="m-4 modals">
    <div>
      <XTTitle title="水处理设备" :right-data="titleData" @click="callTitleClick" />
      <div class="flex items-center justify-between my-4">
        <div />
        <XTForm :form-data="formData" @change="callForm" />
      </div>
    </div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'purchaseDate'">
          <span> {{ dayjs(record.purchaseDate).format('YYYY-MM-DD') }}</span>
        </template>
        <template v-if="column.key === 'useDate'">
          <span> {{ record.useDate ? dayjs(record.useDate).format('YYYY-MM-DD') : '' }}</span>
        </template>
        <template v-if="column.key === 'produceDate'">
          <span> {{ dayjs(record.produceDate).format('YYYY-MM-DD') }}</span>
        </template>
        <template v-if="column.key === 'name'">
          <a @click="goDetail(record)">{{ record.name }}</a>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'biz:consumable:edit',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '维修',
                auth: 'biz:consumable:edit',
                tooltip: '维修',
                onClick: handleMaintenance.bind(null, record),
              },
              {
                label: '保养',
                auth: 'biz:consumable:edit',
                tooltip: '保养',
                onClick: handleUpkeep.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="registerModal" @success="callSuccess" @cancel="handleCancel" />
    <MaintenanceFormModal
      @register="registerMaintenanceModal"
      @success="callSuccess"
      @cancel="handleCancel"
    />
    <UpkeepFormModal
      @register="registerUpkeepModal"
      @success="callSuccess"
      @cancel="handleCancel"
    />
  </div>
</template>
<script lang="ts" setup>
  import { onBeforeMount, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/TableCard';
  import FormModal from './formModal.vue';
  import MaintenanceFormModal from './maintenanceFormModal.vue';
  import UpkeepFormModal from './upkeepFormModal.vue';
  import { columns } from './data';
  import { waterQueryPage } from '/@/api/biz/engineer/waterApi';
  import { listDictModel } from '/@/api/common';
  import { useModal } from '/@/components/Modal';
  import { XTTitle } from '/@/components/XTTitle/index';
  import { XTForm } from '/@/components/XTForm/index';
  import { useRouter } from 'vue-router';
  import dayjs from 'dayjs';

  // 路由跳转
  const router = useRouter();
  // 标题数据
  const titleData = [
    {
      type: 'add',
      btnIcon: 'icon-xt-add_default',
      auth: ['biz:consumable:add'],
      btnText: '新增设备',
    },
  ];
  // formdata
  const formData = [
    {
      name: 'searchNames',
      componentType: 'Input',
      prefix: 'icon-xt-search',
      placeholder: '请输入设备编号',
      width: 240,
    },
  ];
  // 操作名称
  const searchNames = ref('');
  const shiftDate = ref([]);

  const responsesupplierCategoryOptions = ref();
  onBeforeMount(async () => {
    responsesupplierCategoryOptions.value = await listDictModel({ dictCode: 'pht' });
  });

  const [registerModal, { openModal }] = useModal();
  const [registerMaintenanceModal, { openModal: openMaintenanceModal }] = useModal();
  const [registerUpkeepModal, { openModal: openUpkeepModal }] = useModal();
  const tableSort = ref([
    {
      field: 'create_time',
      direction: 'DESC',
    },
  ]) as any;

  const [registerTable, { reload, clearSelectedRowKeys }] = useTable({
    api: waterQueryPage,
    rowKey: 'id',
    columns,
    showIndexColumn: true,
    bordered: true,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
    },
    beforeFetch: handleBeforeFetch,
    sortFn: handleSortFn,
  });
  // 详情按钮事件
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  // 新增按钮事件
  function callTitleClick(data) {
    if (data.type == 'add') {
      openModal(true, {
        isUpdate: false,
        record: data,
      });
    }
  }

  // 表格点击字段排序
  function handleSortFn(sortInfo) {
    if (sortInfo?.order && sortInfo?.columnKey) {
      // 默认单列排序
      tableSort.value = [
        {
          field: sortInfo.columnKey,
          direction: sortInfo.order.replace(/(\w+)(end)/g, '$1').toUpperCase(),
        },
      ];
    }
  }
  // 打开维修弹框
  function handleMaintenance(record) {
    openMaintenanceModal(true, {
      record,
    });
  }
  // 打开保养弹框
  function handleUpkeep(record) {
    openUpkeepModal(true, {
      record,
    });
  }

  // 表格请求之前，对参数进行处理, 添加默认 排序
  async function handleBeforeFetch(params) {
    return {
      ...params,
      orders: tableSort.value,
      uniqueCode: searchNames.value == '' ? undefined : searchNames.value,
    };
  }

  //取消按钮事件
  async function handleCancel() {
    clearSelectedRowKeys();
    await reload();
  }

  // 弹窗回调事件
  async function callSuccess({ isUpdate, values }) {
    console.log(isUpdate);
    console.log(values);
    await reload();
  }

  // 查询组件回调
  async function callForm(data) {
    shiftDate.value = data.shiftDate ? data.shiftDate : '';
    searchNames.value = data.searchNames ? data.searchNames : '';
    await reload();
  }

  // 打开详情页面
  function goDetail(record) {
    console.log('record::', record);
    router.push({
      path: '/bizEngineer/otherDetail',
      query: {
        id: record.id,
        name: record.name,
      },
    });
  }
</script>
<style lang="less" scoped>
  .table-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    margin-right: 6px;
    border-radius: 50%;
  }

  ::v-deep(.ant-input-prefix) {
    color: #8a99ac;
  }

  .colUpdateAvatar {
    display: flex;
    justify-content: center;
    text-align: center;
    line-height: 28px;
  }

  .colImg {
    width: 28px;
    height: 28px;
    margin-right: 5px;
  }
</style>
