<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配方编码" prop="formulaId">
        <el-input
          v-model="queryParams.formulaId"
          placeholder="请输入配方编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="羊舍" prop="sheepHouseId">
        <el-input
          v-model="queryParams.sheepHouseId"
          placeholder="请输入羊舍"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="饲草班人员" prop="zookeeper">
        <el-input
          v-model="queryParams.zookeeper"
          placeholder="请输入饲草班人员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="饲喂计划日期" prop="planDate">
        <el-date-picker clearable
          v-model="queryParams.planDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择饲喂计划日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['feed:FeedPlan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['feed:FeedPlan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['feed:FeedPlan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['feed:FeedPlan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FeedPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="创建日期" align="center" prop="createDate" />
      <el-table-column label="配方编码" align="center" prop="formulaId" />
      <el-table-column label="羊舍" align="center" prop="sheepHouseId">
        <template #default="scope">
          <dict-tag :options="sheep_house_list" :value="scope.row.sheepHouseId"/>
        </template>
      </el-table-column>
      <el-table-column label="羊只数量" align="center" prop="sheepCount" />
      <el-table-column label="日均计划量" align="center" prop="planDailySize" />
      <el-table-column label="计划量(早)" align="center" prop="planMorningSize" />
      <el-table-column label="计划总量(早)" align="center" prop="planMorningTotal" />
      <el-table-column label="饲喂比例(早)" align="center" prop="ratioMorning" />
      <el-table-column label="实际量(早)" align="center" prop="actualMorningSize" />
      <el-table-column label="计划量(中)" align="center" prop="planNoonSize" />
      <el-table-column label="计划总量(中)" align="center" prop="planNoonTotal" />
      <el-table-column label="实际量(中)" align="center" prop="actualNoonSize" />
      <el-table-column label="饲喂比例(中)" align="center" prop="ratioNoon" />
      <el-table-column label="计划量(下)" align="center" prop="planAfternoonSize" />
      <el-table-column label="计划总量(下)" align="center" prop="planAfternoonTotal" />
      <el-table-column label="实际量(下)" align="center" prop="actualAfternoonSize" />
      <el-table-column label="饲喂比例(下)" align="center" prop="ratioAfternoon" />
      <el-table-column label="计划饲喂总量" align="center" prop="planFeedTotal" />
      <el-table-column label="饲草班人员" align="center" prop="zookeeper" />
      <el-table-column label="饲喂计划日期" align="center" prop="planDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['feed:FeedPlan:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['feed:FeedPlan:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改饲喂计划对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="FeedPlanRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="配方编码" prop="formulaId">
          <el-input v-model="form.formulaId" placeholder="请输入配方编码" />
        </el-form-item>
        <el-form-item label="羊舍" prop="sheepHouseId">
          <el-input v-model="form.sheepHouseId" placeholder="请输入羊舍" />
        </el-form-item>
        <el-form-item label="日均计划量" prop="planDailySize">
          <el-input v-model="form.planDailySize" placeholder="请输入日均计划量" />
        </el-form-item>
        <el-form-item label="饲喂比例(早)" prop="ratioMorning">
          <el-input v-model="form.ratioMorning" placeholder="请输入饲喂比例(早)" />
        </el-form-item>
        <el-form-item label="实际量(早)" prop="actualMorningSize">
          <el-input v-model="form.actualMorningSize" placeholder="请输入实际量(早)" />
        </el-form-item>
        <el-form-item label="实际量(中)" prop="actualNoonSize">
          <el-input v-model="form.actualNoonSize" placeholder="请输入实际量(中)" />
        </el-form-item>
        <el-form-item label="饲喂比例(中)" prop="ratioNoon">
          <el-input v-model="form.ratioNoon" placeholder="请输入饲喂比例(中)" />
        </el-form-item>
        <el-form-item label="实际量(下)" prop="actualAfternoonSize">
          <el-input v-model="form.actualAfternoonSize" placeholder="请输入实际量(下)" />
        </el-form-item>
        <el-form-item label="饲喂比例(下)" prop="ratioAfternoon">
          <el-input v-model="form.ratioAfternoon" placeholder="请输入饲喂比例(下)" />
        </el-form-item>
        <el-form-item label="饲草班人员" prop="zookeeper">
          <el-input v-model="form.zookeeper" placeholder="请输入饲草班人员" />
        </el-form-item>
        <el-form-item label="饲喂计划日期" prop="planDate">
          <el-date-picker clearable
            v-model="form.planDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择饲喂计划日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #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="FeedPlan">
import { listFeedPlan, getFeedPlan, delFeedPlan, addFeedPlan, updateFeedPlan } from "@/api/feed/FeedPlan"

const { proxy } = getCurrentInstance()

const FeedPlanList = 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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    formulaId: null,
    sheepHouseId: null,
    zookeeper: null,
    planDate: null,
  },
  rules: {
    formulaId: [
      { required: true, message: "配方编码不能为空", trigger: "blur" }
    ],
    sheepHouseId: [
      { required: true, message: "羊舍不能为空", trigger: "blur" }
    ],
    planDailySize: [
      { required: true, message: "日均计划量不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询饲喂计划列表 */
function getList() {
  loading.value = true
  listFeedPlan(queryParams.value).then(response => {
    FeedPlanList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    createDate: null,
    formulaId: null,
    sheepHouseId: null,
    sheepCount: null,
    planDailySize: null,
    planMorningSize: null,
    planMorningTotal: null,
    ratioMorning: null,
    actualMorningSize: null,
    planNoonSize: null,
    planNoonTotal: null,
    actualNoonSize: null,
    ratioNoon: null,
    planAfternoonSize: null,
    planAfternoonTotal: null,
    actualAfternoonSize: null,
    ratioAfternoon: null,
    planFeedTotal: null,
    zookeeper: null,
    planDate: null,
    remark: null
  }
  proxy.resetForm("FeedPlanRef")
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加饲喂计划"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _createDate = row.createDate || ids.value
  getFeedPlan(_createDate).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改饲喂计划"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["FeedPlanRef"].validate(valid => {
    if (valid) {
      if (form.value.createDate != null) {
        updateFeedPlan(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addFeedPlan(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _createDates = row.createDate || ids.value
  proxy.$modal.confirm('是否确认删除饲喂计划编号为"' + _createDates + '"的数据项？').then(function() {
    return delFeedPlan(_createDates)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

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

getList()
</script>
