<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="machineCode">
        <el-input
          v-model="queryParams.machineCode"
          placeholder="请输入农机编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农机名称" prop="machineName">
        <el-input
          v-model="queryParams.machineName"
          placeholder="请输入农机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农机类型" prop="machineType">
        <el-select v-model="queryParams.machineType" placeholder="请选择农机类型" clearable>
          <el-option
            v-for="dict in dict.type.agricultural_machine_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="厂家" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属农场ID" prop="farmId">
        <el-input
          v-model="queryParams.farmId"
          placeholder="请输入所属农场ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="managerId">
        <el-input
          v-model="queryParams.managerId"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用时间" prop="purchaseDate">
        <el-date-picker clearable
          v-model="queryParams.purchaseDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择启用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="生产日期" prop="manufactureDate">
        <el-date-picker clearable
          v-model="queryParams.manufactureDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="当前状态" prop="onlineStatus">
        <el-select v-model="queryParams.onlineStatus" placeholder="请选择当前状态" clearable>
          <el-option
            v-for="dict in dict.type.agricultural_online_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="当前任务" prop="currentTask">
        <el-input
          v-model="queryParams.currentTask"
          placeholder="请输入当前任务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前地块" prop="currentField">
        <el-input
          v-model="queryParams.currentField"
          placeholder="请输入当前地块"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保养状态" prop="maintenanceStatus">
        <el-select v-model="queryParams.maintenanceStatus" placeholder="请选择保养状态" clearable>
          <el-option
            v-for="dict in dict.type.agricultural_maintenance_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="定位状态" prop="locationStatus">
        <el-select v-model="queryParams.locationStatus" placeholder="请选择定位状态" clearable>
          <el-option
            v-for="dict in dict.type.agricultural_location_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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:machines:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:machines:edit']"
        >修改</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:machines: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:machines:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="machinesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="农机ID" align="center" prop="id" />
      <el-table-column label="农机编号" align="center" prop="machineCode" />
      <el-table-column label="农机名称" align="center" prop="machineName" />
      <el-table-column label="农机类型" align="center" prop="machineType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agricultural_machine_type" :value="scope.row.machineType"/>
        </template>
      </el-table-column>
      <el-table-column label="厂家" align="center" prop="manufacturer" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="所属农场ID" align="center" prop="farmId" />
      <el-table-column label="所属地块区域" align="center" prop="fieldArea" />
      <el-table-column label="负责人" align="center" prop="managerId" />
      <el-table-column label="启用时间" align="center" prop="purchaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="manufactureDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manufactureDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="onlineStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agricultural_online_status" :value="scope.row.onlineStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="当前任务" align="center" prop="currentTask" />
      <el-table-column label="当前地块" align="center" prop="currentField" />
      <el-table-column label="保养状态" align="center" prop="maintenanceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agricultural_maintenance_status" :value="scope.row.maintenanceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="定位状态" align="center" prop="locationStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agricultural_location_status" :value="scope.row.locationStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="报警次数" align="center" prop="alarmCount" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:machines:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:machines: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="农机编号" prop="machineCode">
          <el-input v-model="form.machineCode" placeholder="请输入农机编号" />
        </el-form-item>
        <el-form-item label="农机名称" prop="machineName">
          <el-input v-model="form.machineName" placeholder="请输入农机名称" />
        </el-form-item>
        <el-form-item label="农机类型" prop="machineType">
          <el-select v-model="form.machineType" placeholder="请选择农机类型">
            <el-option
              v-for="dict in dict.type.agricultural_machine_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入厂家" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="所属农场ID" prop="farmId">
          <el-input v-model="form.farmId" placeholder="请输入所属农场ID" />
        </el-form-item>
        <el-form-item label="所属地块区域" prop="fieldArea">
          <el-input v-model="form.fieldArea" placeholder="请输入所属地块区域" />
        </el-form-item>
        <el-form-item label="负责人" prop="managerId">
          <el-input v-model="form.managerId" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="启用时间" prop="purchaseDate">
          <el-date-picker clearable
            v-model="form.purchaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择启用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生产日期" prop="manufactureDate">
          <el-date-picker clearable
            v-model="form.manufactureDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前状态" prop="onlineStatus">
          <el-radio-group v-model="form.onlineStatus">
            <el-radio
              v-for="dict in dict.type.agricultural_online_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="当前任务" prop="currentTask">
          <el-input v-model="form.currentTask" placeholder="请输入当前任务" />
        </el-form-item>
        <el-form-item label="当前地块" prop="currentField">
          <el-input v-model="form.currentField" placeholder="请输入当前地块" />
        </el-form-item>
        <el-form-item label="保养状态" prop="maintenanceStatus">
          <el-radio-group v-model="form.maintenanceStatus">
            <el-radio
              v-for="dict in dict.type.agricultural_maintenance_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="定位状态" prop="locationStatus">
          <el-radio-group v-model="form.locationStatus">
            <el-radio
              v-for="dict in dict.type.agricultural_location_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报警次数" prop="alarmCount">
          <el-input v-model="form.alarmCount" placeholder="请输入报警次数" />
        </el-form-item>
        <el-form-item label="农机描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="农机图片" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入农机图片" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </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 { listMachines, getMachines, delMachines, addMachines, updateMachines } from "@/api/base/machines"

export default {
  name: "Machines",
  dicts: ['agricultural_machine_type', 'agricultural_maintenance_status', 'agricultural_online_status', 'agricultural_location_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农机管理列表表格数据
      machinesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        machineCode: null,
        machineName: null,
        machineType: null,
        manufacturer: null,
        model: null,
        farmId: null,
        managerId: null,
        purchaseDate: null,
        manufactureDate: null,
        onlineStatus: null,
        currentTask: null,
        currentField: null,
        maintenanceStatus: null,
        locationStatus: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        machineCode: [
          { required: true, message: "农机编号不能为空", trigger: "blur" }
        ],
        machineName: [
          { required: true, message: "农机名称不能为空", trigger: "blur" }
        ],
        machineType: [
          { required: true, message: "农机类型不能为空", trigger: "change" }
        ],
        farmId: [
          { required: true, message: "所属农场ID不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询农机管理列表列表 */
    getList() {
      this.loading = true
      listMachines(this.queryParams).then(response => {
        this.machinesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        machineCode: null,
        machineName: null,
        machineType: null,
        manufacturer: null,
        model: null,
        farmId: null,
        fieldArea: null,
        managerId: null,
        purchaseDate: null,
        manufactureDate: null,
        onlineStatus: null,
        currentTask: null,
        currentField: null,
        maintenanceStatus: null,
        locationStatus: null,
        alarmCount: null,
        description: null,
        imageUrl: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: 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 = "添加农机管理列表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMachines(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改农机管理列表"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMachines(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMachines(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 delMachines(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/machines/export', {
        ...this.queryParams
      }, `machines_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
