<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="人员姓名" prop="personId">
          <el-select v-model="queryParams.personId" placeholder="请选择人员" clearable>
            <el-option v-for="person in personList" :key="person.id" :label="person.name" :value="person.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="吃奶时间" prop="time">
          <el-date-picker v-model="queryParams.time" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" />
        </el-form-item>
        <el-form-item label="奶粉品牌" prop="milkPowderBrand">
          <el-input v-model="queryParams.milkPowderBrand" placeholder="请输入奶粉品牌" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleAdd" icon="Plus" v-hasPermi="['health:milkPowderRecord:add']">新增</el-button>
          <el-button :disabled="multiple" icon="Delete" @click="handleDelete" v-hasPermi="['health:milkPowderRecord:remove']">删除</el-button>
          <!-- <el-button
              @click="handleExport"
              icon="Download"
              v-hasPermi="['health:milkPowderRecord:export']"
          >导出</el-button> -->
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="milkPowderRecordList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="人员名称" align="center" prop="personName" />
          <el-table-column label="吃奶时间" align="center" prop="sucklesTime" width="180"> </el-table-column>
          <el-table-column label="吃奶量（毫升）" align="center" prop="consumption" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template v-slot="scope">
              <div class="ctrl-btn d-flex">
                <el-tooltip v-for="item in operateList" :key="item.id" class="item" effect="dark" :content="item.title" placement="top">
                  <el-button :icon="item.icon" :v-hasPermi="item.hasPermi" circle @click="handleOperate(item.id, scope.row)"></el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination small background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" />
      </div>
    </div>
    <!-- 添加或修改吃奶记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="milkPowderRecordRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="人员姓名" prop="personId">
          <el-select v-model="form.personId" placeholder="请选择人员" @change="handlePersonChange" clearable>
            <el-option v-for="person in personList" :key="person.id" :label="person.name" :value="person.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="吃奶时间" prop="sucklesTime">
          <el-date-picker clearable v-model="form.sucklesTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择吃奶时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="吃奶量" prop="consumption">
          <el-input v-model="form.consumption" type="number" placeholder="请输入吃奶量">
            <template #suffix>毫升</template>
          </el-input>
        </el-form-item>
        <el-form-item label="奶粉品牌" prop="milkPowderBrand">
          <el-input v-model="form.milkPowderBrand" placeholder="请输入奶粉品牌" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template v-if="title !== '查看吃奶记录'" #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MilkPowderRecord">
import { listMilkPowderRecord, getMilkPowderRecord, delMilkPowderRecord, addMilkPowderRecord, updateMilkPowderRecord } from '@/api/health/milkPowderRecord'
import { listPerson, getPerson } from '@/api/health/person'
import dayjs from 'dayjs'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()

const milkPowderRecordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const personList = ref([])

const operateList = ref([
  { id: 'view', icon: 'View', title: '查看', hasPermi: ['health:milkPowderRecord:query'] },
  { id: 'edit', icon: 'Edit', title: '修改', hasPermi: ['health:milkPowderRecord:edit'] },
  { id: 'delete', icon: 'Delete', title: '删除', hasPermi: ['health:milkPowderRecord:remove'] }
])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sucklesTime: null,
    personId: null,
    milkPowderBrand: null
  },
  queryPersonParams: {
    pageNum: 1,
    pageSize: 1000
  },
  rules: {
    sucklesTime: [{ required: true, message: '吃奶时间不能为空', trigger: 'blur' }],
    consumption: [{ required: true, message: '吃奶量不能为空', trigger: 'blur' }],
    personId: [{ required: true, message: '人员名称不能为空', trigger: 'blur' }]
  }
})

const handleOperate = (operate, row) => {
  switch (operate) {
    case 'view':
      handleView(row)
      break
    case 'edit':
      handleUpdate(row)
      break
    case 'delete':
      handleDelete(row)
      break
    default:
      break
  }
}

const { queryParams, form, rules, queryPersonParams } = toRefs(data)

/** 查询吃奶记录列表 */
function getList() {
  loading.value = true
  const timeRange = queryParams.value.time
  let st = ''
  let et = ''
  if (timeRange && timeRange.length === 2) {
    st = dayjs(timeRange[0]).format('YYYY-MM-DD')
    et = dayjs(timeRange[1]).format('YYYY-MM-DD')
  }
  queryParams.value.startTime = st
  queryParams.value.endTime = et
  listMilkPowderRecord(queryParams.value).then((response) => {
    milkPowderRecordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 查询成员管理列表 */
function getPersonList() {
  listPerson(queryPersonParams.value).then((response) => {
    personList.value = response.rows
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    sucklesTime: null,
    consumption: null,
    personId: null,
    milkPowderBrand: null,
    spoonful: null
  }
  proxy.resetForm('milkPowderRecordRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 查看
const handleView = (row) => {
  title.value = '查看吃奶记录'
  form.value = row
  open.value = true
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加吃奶记录'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getMilkPowderRecord(_id).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改吃奶记录'
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs.milkPowderRecordRef.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateMilkPowderRecord(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addMilkPowderRecord(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除选中的数据项？')
    .then(function () {
      return delMilkPowderRecord(_ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'health/milkPowderRecord/export',
    {
      ...queryParams.value
    },
    `milkPowderRecord_${new Date().getTime()}.xlsx`
  )
}

getList()
getPersonList()
</script>
