<script setup lang="ts">
import type { MatrixCraftQuery } from 'apis/matrixCraft'
import { list as listMatrix } from 'apis/matrix'
import { del, list } from 'apis/matrixCraft'

import route2 from 'assets/images/route_horizontal.png'
import route1 from 'assets/images/route_vertical.png'
import FtButton from 'components/common/FTButton/index.vue'
import Edit from 'components/martixCraft/Edit/index.vue'
import { ElMessageBox } from 'element-plus'
import { FtMessage } from 'libs/message'
import { onMounted, ref } from 'vue'

onMounted(() => {
  getList()
  getMatrixList()
})

const selectedData = ref<any[]>([])
const handleSelectionChange = (val: any[]) => {
  console.log(val)
  selectedData.value = val
}

const tableData = ref([])
const matrixList = ref<{ id: number, name: string }[]>([])
const total = ref(0)
const query = ref<MatrixCraftQuery>({
  pageNum: 1,
  pageSize: 10,
  matrixCraftName: undefined,
  matrixId: undefined,
})

const getMatrixList = async () => {
  const res = await listMatrix({ pageNum: 1, pageSize: 100, matrixName: '' })
  matrixList.value = res.list
}

const loading = ref(false)
const getList = async () => {
  loading.value = true
  const res = await list(query.value)
  tableData.value = res.list
  total.value = res.total
  loading.value = false
}

const addVisible = ref(false)
const addHandle = () => {
  formData.value = {}
  addVisible.value = true
}

const formData = ref({})
const editHandle = () => {
  formData.value = selectedData.value[0]
  addVisible.value = true
}

const delHandle = async () => {
  const ids = selectedData.value.map((item: any) => item.id)
  ElMessageBox.confirm('确认删除？', '提示', {
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    showCancelButton: true,
    showClose: false,
    closeOnClickModal: false,
    closeOnPressEscape: false,
    closeOnHashChange: false,
  }).then(async () => {
    await del(ids.join(','))
    FtMessage.success('删除成功')
    await getList()
  })
}

const pageChange = (pageNum: number, pageSize: number) => {
  query.value.pageNum = pageNum
  query.value.pageSize = pageSize
  getList()
}

const inputChange = () => {
  query.value.pageNum = 1
  query.value.pageSize = 10
  getList()
}

const ok = () => {
  addVisible.value = false
  getList()
}
</script>

<template>
  <div>
    <el-row class="search-box">
      <el-col :span="12">
        <el-input v-model="query.matrixCraftName" class="my-input" placeholder="请输入工艺名称" clearable @input="inputChange" />
        <el-select v-model="query.matrixId" class="my-select" placeholder="请选择基质" clearable @change="inputChange">
          <el-option v-for="item in matrixList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-col>
      <el-col :span="12">
        <div style="float: right">
          <FtButton type="primary" @click="addHandle">
            新增
          </FtButton>
          <FtButton :disabled="selectedData.length !== 1" @click="editHandle">
            编辑
          </FtButton>
          <FtButton :disabled="selectedData.length === 0" @click="delHandle">
            删除
          </FtButton>
        </div>
      </el-col>
    </el-row>
    <div class="table-box">
      <el-table
        v-loading="loading"
        :data="tableData"
        header-row-class-name="table-header"
        height="100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="matrixName" label="基质名称">
          <template #default="scope">
            <div class="scope-box">
              {{ matrixList.find(item => item.id === scope.row.matrixId)?.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="工艺名称" />
        <el-table-column prop="matrixPathType" label="喷涂路线">
          <template #default="scope">
            <div class="scope-box">
              <img v-show="scope.row.routeType === 'horizontal'" :src="route1" width="20px" height="20px" alt="icon">
              <img v-show="scope.row.routeType === 'vertical'" :src="route2" width="20px" height="20px" alt="icon">
              <img v-show="scope.row.routeType === 'grid'" :src="route2" width="20px" height="20px" alt="icon">
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="motorZHeight" label="喷涂高度" />
        <el-table-column prop="gasPressure" label="氮气气压" />
        <el-table-column prop="volume" label="基质流速" />
        <el-table-column prop="highVoltageValue" label="电压" />
        <el-table-column prop="movingSpeed" label="移速" />
        <el-table-column prop="spacing" label="行间距" />
        <el-table-column prop="times" label="喷涂遍数" />
      </el-table>
    </div>

    <div class="table-footer">
      <el-pagination size="large" :total background layout="prev, pager, next,total" @change="pageChange" />
    </div>
    <Edit v-if="addVisible" :matrix-list :form-data @ok="ok" @cancel="addVisible = false" />
  </div>
</template>

<style scoped lang="scss">
.table-header {
background: #E8ECF7;
}
.my-input {
  width: 400px;
  margin-right: 20px;
}

 :deep(.el-input__inner)::placeholder {
   color: #a8abb2;
   font-size: 40px;
 }

.search-box {
  height: 120px;
}
.table-footer {
  height: 100px;
  display: flex;
  justify-content: flex-end;
}
.table-box {
 height: calc(100% - 120px - 100px);
}
</style>
