<template>
  <div class="ele-body">
    <a-card :bordered="false" style="margin-top: 10px">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="securityId"
        :columns="columns"
        :datasource="datasource"
        :customRow="customRow"
        :expand-icon-column-index="1"
        :scroll="{ x: 1200 }"
        cache-key="towerModel"
      >
        <template #toolbar>
          <a-space>
            <!--            <a-button type="primary" class="ele-btn-icon" @click="openEdit()">-->
            <!--              <template #icon>-->
            <!--                <plus-outlined />-->
            <!--              </template>-->
            <!--              <span>新建</span>-->
            <!--            </a-button>-->
            <a-radio-group v-model:value="searchStatus" @change="onStatus">
              <a-radio-button value="0">正常</a-radio-button>
              <a-radio-button value="1">待修</a-radio-button>
              <a-radio-button value="2">异常已修</a-radio-button>
              <a-radio-button value="3">异常未修</a-radio-button>
            </a-radio-group>
            <a-range-picker
              v-model:value="dateRange"
              @change="onRange"
              value-format="YYYY-MM-DD"
              class="ele-fluid"
            />
            <!-- 搜索表单 -->
            <a-input-search
              allow-clear
              v-model:value="searchText"
              placeholder="请输入搜索关键词"
              @search="search"
              @pressEnter="search"
            />
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'status'">
            <a-tag v-if="record.status == 0">正常</a-tag>
            <a-tag v-if="record.status == 1" color="red">待修</a-tag>
            <a-tag v-if="record.status == 2" color="green">异常已修</a-tag>
            <a-tag v-if="record.status == 3" color="orange">异常未修</a-tag>
          </template>
          <template v-if="column.key === 'action'">
            <a-space>
              <!--              <a @click="openRecord(record)">保养记录</a>-->
              <!--              <a-divider type="vertical" />-->
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="topRight"
                title="确定要删除此记录吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <SecurityEdit v-model:visible="showEdit" :data="current" @done="reload" />
    <!-- 保养记录 -->
    <SecurityRecord
      v-model:visible="showRecord"
      :data="current"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading, toDateString } from 'ele-admin-pro/es';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type { Security, SecurityParam } from '@/api/tower/security/model';
  import { pageSecurity, removeSecurity } from '@/api/tower/security';
  import SecurityEdit from './components/security-edit.vue';
  import SecurityRecord from './components/security-record.vue';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    // {
    //   key: 'index',
    //   width: 48,
    //   align: 'center',
    //   fixed: 'left',
    //   hideInSetting: true,
    //   customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    // },
    // {
    //   title: 'ID',
    //   dataIndex: 'securityId',
    //   key: 'securityId'
    // },
    {
      title: '项目名称',
      dataIndex: 'projectName',
      key: 'projectName'
    },
    {
      title: '保养编号',
      dataIndex: 'securityCode',
      key: 'securityCode'
    },
    {
      title: '所在地',
      dataIndex: 'address',
      key: 'address',
      showSorterTooltip: false,
      ellipsis: true
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      width: 180
    },
    {
      title: '生成时间',
      dataIndex: 'createTime',
      width: 180,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center'
    }
  ]);

  // 当前编辑数据
  const current = ref<Security | null>(null);
  const searchText = ref('');
  const searchStatus = ref('');
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  const showRecord = ref(false);

  // 日期范围选择
  const dateRange = ref<[string, string]>(['', '']);
  /* 搜索 */
  const onRange = () => {
    const [d1, d2] = dateRange.value ?? [];
    reload({
      createTimeStart: d1 ? d1 + ' 00:00:00' : '',
      createTimeEnd: d2 ? d2 + ' 23:59:59' : ''
    });
  };

  // 表格数据源
  const datasource: DatasourceFunction = ({ where }) => {
    if (searchText.value && searchText.value != '') {
      where.keywords = searchText.value;
    }
    where.type = 1;
    return pageSecurity({ ...where });
  };

  /* 刷新表格 */
  const reload = (where?: SecurityParam) => {
    tableRef?.value?.reload({ where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Security | null, id?: number) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  const openRecord = (row?: Security, id?: number) => {
    current.value = row ?? null;
    showRecord.value = true;
  };

  const search = (searchText) => {
    reload({ keywords: searchText });
  };

  const onStatus = (e: any) => {
    console.log(e.target.value);
    const status = e.target.value;
    reload({ status });
  };

  /* 删除单个 */
  const remove = (row: Security) => {
    const hide = messageLoading('请求中..', 0);
    removeSecurity(row.securityId)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 自定义行属性 */
  const customRow = (record: Security) => {
    return {
      // 行点击事件
      onClick: () => {
        // console.log(record);
      },
      // 行双击事件
      onDblclick: () => {
        openEdit(record);
      }
    };
  };
</script>

<script lang="ts">
  export default {
    name: 'TowerSecurity'
  };
</script>
<style lang="less" scoped>
  .count {
    display: flex;
    .btn {
      margin: 0 12px;
      padding: 6px 18px;
      background-color: #f3f3f3;
      border-radius: 4px;
      cursor: pointer;
      display: flex;
      align-items: center;
      font-size: 16px;
      .divider {
        width: 4px;
        height: 15px;
        border-radius: 8px;
        margin-right: 8px;
        background-color: #16a90b;
      }
      .active {
        color: #16a90b;
      }
    }
  }
</style>
