<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 class="form_item" prop="name" style="width:100%">
              <el-col :xs="24" :sm="12" :md="8" :lg="8" :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="8" :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="8" :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="8" :xl="6" class="mb20">
                <el-form-item label="部件类型">
                  <el-select  v-model="bomModel.filterModel.bomType" placeholder="请选择部件类型" >
                    <el-option v-for="(value, key) in componentType" :key="key" :label="value[1].name"  :value="Number(value[1].value)" />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                <el-form-item label="厂商">
                  <el-input v-model="bomModel.filterModel.manufacturer" placeholder="厂商" clearable></el-input>
                </el-form-item>
              </el-col>

              <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                <el-form-item label="条码">
                  <el-input v-model="bomModel.filterModel.barcode" placeholder="条码" clearable></el-input>
                </el-form-item>
              </el-col>

              <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                <el-form-item>
                  <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
                  <el-button type="primary" icon="ele-RefreshRight" @click="onReset"> 重置 </el-button>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-form>
          <div class="my-flex my-flex-end">
            <el-button v-auth="'api:admin:file:upload-file'" type="primary" icon="ele-Upload" @click="onUpload"> 上传 </el-button>
          </div>
        </el-card>
      </el-col>
      <!--表格-->
      <el-col :xs="24">
        <el-card style="height: 70vh" class="my-fill mt8" shadow="hover">
          <el-table v-loading="bomModel.loading" stripe :data="bomModel.bomListData" row-key="id" style="width: 100%">
            <el-table-column prop="guid" label="序号"  />
            <el-table-column prop="manufacturer" label="厂商"  />
            <el-table-column prop="bomName" label="部件名称"  />
            <el-table-column prop="bomNo" label="物料号"  />
            <el-table-column prop="bomProNo" label="生产编号"  />
            <el-table-column prop="barCode" label="条码"  />
            <el-table-column prop="software_v" label="软件版本"  />
            <el-table-column prop="status"  label="状态" width="120" >
              <template #default="{ row }">
                <div :style="{color:getComponentStatusType(row.status).color} " class="bold-font">
                  {{getComponentStatusType(row.status).name}}
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="key_name" label="中化密钥"  >
              <template #default="{ row }">
                <div :style="{color:getComponentStatus(row.key_name).color}" class="bold-font">
                  {{getComponentStatus(row.key_name).name}}
                </div>
              </template>
            </el-table-column> -->
            <el-table-column prop="tqcKeyState" label="团标状态" >
              <template #default="{ row }">
                <!-- <div :style="{color:getComponentStatus(row.key_name).color}" class="bold-font"> -->
                  <div :style="{color:getColor(getComponentStatus(row.tqcKeyState).name)}" class="bold-font">
                  {{getComponentStatus(row.tqcKeyState).name}}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="ecvrKeyState" label="油气回收" >
              <template #default="{ row }">
                <!-- <div :style="{color:getComponentStatus(row.key_name).color}" class="bold-font"> -->
                <div :style="{color:getColor(getComponentStatus(row.ecvrKeyState).name)}" class="bold-font">
                  {{getComponentStatus(row.ecvrKeyState).name}}
                </div>
              </template>
            </el-table-column>
            <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" class="right-operation" width="100">
              <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="toPage(row)"
                  type="primary"
                  icon="ele-Tickets"
                  :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 {onMounted, reactive, ref} from 'vue' //computed, getCurrentInstance
import {ComponentApi} from '/@/api/admin/productionManagement/Component'
// import dayjs from 'dayjs'
// import eventBus from '/@/utils/mitt'
import {PageInputFileGetPageDto} from "/@/api/admin/productionManagement/ComopnentDto";
import {ComponentGetPageOutPut} from "/@/api/admin/productionManagement/CommunalDto";
import router from "/@/router";

import {useGlobalCacheStore} from "/@/stores/globalCacheStore";

/**获取全局缓存*/
const globalCacheStore = useGlobalCacheStore()
/**部件类型缓存*/
const componentType = ref(globalCacheStore.getGlobalStore().get('componentType'))
/**部件电子档案 - 列表状态 */
const componentStatusType = ref(globalCacheStore.getGlobalStore().get('componentStatusType'))
/**部件电子档案 - 列表中化密钥、团标密钥 */
const componentStatus = ref(globalCacheStore.getGlobalStore().get('componentStatus'))

const fileUploadRef = ref()

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

/**部件电子档案 - 列表状态 */
const getComponentStatusType = (val) => {
  val = String(val)
  if(componentStatusType.value?.has(val)){
    return componentStatusType.value.get(val)
  }else {
    return val
  }
}

/**部件电子档案 - 列表中化密钥、团标密钥 */
const getComponentStatus = (val) => {
  val = String(val)
  if(componentStatus.value.has(val)){
    return componentStatus.value.get(val)
  }else {
    return val
  }
}

//设置不同团标状态、油气回收字体颜色
const getColor = (value: string): string => {
  if (value === '已备案') {
    return '#41b584';
  } else if (value === '灌注') {
      return '#409eff';
  } else {
    return '#E6A23C';
  }
}
//const { copyText } = commonFunction()

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

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

onMounted(async () => {
  await onQuery()
})



// 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
  debugger
  const res = await new ComponentApi().getPage({ ...bomModel.pageInput, filter: bomModel.filterModel }).catch(() => {
    bomModel.loading = false
  })
  bomModel.bomListData = res?.data?.list
  //   .map(item => ({
  //   ...item,
  //   key_name:componentStatus.value?.get(item.key_name) ?? '',
  //   stand_name:componentStatus.value?.get(item.stand_name) ?? '',
  //   status:componentStatusType.value?.get(item.status) ?? ''
  // }))
  bomModel.total = res?.data?.total ?? 0
  bomModel.loading = false
}

/**重置表单*/
const onReset = () => {
  resetSearchForm()
  onQuery()
}

/**
 * 重置表单
 */
const resetSearchForm = () => {
  bomModel.filterModel.bomType = ''
  bomModel.filterModel.bomName = ''
  bomModel.filterModel.bomProNo = ''
  bomModel.filterModel.bomMateNo = ''
  bomModel.pageInput.currentPage = 1
}

/**
 * 页条变化
 * @param val
 */
const onSizeChange = (val: number) => {
  bomModel.pageInput.pageSize = val
  onQuery()
}

/**
 * 页数变化
 * @param val
 */
const onCurrentChange = (val: number) => {
  bomModel.pageInput.currentPage = val
  onQuery()
}

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

const toPage = (row) => {
  router.push({path:`/product/${row.guid}`})
}

//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: 14px;
}
.el-form .el-col.mb20 {
  margin: 0 !important;
}
.el-input, .el-select {
  width: 240px;
}
.bold-font {
  font-weight: bold;
}
</style>
