<template>
  <div class="g-p20 main g-h">
    <base-search @on-search="search" @reset="reset"></base-search>
    <div class="content g-br-10 g-bg-fff g-p20">
      <el-table
        :data="allData.items"
        v-loading="allData.loading"
        header-cell-class-name="table-head"
        row-class-name="table-row"
        style="width: 100%"
      >
        <el-table-column prop="date" label="部件图片" width="150">
          <template #default="{ row }">
            <el-image
              :preview-src-list="[row.imageSource]"
              :initial-index="1"
              hide-on-click-modal
              :preview-teleported="true"
              style="width: 85px; height: 85px; border-radius: 15px"
              :src="row.imageSource"
              fit="contain"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="部件名称" width="300">
          <template #default="{ row }">
            <div class="second">
              {{ row.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="部件寿命">
          <template #default="{ row }">
            <div class="progress">
              <div style="color: #7d7d86">{{ row.describe }}</div>
              <div>
                <el-progress
                  :percentage="row.scale"
                  :status="row.status"
                  :color="row.scale > 20 ? '#55B758' : row.scale > 10 && row.scale < 20 ? '#D5A17D' : '#ED6159'"
                />
              </div>
              <el-button
                :loading="putEquipment.loading && row.id === lifeId"
                v-if="row.canChange"
                @click="putEquipmentHandle(row)"
                >更换</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pro-pagination
        v-if="allData.data.totalCount"
        class="pagination"
        :total="allData.data.totalCount"
        :pageObj="pageObj"
      ></pro-pagination>
    </div>
  </div>
</template>

<script setup lang="ts" name="部件寿命">
  import { getEquipmentLifes, putEquipmentChange } from '@/api'
  import { ElMessage, ElMessageBox } from 'element-plus'

  const pageObj = reactive({
    MaxResultCount: 10,
    SkipCount: 1
  })
  // onMounted(async () => {
  //

  //   await allData.getData()
  //

  //   // allData.items.forEach((element: any) => {
  //   //   element.isCheck = false
  //   // })
  // })

  let iptValue = ref('')
  const allData = useRequest(() =>
    getEquipmentLifes({
      MaxResultCount: pageObj.MaxResultCount,
      SkipCount: (pageObj.SkipCount - 1) * pageObj.MaxResultCount,
      DeviceId: useQuery('id'),
      KeyWord: iptValue.value
    })
  )

  const lifeId = ref('')
  const putEquipment = useRequest(() => putEquipmentChange({ lifeId: lifeId.value }), false)
  const putEquipmentHandle = (item: any) => {
    ElMessageBox.confirm(`确定更换 ${item.name}吗?`, '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      lifeId.value = item.id
      putEquipment.getData()
      ElMessage.success('操作成功')
    })
  }
  watchEffect(() => {
    allData?.getData()
  })
  const search = (v: any) => {
    iptValue.value = v
    if (!v) {
      allData?.getData()
    }
  }
  const reset = () => {
    iptValue.value = ''
  }
</script>

<style lang="less" scoped>
  .main {
    background: #c8cfdf;
    margin-bottom: 40px;
  }
  .content {
    min-height: calc(100vh - 200px);
    padding-bottom: 100px;
    position: relative;
    .first {
      width: 63px;
      height: 20px;
      background: #a6a9af;
      border-bottom-left-radius: 10px;
      border-top-right-radius: 10px;
      font-size: 12px;
      color: #fff;
      text-align: center;
    }
    .second {
      font-weight: bold;
    }
    .remark {
      border-right: 1px solid #efefef;
      padding-right: 15px;
      font-weight: bold;
    }
    .three {
      color: #7d7d86;
    }
    .progress {
      display: flex;
      align-items: center;
      .el-button {
        width: 80px;
        height: 26px;
        line-height: 26px;
        border: 1px solid #597fda;
        color: #597fda;
        border-radius: 10px;
        left: 10px;
        top: 0%;
        margin-left: 100px;
      }
    }
    :deep(.progress) {
      .el-progress {
        width: 500px;
        margin: 0 20px;
        padding-left: 120px;
      }
      .el-progress__text {
        font-weight: bold;
        font-size: 30px !important;
        position: absolute;
        left: 0;
      }
    }

    .pagination {
      position: absolute;
      bottom: 20px;
      right: 30px;
    }
  }
  :deep(.content) {
    .table-head {
      background: #f6f6f6;
      .cell {
        color: #333;
      }
    }
    .table-head {
      height: 40px;
      font-size: 12px;
    }
    .table-row {
      color: #333;
      .el-table__cell {
        padding: 16px 0;
      }
    }
    .el-icon {
      width: 20px;
      height: 20px;
      font-size: 16px;
      font-weight: bold;
    }
    .fs-16 {
      font-size: 18px;
    }
  }
</style>
