<template>
  <div class="app-container">
    <!-- 搜索区 -->
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" inline label-width="110px" class="search-form">
      <el-form-item label="业务单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="计划 ID" prop="planId">
        <el-input v-model="queryParams.planId" placeholder="无计划可空" clearable />
      </el-form-item>
      <el-form-item label="任务标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="关键词" clearable />
      </el-form-item>
      <el-form-item label="执行部门" prop="department">
        <el-select v-model="queryParams.department" placeholder="请选择" clearable style="width: 140px">
          <el-option label="免疫组" value="免疫组" />
          <el-option label="饲喂组" value="饲喂组" />
        </el-select>
      </el-form-item>
      <el-form-item label="执行人" prop="executorIds">
        <el-input v-model="queryParams.executorIds" placeholder="用户 ID" clearable />
      </el-form-item>
      <el-form-item label="计划日期" prop="executeDate">
        <el-date-picker v-model="queryParams.executeDate" type="date" value-format="YYYY-MM-DD" style="width: 140px" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="全部" clearable style="width: 110px">
          <el-option label="待派工" :value="0" />
          <el-option label="已派工" :value="1" />
          <el-option label="执行中" :value="2" />
          <el-option label="已完成" :value="3" />
          <el-option label="已取消" :value="4" />
          <el-option label="异常" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <el-icon>
            <Search />
          </el-icon> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <el-icon>
            <Refresh />
          </el-icon> 重置
        </el-button>
      </el-form-item>
    </el-form>

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

    <!-- 表格 -->
    <el-table v-loading="loading" :data="workList" stripe border size="small" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="ID" prop="id" width="60" align="center" />
      <el-table-column label="业务单号" prop="orderNo" min-width="140" show-overflow-tooltip />
      <el-table-column label="计划 ID" prop="planId" width="100" show-overflow-tooltip />
      <el-table-column label="业务类型" prop="bizType" width="90" align="center">
        <template #default="{ row }">
          <el-tag
            :type="['primary', 'success', 'info', 'warning', 'danger', 'info', 'danger', 'info', 'success'][row.bizType - 1]"
            size="small">
            {{ ['免疫', '保健', '转群', '称重', '配种', '干奶', '淘汰', '消毒', '饲喂'][row.bizType - 1] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务标题" prop="title" min-width="160" show-overflow-tooltip />
      <el-table-column label="执行部门" prop="department" width="110" show-overflow-tooltip />
      <el-table-column label="计划日期" prop="executeDate" width="110" align="center" />
      <el-table-column label="状态" prop="status" width="90" align="center">
        <template #default="{ row }">
          <el-tag :type="['info', 'primary', 'warning', 'success', 'info', 'danger'][row.status]" size="small">
            {{ ['待派工', '已派工', '执行中', '已完成', '已取消', '异常'][row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="优先级" prop="priority" width="90" align="center">
        <template #default="{ row }">
          <el-tag :type="['info', 'primary', 'danger'][row.priority - 1]" size="small">
            {{ ['普通', '重要', '紧急'][row.priority - 1] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template #default="{ row }">
          <el-button link type="primary" icon="View" @click="handleDetail(row)">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(row)"
            v-hasPermi="['work:work:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(row)"
            v-hasPermi="['work:work: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 v-model="open" :title="title" width="680px" top="8vh" append-to-body>
      <el-form ref="workRef" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="业务单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划 ID" prop="planId">
              <el-input v-model="form.planId" placeholder="无计划可空" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型" prop="bizType">
              <el-select v-model="form.bizType" placeholder="请选择" style="width: 100%">
                <el-option v-for="(t, idx) in ['免疫', '保健', '转群', '称重', '配种', '干奶', '淘汰', '消毒', '饲喂']" :key="idx" :label="t"
                  :value="idx + 1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优先级" prop="priority">
              <el-select v-model="form.priority" placeholder="请选择" style="width: 100%">
                <el-option label="普通" :value="1" />
                <el-option label="重要" :value="2" />
                <el-option label="紧急" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务标题" prop="title">
              <el-input v-model="form.title" placeholder="简短标题" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细说明">
              <Editor v-model="form.content" :min-height="180" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行部门" prop="department">
              <el-select v-model="form.department" placeholder="请选择" style="width: 100%">
                <el-option label="免疫组" value="免疫组" />
                <el-option label="饲喂组" value="饲喂组" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行人" prop="executorIds">
              <el-input v-model="form.executorIds" placeholder="用户 ID，多ID用逗号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划日期" prop="executeDate">
              <el-date-picker v-model="form.executeDate" type="date" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时段" prop="executeTime">
              <el-input v-model="form.executeTime" placeholder="例 08:00-10:00" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="派工人" prop="issuerId">
              <el-input v-model="form.issuerId" placeholder="用户 ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="派工时间" prop="issueTime">
              <el-date-picker v-model="form.issueTime" type="date" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接工人" prop="receiverId">
              <el-input v-model="form.receiverId" placeholder="用户 ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接工时间" prop="receiveTime">
              <el-date-picker v-model="form.receiveTime" type="date" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成时间" prop="finishTime">
              <el-date-picker v-model="form.finishTime" type="date" value-format="YYYY-MM-DD" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="执行结果">
              <el-input v-model="form.result" type="textarea" :rows="3" placeholder="可空" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="备注信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog v-model="detailOpen" title="派工单详情" width="680px" top="8vh" append-to-body>
      <el-form label-width="110px" class="detail-form">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="业务单号：">
              <span>{{ detailForm.orderNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划 ID：">
              <span>{{ detailForm.planId || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型：">
              <el-tag size="small"
                :type="['primary', 'success', 'info', 'warning', 'danger', 'info', 'danger', 'info', 'success'][detailForm.bizType - 1]">
                {{ ['免疫', '保健', '转群', '称重', '配种', '干奶', '淘汰', '消毒', '饲喂'][detailForm.bizType - 1] }}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优先级：">
              <el-tag size="small" :type="['info', 'primary', 'danger'][detailForm.priority - 1]">
                {{ ['普通', '重要', '紧急'][detailForm.priority - 1] }}
              </el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="任务标题：">
              <span>{{ detailForm.title }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细说明：">
              <div v-html="detailForm.content || '-'" class="detail-content"></div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行部门：">
              <span>{{ detailForm.department }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行人：">
              <span>{{ detailForm.executorIds }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划日期：">
              <span>{{ detailForm.executeDate }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时段：">
              <span>{{ detailForm.executeTime || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="派工人：">
              <span>{{ detailForm.issuerId || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="派工时间：">
              <span>{{ detailForm.issueTime || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接工人：">
              <span>{{ detailForm.receiverId || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接工时间：">
              <span>{{ detailForm.receiveTime || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成时间：">
              <span>{{ detailForm.finishTime || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="执行结果：">
              <span>{{ detailForm.result || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注：">
              <span>{{ detailForm.remark || '-' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="detailOpen = false">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Work">
import { listWork, getWork, delWork, addWork, updateWork } from '@/api/work/work'
import Editor from '@/components/Editor/index.vue'
import { Search, Refresh, Plus, Edit, Delete, Download, View } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()

const workList = 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 detailOpen = ref(false)
const detailForm = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    planId: null,
    bizType: null,
    title: null,
    content: null,
    department: null,
    executorIds: null,
    executeDate: null,
    executeTime: null,
    sheepScope: null,
    location: null,
    materialList: null,
    toolList: null,
    status: null,
    priority: null,
    issuerId: null,
    issueTime: null,
    receiverId: null,
    receiveTime: null,
    finishTime: null,
    result: null,
  },
  rules: {
    orderNo: [{ required: true, message: '业务单号不能为空', trigger: 'blur' }],
    bizType: [{ required: true, message: '请选择业务类型', trigger: 'change' }],
    title: [{ required: true, message: '任务标题不能为空', trigger: 'blur' }],
    department: [{ required: true, message: '执行部门不能为空', trigger: 'change' }],
    executorIds: [{ required: true, message: '执行人不能为空', trigger: 'blur' }],
    executeDate: [{ required: true, message: '计划日期不能为空', trigger: 'change' }],
    sheepScope: [{ required: true, message: '羊只范围不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    priority: [{ required: true, message: '请选择优先级', trigger: 'change' }],
  },
})

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

function getList() {
  loading.value = true
  listWork(queryParams.value).then(response => {
    workList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
function cancel() {
  open.value = false
  reset()
}
function reset() {
  form.value = {}
  proxy.resetForm('workRef')
}
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加派工单'
}
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getWork(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改派工单'
  })
}
function submitForm() {
  proxy.$refs.workRef.validate(valid => {
    if (valid) {
      if (form.value.id) {
        updateWork(form.value).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addWork(form.value).then(() => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除派工单编号为"' + _ids + '"的数据项？')
    .then(() => delWork(_ids))
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
}
function handleExport() {
  proxy.download('work/work/export', { ...queryParams.value }, `work_${new Date().getTime()}.xlsx`)
}

/** 查看详情 */
function handleDetail(row) {
  detailForm.value = { ...row } // 如需实时数据可改为 getWork(row.id).then(...)
  detailOpen.value = true
}

getList()
</script>

<style lang="scss" scoped>
.search-form {
  margin-bottom: 6px;
}

.mb8 {
  margin-bottom: 8px;
}

:deep(.el-table) {
  font-size: 13px;

  th {
    background-color: #fafafa;
    font-weight: 600;
    color: #333;
  }
}

.detail-form .el-form-item {
  margin-bottom: 8px;
}

.detail-content {
  padding: 6px 10px;
  background: #fafafa;
  border-radius: 4px;
  min-height: 60px;
  line-height: 1.6;
}
</style>