<!--生产线管理界面-->
<template>
  <div class='default-main'>
    <TableHeader :showReset="false" showCustomColumn>
      <template v-slot:operation>
<!--        <el-button type='primary' @click='refresh' class='ml10' :icon='Refresh'>刷新</el-button>-->
        <el-button type='primary' @click='add' class='ml10' :icon='Plus'>新增</el-button>
        <!--返回按钮-->
        <back-component />
      </template>
    </TableHeader>
    <!--表格-->
    <Table ref='tableRef'></Table>
    <!--弹框-->
    <product-popup ref='productLine' />
  </div>
</template>

<script setup lang='ts'>
import { Plus, Refresh } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { provide, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { deleteSgProductLine } from '@/api/advance-boot/sgGroven/sgProductLine'
import ProductPopup from './productPopup.vue'
import BackComponent from '@/components/icon/back/index.vue'

const productLine = ref()

defineOptions({
  name: 'productLine'
})
const { push } = useRouter()
const { query } = useRoute()


const tableStore = new TableStore({
  url: '/advance-boot/sgProductLine/list',
  method: 'POST',
  column: [
  { title: '序号',  width: 80,formatter: (row: any) => {
        return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
      } },
    { title: '用户名', minWidth: '130', field: 'userName' },
    { title: '进线名称', minWidth: '130', field: 'incomingLineName' },
    { title: '产线名称', minWidth: '130', field: 'name' },
    { title: '产能损失(万元)', minWidth: '120', field: 'capacityLoss' },
    { title: '终端损失(万元)', minWidth: '120', field: 'machineLoss' },
    { title: '原料损失(万元)', minWidth: '120', field: 'materialLoss' },
    { title: '产线功率(kW)', minWidth: '120', field: 'machinePower' },
    {
      title: '计算模式',
      titlePrefix: { message: '模式一：单次中断不计算终端、原料损失 \n 模式二：单次中断计算终端、原料损失', icon: 'vxe-icon-question-circle-fill' },
      render: 'tag',
      minWidth: '120',
      field: 'calcMode',
      replaceValue: {
        '0': '模式一',
        '1': '模式二'
      }
    },
    {
       title: '操作',fixed: 'right',
      align: 'center',
      render: 'buttons',
      minWidth: '230',
    
      buttons: [
        {
          name: 'productSetting',
          title: '终端管理',
          type: 'primary',
          icon: 'el-icon-EditPen',
          render: 'basicButton',
          click: row => {
            push(`/admin/businessUserRouter/machine?id=${query.id}&productLineId=${row.id}`)
          }
        },
        {
          name: 'update',
          title: '编辑',
          type: 'primary',
          icon: 'el-icon-EditPen',
          render: 'basicButton',
          click: row => {
            productLine.value.open('修改生产线', row)
          }
        },
        {
          name: 'update',
          title: '删除',
          type: 'danger',
          icon: 'el-icon-Delete',
          render: 'confirmButton',
          popconfirm: {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            confirmButtonType: 'danger',
            title: '确定删除吗？'
          },
          click: row => {
            deleteSgProductLine(row.id).then(res => {
              ElMessage.success('删除成功')
              tableStore.index()
            })
          }
        }
      ]
    }
  ],
  beforeSearchFun: () => {
    if (tableStore.table.params.addr) {
      tableStore.table.params.addrStrOption = tableStore.table.params.addr.map(tempArray => tempArray.join('/'))
    }
    for (let key in tableStore.table.params) {
      if (tableStore.table.params[key] === '') {
        delete tableStore.table.params[key]
      }
    }

  }
})

const getTableData = () => {
  tableStore.table.params.userId = query.id
  tableStore.index()
}
getTableData()

provide('tableStore', tableStore)

//新增
const add = () => {
  let data = {
    userId: query.id
  }
  productLine.value.open('新增生产线', data)
}

//刷新
const refresh = () => {
  tableStore.index()
}

</script>
