<template>
  <div class="g-p20 life">
    <div class="g-bg"></div>
    <div class="g-fx">
      <pro-select type="1" placeholder="全部公司" :selectValue="selectValue"></pro-select>
      <base-search @on-search="search" class="g-ml-20" @reset="reset"></base-search>
    </div>
    <div class="g-card g-p30 g-pb-50">
      <div class="table">
        <el-table
          v-loading="allData.loading"
          header-cell-class-name="table-head"
          row-class-name="table-row"
          tooltip-effect
          :data="allData.items"
        >
          <el-table-column type="expand" column-key="id">
            <template #default="{ row }">
              <div class="placeholder-box">
                <div class="placeholder-box__item" v-for="(item, index) in row.equipmentLifes" :key="index">
                  <div class="item-line"></div>
                  <div class="item-line2" :class="{ 'last-line2': index == row.equipmentLifes.length - 1 }"></div>
                  <div class="g-fx">
                    <div class="item-name">{{ item.name }}</div>
                    <div class="item-life">{{ item.describe }}</div>
                    <div class="item-name fs-16">{{ item.scale }}%</div>
                    <el-progress
                      style="width: 450px; margin-left: 50px"
                      :show-text="false"
                      :percentage="item.scale"
                      :color="item.scale > 20 ? '#55B758' : item.scale > 10 && item.scale < 20 ? '#D5A17D' : '#ED6159'"
                    />
                    <el-button v-if="item.canChange" class="item-btn" plain type="primary" @click="onCheck(item)"
                      >更换</el-button
                    >
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="250" label="所属公司">
            <template #default="{ row }">
              <div class="ml-15">{{ row.companyName }}</div>
            </template>
          </el-table-column>
          <el-table-column width="200" label="设备名称">
            <template #default="{ row }">
              <div>
                <div>{{ row.deviceName }}</div>
                <div v-show="row.isCheck" class="placeholder-box">0000000</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="设备编号">
            <template #default="{ row }">
              <div>
                <div>{{ row.deviceCode }}</div>
                <div v-show="row.isCheck" class="placeholder-box">0000000</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="420" label="班组">
            <template #default="{ row }">
              <div>
                <div>{{ row.workshopName }}</div>
                <div v-show="row.isCheck" class="placeholder-box">0000000</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="100" label="部件数量 ">
            <template #default="{ row }">
              <div class="item-num">{{ row.partsCount }}</div>
            </template>
          </el-table-column>
          <el-table-column width="100" label="部件更换">
            <template #default="{ row }">
              <div class="item-num">{{ row.changeCount }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pro-pagination
        class="g-pagination"
        v-if="allData.items.length !== 0"
        :pageObj="pageObj"
        :total="allData.data.totalCount"
      ></pro-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { getAllEquipmentLifes, equipmentChange } from '@/api'
  import { useRequest } from '@/utils/hooks'
  import { ElMessage, ElMessageBox } from 'element-plus'
  const isCheck = ref(false)
  let pageObj: any = ref({
    MaxResultCount: 10,
    SkipCount: 1
  })
  let selectValue = ref('')
  onMounted(async () => {
    await allData.getData()
  })
  let filter = ref('')
  const allData = useRequest(() =>
    getAllEquipmentLifes({
      MaxResultCount: pageObj.value.MaxResultCount,
      SkipCount: (pageObj.value.SkipCount - 1) * pageObj.value.MaxResultCount,
      Filter: filter.value
    })
  )
  watch([pageObj.value], () => {
    allData?.getData()
  })
  const search = (v: any) => {
    pageObj.value = {
      MaxResultCount: 10,
      SkipCount: 1
    }
    filter.value = v
    allData.getData()
  }
  const onCheck = (v: any) => {
    ElMessageBox.confirm(`确定更换 【${v.name}】 吗 ?`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(async () => {
      await equipmentChange(v.id)
      ElMessage.success('操作成功')
      allData.getData()
    })
  }
  const reset = () => {
    pageObj.value = {
      MaxResultCount: 10,
      SkipCount: 1
    }
    filter.value = ''
    allData.getData()
  }
</script>

<style lang="less" scoped>
  .life {
    position: relative;

    .first {
      display: flex;
      flex-direction: row-reverse;
      justify-content: flex-end;

      .ml-15 {
        margin-left: 15px;
      }
    }

    .check-box {
      width: 20px;
      height: 20px;
      background: #f2f5f9;
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #b0bed4;

      &--active {
        color: #fff;
        background: #5f7cd6;
      }
    }

    .second {
      height: 260px;
      width: 200px;
    }
  }

  .placeholder-box {
    min-height: 260px;
    background: #f2f5f9;
    padding-left: 350px;

    &__item {
      height: 60px;
      line-height: 60px;
      position: relative;

      .item-line2 {
        position: absolute;
        left: -50px;
        top: 0%;
        height: 61px;
        width: 2px;
        background: #efefef;
      }

      .last-line2 {
        height: 31px;
      }

      .item-name {
        font-weight: bold;
        color: #333;
        width: 150px;
      }

      .item-life {
        color: #7d7d86;
        margin-left: 9px;
        margin-right: 20px;
      }

      .item-btn {
        margin: auto 50px;
        width: 80px;
        height: 30px;
        border-radius: 10px;
      }
    }

    .item-line {
      position: absolute;
      left: -50px;
      top: 50%;
      transform: translateY(-50%);
      height: 2px;
      width: 30px;
      background: #efefef;
    }

    &__item::after {
      content: '';
      position: absolute;
      width: 8px;
      height: 8px;
      background: #5f7cd6;
      border-radius: 50%;
      top: 50%;
      left: -20px;
      transform: translateY(-50%);
    }
  }

  .item-num {
    width: 60px;
    height: 20px;
    background: #597fda;
    border-radius: 7px;
    text-align: center;
    color: #fff;
  }

  .table {
    margin-bottom: 20px;
  }

  :deep(.el-table) {
    .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>
