<script setup lang="ts">
import type { MatrixQuery } from 'apis/matrix'
import { del, list as listMatrix } from 'apis/matrix'
import FtButton from 'components/common/FTButton/index.vue'
import Edit from 'components/martix/Edit/index.vue'
import { ElMessageBox } from 'element-plus'
import { FtMessage } from 'libs/message'

import { onMounted, ref } from 'vue'

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

const tableData = ref([])
const total = ref(0)
const query = ref<MatrixQuery>({
  pageNum: 1,
  pageSize: 10,
  matrixName: undefined,
})

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

const addVisible = ref(false)

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

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()
}

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

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 () => {
    const res = await del(ids.join(','))
    if (res.matrixName?.length) {
      FtMessage.error(`基质: ${res.matrixName.join(',')}已绑定工艺, 无法删除`)
      return
    }
    FtMessage.success('删除成功')
    await getList()
  })
}
</script>

<template>
  <div>
    <el-row class="search-box">
      <el-col :span="12">
        <el-input v-model="query.matrixName" class="my-input" placeholder="请输入基质名称" clearable @input="inputChange" />
      </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="name" label="基质名称" />
      </el-table>
    </div>

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

<style scoped lang="scss">
.table-header {
  background: #E8ECF7;
}
.my-input {
  height: 100px;
  width: 400px;
  margin-right: 20px;
  background: #E8ECF7;
  border: 0;
  .el-input__wrapper {
    background: #E8ECF7;
    border: 0;
    .el-input__inner {
      height: calc(100% - 2px);
    }
  }
}

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

.my-select {
  width: 400px;
  .el-select__input {
    height: 80px !important;
  }
  .el-select__wrapper {
    min-height: 80px;
    background: #E8ECF7;
    border: 0;
    font-size: 40px;
    line-height:40px;
  }
  .el-select__placeholder {
    color: #0349A8;
    font-weight: 500;
  }
  .el-select-dropdown__item {
    height: 80px;
    line-height: 40px;
    padding: 20px;
    font-size: 40px;
  }
  .el-select-dropdown__empty {
    font-size: 40px;
    line-height: 40px;
    padding: 20px;
  }
}
.search-box {
  height: 120px;
}
.table-footer {
  height: 100px;
  display: flex;
  justify-content: flex-end;
}
.table-box {
  height: calc(100% - 120px - 100px);
}
</style>
