<template>
  <MyLayout>
    <el-card v-show="state.showQuery" class="my-query-box mt8" shadow="never" :body-style="{ paddingBottom: '0' }">
      <el-form :inline="true" label-width="auto" @submit.stop.prevent>
        <el-form-item label="关键词">
          <el-input v-model="state.filter.keyWord" placeholder="细胞编号、细胞名称" @keyup.enter="onQuery" />
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="state.filter.stDate"
            type="datetime"
            placeholder="选择开始时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="state.filter.edDate"
            type="datetime"
            placeholder="选择结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
          <el-button v-auth="'api:admin:item-def-cell:add'" type="primary" icon="ele-Plus" @click="onAdd"> 新增 </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="my-fill mt8" shadow="never">
      <div class="my-tools-box mb8 my-flex my-flex-between">
        <div>
        </div>
      </div>
      <el-table
        v-if="state.showCellList"
        :data="state.cellListData"
        style="width: 100%"
        v-loading="state.loading"
        border
      >
        <el-table-column prop="cellNo" label="细胞编号" min-width="120" show-overflow-tooltip />
        <el-table-column prop="cellName" label="细胞名称" min-width="120" show-overflow-tooltip />
        <el-table-column label="状态" width="80" align="center" show-overflow-tooltip>
          <template #default="{ row }">
            <el-tag type="success" v-if="row.status">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            {{ formatDate(new Date(row.createdTime), 'YYYY-mm-dd HH:MM:SS') }}
          </template>
        </el-table-column>
        <el-table-column prop="modifiedTime" label="修改时间" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            {{ formatDate(new Date(row.modifiedTime), 'YYYY-mm-dd HH:MM:SS') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right" header-align="center" align="center">
          <template #default="{ row }">
            <el-button
              v-if="auth('api:admin:item-def-cell:update')"
              icon="ele-EditPen"
              size="small"
              text
              type="primary"
              @click="onEdit(row)"
              >编辑</el-button
            >
            <el-button
              v-if="auth('api:admin:item-def-cell:soft-delete')"
              icon="ele-Delete"
              size="small"
              text
              type="danger"
              @click="onDelete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="my-flex my-flex-end" style="margin-top: 10px">
        <el-pagination
          v-model:current-page="state.pageInput.currentPage"
          v-model:page-size="state.pageInput.pageSize"
          :total="state.total"
          :page-sizes="[10, 20, 50, 100]"
          size="small"
          background
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
    </el-card>

    <cell-form ref="cellFormRef" :title="state.cellFormTitle"></cell-form>
  </MyLayout>
</template>

<script lang="ts" setup name="admin/cell">
import { ref, reactive, onMounted, getCurrentInstance, onBeforeMount, defineAsyncComponent } from 'vue'
import { CellDto, PageInputCellDtoGetPageInput } from '/@/api/admin/data-contracts'
import { ItemDefCellApi } from '/@/api/admin/item-def-cell'
import eventBus from '/@/utils/mitt'
import { auth } from '/@/utils/authFunction'
import { formatDate } from '/@/utils/formatTime'

// 引入组件
const CellForm = defineAsyncComponent(() => import('./components/CellForm.vue'))

const { proxy } = getCurrentInstance() as any

const cellFormRef = ref()

const state = reactive({
  loading: false,
  cellFormTitle: '',
  filter: {
    keyWord: '',
    stDate: '',
    edDate: ''
  },
  total: 0,
  pageInput: {
    currentPage: 1,
    pageSize: 20,
    filter: {
      keyWord: '',
      stDate: '',
      edDate: ''
    }
  } as PageInputCellDtoGetPageInput,
  cellListData: [] as Array<CellDto>,
  showQuery: true,
  showCellList: true,
})

onMounted(() => {
  Query()
  eventBus.off('refreshCell')
  eventBus.on('refreshCell', () => {
    Query()
  })
})

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

const onChangeCellList = () => {
  state.showCellList = !state.showCellList
  if (state.showCellList) {
    Query()
  }
}

const onQuery = () => {
  Query()
}

const Query = async () => {
  state.loading = true
  state.pageInput.filter = state.filter
  const res = await new ItemDefCellApi().getPage(state.pageInput).catch(() => {
    state.loading = false
  })

  state.cellListData = res?.data?.list ?? []
  state.total = res?.data?.total ?? 0
  state.loading = false
}

const onAdd = () => {
  state.cellFormTitle = '新增细胞类型'
  cellFormRef.value.open({
    id: 0,
    cellNo: '',
    cellName: '',
    status: true,
    createdTime: '',
    modifiedTime: '',
    isDeleted: false
  })
}

const onEdit = (row: CellDto) => {
  state.cellFormTitle = '编辑细胞类型'
  cellFormRef.value.open(row)
}

const onDelete = (row: CellDto) => {
  proxy.$modal
    .confirmDelete(`确定要删除细胞类型【${row.cellName}】?`)
    .then(async () => {
      await new ItemDefCellApi().softDelete({ id: row.id }, { loading: true })
      Query()
    })
    .catch(() => {})
}

const onSizeChange = (val: number) => {
  state.pageInput.currentPage = 1
  state.pageInput.pageSize = val
  onQuery()
}

const onCurrentChange = (val: number) => {
  state.pageInput.currentPage = val
  onQuery()
}
</script>

<style scoped lang="scss"></style>
