<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable>
          <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-option label="其他" value="6" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属地块" prop="plotNames">
        <el-select 
          v-model="queryParams.plotNames" 
          placeholder="请选择地块" 
          multiple 
          collapse-tags
          clearable
          style="width: 200px"
        >
          <el-option 
            v-for="item in plotOptions" 
            :key="item.value" 
            :label="item.label" 
            :value="item.value"
          />
          <div v-if="plotOptions.length === 0" style="text-align: center; padding: 20px; color: #999;">
            暂无数据
          </div>
        </el-select>
      </el-form-item>
      <el-form-item label="所属农场" prop="farmNames">
        <el-select 
          v-model="queryParams.farmNames" 
          placeholder="请选择农场" 
          multiple 
          collapse-tags
          clearable
          style="width: 200px"
        >
          <el-option 
            v-for="item in farmOptions" 
            :key="item.value" 
            :label="item.label" 
            :value="item.value"
          />
          <div v-if="farmOptions.length === 0" style="text-align: center; padding: 20px; color: #999;">
            暂无数据
          </div>
        </el-select>
      </el-form-item>
      <el-form-item label="执行人" prop="executor">
        <el-input
          v-model="queryParams.executor"
          placeholder="请输入执行人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择任务状态" clearable>
          <el-option label="待完成" value="0" />
          <el-option label="已完成" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="执行时间" prop="executeTimeRange">
        <el-date-picker
          v-model="queryParams.executeTimeRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          clearable
          style="width: 350px">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成时间" prop="completionTimeRange">
        <el-date-picker
          v-model="queryParams.completionTimeRange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          clearable
          style="width: 350px">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @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="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:tasks:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:tasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:tasks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务类型" align="center" prop="taskType" width="100">
        <template slot-scope="scope">
          <el-tag :type="getTaskTypeTagType(scope.row.taskType)" size="small">
            {{ getTaskTypeName(scope.row.taskType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="作物名称" align="center" prop="cropName" />
      <el-table-column label="所属地块" align="center" prop="plotName" />
      <el-table-column label="所属农场" align="center" prop="farmName" />
      <el-table-column label="执行人" align="center" prop="executor" />
      <el-table-column label="执行时间" align="center" prop="executeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.executeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatus" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.taskStatus === 1 ? 'success' : 'warning'" size="small">
            {{ scope.row.taskStatus === 1 ? '已完成' : '待完成' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务说明" align="center" prop="taskDescription" width="150" show-overflow-tooltip />
      <el-table-column label="完成时间" align="center" prop="completionTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.completionTime ? parseTime(scope.row.completionTime, '{y}-{m}-{d} {h}:{i}:{s}') : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成说明" align="center" prop="completionDescription" width="150" show-overflow-tooltip />
      <el-table-column label="附件" align="center" prop="attachmentImages" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.attachmentImages && scope.row.attachmentImages.length > 0" type="success" size="mini">
            {{ scope.row.attachmentImages.length }}张
          </el-tag>
          <span v-else style="color: #999;">-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:tasks:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:tasks:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农事任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="任务名称" prop="taskName">
              <el-input v-model="form.taskName" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="taskType">
              <el-select v-model="form.taskType" placeholder="请选择任务类型">
                <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-option label="其他" value="6" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属地块" prop="plotName">
              <el-select v-model="form.plotName" placeholder="请选择地块" clearable style="width: 100%">
                <el-option 
                  v-for="item in plotOptions" 
                  :key="item.value" 
                  :label="item.label" 
                  :value="item.value"
                />
                <div v-if="plotOptions.length === 0" style="text-align: center; padding: 20px; color: #999;">
                  暂无数据
                </div>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属农场" prop="farmName">
              <el-select v-model="form.farmName" placeholder="请选择农场" clearable style="width: 100%">
                <el-option 
                  v-for="item in farmOptions" 
                  :key="item.value" 
                  :label="item.label" 
                  :value="item.value"
                />
                <div v-if="farmOptions.length === 0" style="text-align: center; padding: 20px; color: #999;">
                  暂无数据
                </div>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="执行人" prop="executor">
              <el-select v-model="form.executor" placeholder="请选择执行人" clearable style="width: 100%">
                <el-option 
                  v-for="item in executorOptions" 
                  :key="item.value" 
                  :label="item.label" 
                  :value="item.value"
                />
                <div v-if="executorOptions.length === 0" style="text-align: center; padding: 20px; color: #999;">
                  暂无数据
                </div>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行时间" prop="executeTime">
              <el-date-picker clearable
                v-model="form.executeTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择执行时间"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="任务状态" prop="taskStatus">
              <el-select v-model="form.taskStatus" placeholder="请选择任务状态" style="width: 100%">
                <el-option label="待完成" value="0" />
                <el-option label="已完成" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="任务说明" prop="taskDescription">
          <el-input v-model="form.taskDescription" type="textarea" :rows="3" placeholder="请输入任务说明" />
        </el-form-item>
        <el-row :gutter="20" v-if="form.taskStatus == 1">
          <el-col :span="12">
            <el-form-item label="完成时间" prop="completionTime">
              <el-date-picker clearable
                v-model="form.completionTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择完成时间"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="完成说明" prop="completionDescription" v-if="form.taskStatus == 1">
          <el-input v-model="form.completionDescription" type="textarea" :rows="3" placeholder="请输入完成说明" />
        </el-form-item>
        <el-form-item label="上传附件" prop="attachmentImages" v-if="form.taskStatus == 1">
          <image-upload 
            v-model="form.attachmentImages"
            :limit="5"
            :fileSize="10"
            :fileType="['jpg', 'jpeg', 'png', 'gif']"
          />
          <div style="color: #999; font-size: 12px; margin-top: 5px;">
            支持jpg、jpeg、png、gif格式，单张图片不超过10MB，最多上传5张
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTasks, getTasks, delTasks, addTasks, updateTasks } from "@/api/base/tasks"
import ImageUpload from "@/components/ImageUpload"

export default {
  name: "Tasks",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农事任务表格数据
      tasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        taskType: null,
        plotNames: [],
        farmNames: [],
        executor: null,
        taskStatus: null,
        executeTimeRange: null,
        completionTimeRange: null,
      },
      // 地块选项
      plotOptions: [],
      // 农场选项
      farmOptions: [],
      // 执行人选项
      executorOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        taskType: [
          { required: true, message: "任务类型不能为空", trigger: "change" }
        ],
        plotName: [
          { required: true, message: "所属地块不能为空", trigger: "change" }
        ],
        farmName: [
          { required: true, message: "所属农场不能为空", trigger: "change" }
        ],
        executor: [
          { required: true, message: "执行人不能为空", trigger: "change" }
        ],
        executeTime: [
          { required: true, message: "执行时间不能为空", trigger: "blur" }
        ],
        taskStatus: [
          { required: true, message: "任务状态不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.initOptions()
  },
  methods: {
    /** 初始化下拉选项 */
    initOptions() {
      // 为了支持修改表单回填，这里提供一些模拟数据
      // 实际开发中应该调用API获取真实数据
      this.plotOptions = [
        { label: "东区1号田", value: "东区1号田" },
        { label: "西区2号田", value: "西区2号田" },
        { label: "南区果园", value: "南区果园" },
        { label: "北区菜地", value: "北区菜地" },
        { label: "温室大棚A区", value: "温室大棚A区" },
        { label: "智能温室B区", value: "智能温室B区" },
        { label: "智能温室C区", value: "智能温室C区" },
        { label: "有机种植区", value: "有机种植区" },
        { label: "葡萄园A区", value: "葡萄园A区" },
        { label: "机械库房", value: "机械库房" }
      ]
      
      this.farmOptions = [
        { label: "阳光农场", value: "阳光农场" },
        { label: "绿野农场", value: "绿野农场" },
        { label: "丰收农场", value: "丰收农场" },
        { label: "田园农场", value: "田园农场" },
        { label: "科技农场", value: "科技农场" },
        { label: "有机农场", value: "有机农场" },
        { label: "现代农场", value: "现代农场" },
        { label: "生态农场", value: "生态农场" },
        { label: "智慧农场", value: "智慧农场" },
        { label: "酒庄农场", value: "酒庄农场" }
      ]
      
      this.executorOptions = [
        { label: "张三", value: "张三" },
        { label: "李四", value: "李四" },
        { label: "王五", value: "王五" },
        { label: "赵六", value: "赵六" },
        { label: "钱七", value: "钱七" },
        { label: "孙八", value: "孙八" },
        { label: "周九", value: "周九" },
        { label: "吴十", value: "吴十" },
        { label: "郑十一", value: "郑十一" },
        { label: "冯十二", value: "冯十二" }
      ]
    },
    /** 获取任务类型名称 */
    getTaskTypeName(type) {
      const typeMap = {
        '0': '施肥',
        '1': '灌溉', 
        '2': '打药',
        '3': '采摘',
        '4': '巡检',
        '5': '除草',
        '6': '其他'
      }
      return typeMap[type] || '未知'
    },
    /** 获取任务类型标签类型 */
    getTaskTypeTagType(type) {
      const tagTypeMap = {
        '0': 'success',  // 施肥
        '1': 'primary',  // 灌溉
        '2': 'warning',  // 打药
        '3': 'danger',   // 采摘
        '4': 'info',     // 巡检
        '5': '',         // 除草
        '6': 'default'   // 其他
      }
      return tagTypeMap[type] || ''
    },
    /** 查询农事任务列表 */
    getList() {
      this.loading = true
      // 模拟假数据，实际开发中应该调用真实的API
      const mockData = {
        rows: [
          {
            id: 1,
            taskName: "水稻施肥作业",
            taskType: "0",
            cropName: "水稻",
            plotName: "东区1号田",
            farmName: "阳光农场",
            executor: "张三",
            executeTime: "2024-01-15 09:00:00",
            taskStatus: 1,
            taskDescription: "对水稻进行有机肥施肥，提高产量和品质，需要注意施肥量的控制，避免过量施肥导致烧苗",
            completionTime: "2024-01-15 11:30:00",
            completionDescription: "已完成有机肥施肥，共施肥50亩，肥料用量符合标准要求，苗情良好",
            attachmentImages: ["https://example.com/images/fertilizer1.jpg", "https://example.com/images/fertilizer2.jpg"]
          },
          {
            id: 2,
            taskName: "玉米灌溉任务",
            taskType: "1",
            cropName: "玉米",
            plotName: "西区2号田",
            farmName: "绿野农场",
            executor: "李四",
            executeTime: "2024-01-16 06:30:00",
            taskStatus: 0,
            taskDescription: "对玉米田进行滴灌作业，确保土壤湿度适宜",
            completionTime: null,
            completionDescription: null,
            attachmentImages: null
          },
          {
            id: 3,
            taskName: "果园病虫害防治",
            taskType: "2",
            cropName: "苹果",
            plotName: "南区果园",
            farmName: "丰收农场",
            executor: "王五",
            executeTime: "2024-01-17 07:00:00",
            taskStatus: 1,
            taskDescription: "对苹果树进行病虫害防治，使用生物农药，确保果实品质安全",
            completionTime: "2024-01-17 10:45:00",
            completionDescription: "病虫害防治作业完成，使用生物农药150公斤，覆盖果园100亩",
            attachmentImages: ["https://example.com/images/pesticide1.jpg"]
          },
          {
            id: 4,
            taskName: "草莓采摘作业",
            taskType: "3",
            cropName: "草莓",
            plotName: "温室大棚A区",
            farmName: "田园农场",
            executor: "赵六",
            executeTime: "2024-01-18 05:00:00",
            taskStatus: 0,
            taskDescription: "对成熟草莓进行采摘，注意保持果实完整性，分级包装",
            completionTime: null,
            completionDescription: null,
            attachmentImages: null
          },
          {
            id: 5,
            taskName: "大棚设施巡检",
            taskType: "4",
            cropName: "番茄",
            plotName: "智能温室B区",
            farmName: "科技农场",
            executor: "钱七",
            executeTime: "2024-01-19 08:00:00",
            taskStatus: 1,
            taskDescription: "对温室大棚设施进行全面巡检，检查通风、灌溉、温控等系统运行状态",
            completionTime: "2024-01-19 12:00:00",
            completionDescription: "巡检完成，发现2号风机需要维护，其他设施运行正常",
            attachmentImages: ["https://example.com/images/inspection1.jpg", "https://example.com/images/inspection2.jpg", "https://example.com/images/inspection3.jpg"]
          },
          {
            id: 6,
            taskName: "蔬菜田除草工作",
            taskType: "5",
            cropName: "白菜",
            plotName: "北区菜地",
            farmName: "有机农场",
            executor: "孙八",
            executeTime: "2024-01-20 07:30:00",
            taskStatus: 0,
            taskDescription: "对白菜田进行人工除草，清除田间杂草，提高蔬菜生长环境",
            completionTime: null,
            completionDescription: null,
            attachmentImages: null
          },
          {
            id: 7,
            taskName: "农机设备维护",
            taskType: "6",
            cropName: "小麦",
            plotName: "机械库房",
            farmName: "现代农场",
            executor: "周九",
            executeTime: "2024-01-21 09:00:00",
            taskStatus: 1,
            taskDescription: "对收割机进行定期维护保养，更换机油、清洁滤芯、检查传动系统",
            completionTime: "2024-01-21 16:30:00",
            completionDescription: "设备维护完成，更换机油20升，清洁空气滤芯，传动系统正常",
            attachmentImages: ["https://example.com/images/maintenance1.jpg"]
          },
          {
            id: 8,
            taskName: "有机肥料施用",
            taskType: "0",
            cropName: "茄子",
            plotName: "有机种植区",
            farmName: "生态农场",
            executor: "吴十",
            executeTime: "2024-01-22 08:30:00",
            taskStatus: 0,
            taskDescription: "为茄子田施用有机肥料，改善土壤结构，提供充足养分支持茄子生长发育",
            completionTime: null,
            completionDescription: null,
            attachmentImages: null
          },
          {
            id: 9,
            taskName: "精准灌溉系统调试",
            taskType: "1",
            cropName: "黄瓜",
            plotName: "智能温室C区",
            farmName: "智慧农场",
            executor: "郑十一",
            executeTime: "2024-01-23 10:00:00",
            taskStatus: 1,
            taskDescription: "调试精准灌溉系统，设置不同生长期的灌溉参数，确保黄瓜水分需求得到满足",
            completionTime: "2024-01-23 14:20:00",
            completionDescription: "灌溉系统调试完成，设置了3个不同生长期的灌溉方案，系统运行稳定",
            attachmentImages: ["https://example.com/images/irrigation1.jpg", "https://example.com/images/irrigation2.jpg"]
          },
          {
            id: 10,
            taskName: "葡萄园修剪作业",
            taskType: "6",
            cropName: "葡萄",
            plotName: "葡萄园A区",
            farmName: "酒庄农场",
            executor: "冯十二",
            executeTime: "2024-01-24 07:00:00",
            taskStatus: 0,
            taskDescription: "对葡萄藤进行冬季修剪，去除病枝、弱枝，保持树形美观，为来年丰产奠定基础",
            completionTime: null,
            completionDescription: null,
            attachmentImages: null
          }
        ],
        total: 10
      }
      
      // 模拟网络延迟
      setTimeout(() => {
        this.tasksList = mockData.rows
        this.total = mockData.total
        this.loading = false
      }, 500)
      
      // 真实API调用 (注释掉，用于实际开发)
      // listTasks(this.queryParams).then(response => {
      //   this.tasksList = response.rows
      //   this.total = response.total
      //   this.loading = false
      // })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        taskType: null,
        plotName: null,
        farmName: null,
        executor: null,
        executeTime: null,
        taskStatus: "0",
        taskDescription: null,
        completionTime: null,
        completionDescription: null,
        attachmentImages: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加农事任务"
    },
    /** 格式化表单数据 */
    formatFormData(rowData) {
      return {
        id: rowData.id,
        taskName: rowData.taskName || '',
        taskType: rowData.taskType ? rowData.taskType.toString() : '',
        plotName: rowData.plotName || '',
        farmName: rowData.farmName || '',
        executor: rowData.executor || '',
        executeTime: rowData.executeTime || null,
        taskStatus: rowData.taskStatus ? rowData.taskStatus.toString() : '0',
        taskDescription: rowData.taskDescription || '',
        completionTime: rowData.completionTime || null,
        completionDescription: rowData.completionDescription || '',
        attachmentImages: rowData.attachmentImages || null
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      
      // 如果是从表格行点击修改，直接使用行数据回填
      if (row && row.id) {
        this.form = this.formatFormData(row)
        this.open = true
        this.title = "修改农事任务"
      } else {
        // 如果是批量修改或者需要从服务器获取数据
        getTasks(id).then(response => {
          this.form = this.formatFormData(response.data)
          this.open = true
          this.title = "修改农事任务"
        })
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTasks(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addTasks(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除农事任务编号为"' + ids + '"的数据项？').then(function() {
        return delTasks(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/tasks/export', {
        ...this.queryParams
      }, `tasks_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
