<template>
  <div class="storage-info-wrap">
    <div class="left-wrap">
      <EdfsWrap class="chart-box" title="设备库存概览">
        <PieChart :data="chartData" />
      </EdfsWrap>
      <div class="filter">
        <LeftFilter @search="onSearch" :customerList="customerList" />
      </div>
    </div>
    <EdfsWrap title="设备库存列表" class="storage-info-table">
      <EdfsTable
        class="table"
        v-loading="loading"
        :data="list"
        ref="tableRef"
        :highlight-current-row="true"
        :page-total="total"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        row-class-name="row"
        @pageCurrentChange="handleJump"
      >
        <template v-for="(col, idx) in tableCol" :key="idx">
          <el-table-column
            v-if="col.prop.endsWith('Time')"
            :label="storageStatus + col.label"
            :min-width="col.minWidth"
          >
            <template #default="scope">
              {{ dayjs(scope.row[col.prop]).format('YYYY-MM-DD HH:mm:ss') }}
            </template>
          </el-table-column>

          <el-table-column
            v-if="col.prop === 'status'"
            :label="col.label"
            :min-width="col.minWidth"
          >
            <template #default="scope">
              {{
                statusList.find(item => item.value === scope.row[col.prop])?.label ?? ''
              }}
            </template>
          </el-table-column>
          <el-table-column
            v-else
            :prop="col.prop"
            :label="col.label"
            :min-width="col.minWidth"
          />
        </template>

        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <EdfsButton
              link
              type="success"
              inner-text="添加维修信息"
              v-if="scope.row.status === 3"
              @click="addMaintain(scope.row)"
            />
            <EdfsButton
              link
              type="success"
              v-else-if="scope.row.status === 2"
              inner-text="添加出库信息"
              @click="addOutStorage(scope.row)"
            />
          </template>
        </el-table-column>
      </EdfsTable>
    </EdfsWrap>
  </div>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import EdfsWrap from '@/components/dashboard/Edfs-wrap.vue'
import EdfsTable from '@/components/dashboard/Edfs-table/index.vue'
import { statusList, tableCol } from './utils'
import { isResError } from '@/hooks/useMessage'
import LeftFilter from './components/data-filter.vue'
import { getStoragePage, getStorageStatics } from '@/api/module/eam/device/storage'
import { useRouter } from 'vue-router'
import PieChart from './components/pie-chart.vue'
import { getSimpleCustomerList, type CustomerVO } from '@/api/module/eam/customer'

const router = useRouter()
const loading = ref(true)
const total = ref(0)
const list = ref<any>([])
const queryParams = reactive({
  pageNo: 1,
  pageSize: undefined,
})
const tableRef = ref<any>(null)
const getList = async () => {
  if (!queryParams.pageSize) {
    queryParams.pageSize = tableRef.value.getSize()
  }
  loading.value = true

  const res = await getStoragePage(Object.assign({}, queryParams, filter.value))

  if (!isResError(res)) {
    list.value = res.data.list
    total.value = res.data.total
  }

  loading.value = false
}

const storageStatus = computed(
  () => statusList.find(item => item.value === filter.value.status)?.colName ?? ''
)

const filter = ref<any>({})
function onSearch(search: any) {
  queryParams.pageNo = 1
  filter.value = search
  getList()
}

function handleJump(page: number) {
  queryParams.pageNo = page
  getList()
}

function addMaintain(row: any) {
  router.push({
    path: '/device/deviceOperation',
    query: { action: 'update', type: 'maintain', id: row.deviceId },
  })
}

function addOutStorage(row: any) {
  router.push({
    path: '/device/deviceOperation',
    query: { action: 'update', type: 'delivery', id: row.deviceId },
  })
}

const customerList = ref<Array<Pick<CustomerVO, 'id' | 'name'>>>([])
async function loadCustomerList() {
  const res = await getSimpleCustomerList()
  if (isResError(res)) return
  customerList.value = res.data
}

const chartData = ref([]) as Ref<any>
async function loadChartData() {
  const res = await getStorageStatics()
  if (!isResError(res)) {
    chartData.value = res.data
  }
}
onMounted(() => {
  loadCustomerList()
  loadChartData()
})
</script>

<style scoped lang="scss">
.storage-info-wrap {
  width: 100%;
  height: 100%;
  display: flex;
  column-gap: 12px;
  box-sizing: border-box;
  font-size: 16px;
  .left-wrap {
    width: 280px;
    min-width: 100px;
    height: 100%;
    background: var(--warp-bg);
    display: flex;
    flex-direction: column;

    box-sizing: border-box;
    .station {
      position: relative;
      height: 60px;
      padding-left: 20px;
      border-bottom: 1px solid var(--pagination-border-color);
      :deep(.el-input__inner) {
        color: #666;
      }

      :deep(.el-input__wrapper) {
        padding: 0;
      }

      :deep(.el-input__suffix) {
        display: none;
      }
    }
    .chart-box {
      height: 300px;
    }
    .filter {
      height: calc(100% - 300px);
      overflow: hidden;
    }
    :deep(.edfs-wrap) {
      box-shadow: none;
    }
  }

  .storage-info-table {
    flex: 1;
    .table {
      height: 100%;
    }
  }
}
</style>
