<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="router.back()" v-auth="'dkdc-tbz-zltb'">返回</a-button>
        <a-button type="primary" @click="handleAddAndEdit({})" v-auth="'dkdc-tbz-zltb'">采样</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
  <cyModal @register="registerModal" @sumbitTable="sumbitTable" />
</template>

<script lang="ts" name="xcjd-Sampling-cyList" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useRouter, useRoute } from 'vue-router';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { lookDatacolumns } from './data';
  import { getCyxxList, SetDel, setAdd, addSuccess } from './api';
  import { useModal } from '/@/components/Modal';
  import cyModal from './cyModal.vue';
  import { ref } from 'vue';
  //注册model
  const [registerModal, { openModal, closeModal }] = useModal();
  // let isEdit = ref(true)
  let router = useRouter();
  let route = useRoute();
  const { createMessage } = useMessage();

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '现场监督-现场采样-采样样点',
      api: getCyxxList,
      columns: lookDatacolumns,
      canResize: false,
      rowKey: 'id',
      useSearchForm: false,
      showIndexColumn: true,
      actionColumn: {
        width: 150,
        fixed: 'right',
      },
      beforeFetch(params) {
        if (route.query.xccyId) {
          params.xccyId = route.query.xccyId;
        } else {
          createMessage.error('内容错误，请返回重试');
        }
      },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  //删除事件
  async function handleDelete(record) {
    await SetDel({ id: record.id });
    await addSuccess({ xccyId: record.xccyId });
    reload();
  }
  //查看
  function handleLook(record) {
    router.push(`/xcjd/xccy/cyList/look?ydxxId=${record.id}`);
  }
  //新增*编辑事件
  function handleAddAndEdit(record) {
    openModal(true, {
      ...record,
      xccyId: route.query.xccyId,
      ydlx: route.query.ydlx,
    });
    // router.push(`/xcjd/Supervise/LookData/form?wrdkid=${route.query.wrdkid}`)
  }
  // 提交事件
  async function sumbitTable(val) {
    // 获取modal传参 保存到表格里面
    val.smtdmc = '四面图(东)';
    val.smtnmc = '四面图(南)';
    val.smtxmc = '四面图(西)';
    val.smtbmc = '四面图(北)';
    val.xchczptmc = '现场核查人员自拍图';
    val.gpszbtmc = 'GPS设备坐标图';
    val.yxtmc = '岩芯图';

    await setAdd(val);
    if (val.status == 1) {
      await addSuccess({ xccyId: val.xccyId });
    }
    closeModal();
    reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    // 判断数据来源显示按钮 没有找到控制字段 待优化
    if (record.status == 0) {
      return [
        {
          label: '删除',
          popConfirm: {
            title: '是否确认删除',
            confirm: handleDelete.bind(null, record),
          },
        },
        {
          label: '编辑',
          onClick: handleAddAndEdit.bind(null, record),
        },
        {
          label: '查看',
          onClick: handleLook.bind(null, record),
        },
      ];
    } else if (record.status == 1) {
      return [
        {
          label: '删除',
          popConfirm: {
            title: '是否确认删除',
            confirm: handleDelete.bind(null, record),
          },
        },
        {
          label: '查看',
          onClick: handleLook.bind(null, record),
        },
      ];
    }
  }
</script>
<style scoped></style>
