<template>
    <div class="layout-pd">
      <!--部件电子档案-->
      <el-row>
        <!--操作-->
        <el-col :xs="24" >
          <el-card class="mt8" shadow="hover" >
            <el-form :model="bomModel.filterModel" :inline="true" @submit.stop.prevent>
              <el-form-item prop="name">
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb20">
                  <el-form-item label="物料号">
                    <el-input v-model="bomModel.filterModel.bomMateNo" placeholder="物料号" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb20">
                  <el-form-item label="生产编码">
                    <el-input v-model="bomModel.filterModel.bomProNo" placeholder="生产编码" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb20">
                  <el-form-item label="部件名称">
                    <el-input v-model="bomModel.filterModel.bomName" placeholder="部件名称" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb20">
                  <el-form-item label="部件类型">
                    <el-select v-model="bomModel.filterModel.bomType" placeholder="请选择部件类型" clearable class="w100">
                      <el-option label="计税主板" value="1"></el-option>
                      <el-option label="油气回收" value="2"></el-option>
                      <el-option label="计量主板" value="3"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>

              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
                <el-button v-auth="'api:admin:file:upload-file'" type="primary" icon="ele-Upload" @click="onUpload"> 上传 </el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <!--表格-->
        <el-col :xs="24" >
          <el-card class="my-fill mt8" shadow="hover">
            <el-table v-loading="bomModel.loading" :data="bomModel.bomListData" row-key="id" style="width: 100%">
              <el-table-column prop="guid" label="序号"  />
              <el-table-column prop="bomName" label="部件名称"  />
              <el-table-column prop="bomNo" label="物料号"  />
              <el-table-column prop="bomProNo" label="生产编号"  />
              <el-table-column prop="cpuId" label="CPUID"  />
              <el-table-column prop="software_v" label="软件版本"  />
              <el-table-column prop="status" label="状态" width="120" />
              <el-table-column prop="key_name" label="密钥状态"  />
              <el-table-column prop="stand_name" label="团标状态" />
              <el-table-column prop="createTime" label="创建时间"  />
              <el-table-column prop="remark" label="备注" >
                <!-- <template #default="{ row }">
                  <div class="my-flex">
                    <el-image
                      v-if="isImage(row.extension)"
                      :src="row.linkUrl"
                      :preview-src-list="previewImglist"
                      :initial-index="getInitialIndex(row.linkUrl)"
                      :lazy="true"
                      :hide-on-click-modal="true"
                      fit="scale-down"
                      preview-teleported
                      style="width: 80px; height: 80px"
                    />
                    <div class="ml10 my-flex-fill my-flex-y-center">
                      <div>{{ (row.fileName || '') + (row.extension || '') }}</div>
                    </div>
                  </div>
                </template> -->
              </el-table-column>

              <el-table-column prop="createdTime" label="更新时间" >
                <template #default="{ row }">
                  {{ formatterTime(row.modifiedTime || row.createdTime || '') }}
                </template>
              </el-table-column>
              <el-table-column label="操作"  fixed="right" header-align="center" align="center">
                <template #default="{ row }">
                  <!-- <el-popover :width="220">
                    <p>{{ row.linkUrl }}</p>
                    <div class="mt10" style="text-align: right; margin: 0">
                      <el-button icon="ele-CopyDocument" size="small" type="primary" @click="copyText(row.linkUrl)">复制地址</el-button>
                    </div>
                    <template #reference>
                      <el-button size="small" text type="primary">地址</el-button>
                    </template>
                  </el-popover>
                  <el-link
                    class="my-el-link mr12 ml12"
                    :href="row.linkUrl"
                    type="primary"
                    icon="ele-Download"
                    size="small"
                    :underline="false"
                    target="_blank"
                    >下载</el-link
                  >
                  <el-button v-auth="'api:admin:file:delete'" icon="ele-Delete" size="small" text type="danger" @click="onDelete(row)">删除</el-button> -->
                  <el-link
                    class="my-el-link mr12 ml12"
                    @click="$router.push(`/product/${row.guid}`)"
                    type="primary"
                    icon="ele-edit"
                    size="small"
                    :underline="false"
                    target="_blank"
                  >详情</el-link>
                </template>
              </el-table-column>
            </el-table>
            <div class="my-flex my-flex-end" style="margin-top: 20px">
              <el-pagination
                v-model:currentPage="bomModel.pageInput.currentPage"
                v-model:page-size="bomModel.pageInput.pageSize"
                :total="bomModel.total"
                :page-sizes="[10, 20, 50, 100]"
                small
                background
                @size-change="onSizeChange"
                @current-change="onCurrentChange"
                layout="total, sizes, prev, pager, next, jumper"
              />
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </template>
  <script lang="ts" setup name="admin/Component">
  import { ref, reactive, onMounted, onBeforeMount} from 'vue'  //computed, getCurrentInstance
  import { PageInputFileGetPageDto, ComponentGetPageOutPut } from '/src/api/admin/data-contracts'
  import { ComponentApi } from '/@/api/admin/productionManagement/Component'
  import dayjs from 'dayjs'
  import eventBus from '/src/utils/mitt'
  //import { isImage } from '/@/utils/test'
  //import commonFunction from '/@/utils/commonFunction'

  //const { proxy } = getCurrentInstance() as any



  const fileUploadRef = ref()

  //部件的页面对象
  const bomModel = reactive({
    loading: false,
    fileFormTitle: '',
    filterModel: {
      fileName: '',
      bomType: '',
      bomName: '',
      bomProNo: '',
      bomMateNo: '',
    },
    total: 0,
    pageInput: {
      currentPage: 1,
      pageSize: 20,
    } as PageInputFileGetPageDto,
    bomListData: [] as Array<ComponentGetPageOutPut>,
    bomLogsTitle: '',
  })

  //const { copyText } = commonFunction()

  //const previewImglist = computed(() => {
    //let imgList = [] as string[]
    //bomModel.bomListData.forEach((a) => {

    //})
   // return imgList
  //})

  onMounted(() => {
    onQuery()
    eventBus.off('refreshFile')
    eventBus.on('refreshFile', async () => {
      onQuery()
    })
  })

  onBeforeMount(() => {
    eventBus.off('refreshFile')
  })

  const formatterTime = (cellValue: any) => {
    return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss')
  }

  //const getInitialIndex = (imgUrl: string) => {
    //return previewImglist.value.indexOf(imgUrl)
  //}

  const onQuery = async () => {
    bomModel.loading = true
    const res = await new ComponentApi().getPage({ ...bomModel.pageInput, filter: bomModel.filterModel }).catch(() => {
        bomModel.loading = false
    })
    console.log(res)
    bomModel.bomListData = res?.data?.list ?? []
    bomModel.total = res?.data?.total ?? 0
    bomModel.loading = false
  }

  const onSizeChange = (val: number) => {
    bomModel.pageInput.pageSize = val
    onQuery()
  }

  const onCurrentChange = (val: number) => {
    bomModel.pageInput.currentPage = val
    onQuery()
  }

  const onUpload = () => {
    fileUploadRef.value.open()
  }

  //const onDelete = (row: FileGetPageOutput) => {
    //proxy.$modal
      //.confirmDelete(`确定要删除文件【${row.fileName}${row.extension}】?`)
      //.then(async () => {
        //await new BomApi().delete({ id: row.id as number }, { loading: true, showSuccessMessage: true })
       // onQuery()
    //  })
    //  .catch(() => {})
 // }
  </script>

  <style scoped lang="scss">
  .my-el-link {
    font-size: 12px;
  }
  </style>
