<template>
  <div class="app-container">
    <new-card cardTitle="气象设备管理">
      <el-row :gutter="10" class="mb8 mt10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
      </el-row>
    </new-card>
    <new-card cardTitle="气象设备列表">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
        class="mt10"
      >
        <el-form-item label="气象设备名称" prop="monName">
          <el-input
            v-model="queryParams.monName"
            placeholder="请输入气象设备名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="监测编号" prop="monNumber">
          <el-input
            v-model="queryParams.monNumber"
            placeholder="请输入监测编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="eqListData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="50" label="序号">
        </el-table-column>
        <el-table-column label="气象设备名称" align="center" prop="monName" />
        <el-table-column label="监测项目" align="center" prop="monType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.slope_mon_me_eq"
              :value="scope.row.monType"
            />
          </template>
        </el-table-column>
        <el-table-column label="气象设备编号" align="center" prop="monNumber" />
        <el-table-column
          label="安装日期"
          align="center"
          prop="installationdata"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.installationdata, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="生产厂家" align="center" prop="manufacture" />

        <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)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</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"
      />
    </new-card>

    <!-- 添加或修改监测气象设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-col :span="12">
          <el-form-item label="气象设备名称" prop="monName">
            <el-input v-model="form.monName" placeholder="请输入气象设备名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="监测项目" prop="monType">
            <el-select
              v-model="form.monType"
              placeholder="请输入监测项目"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.slope_mon_me_eq"
                :key="dict.value"
                :label="dict.label"
                :value="Number(dict.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="气象设备编号" prop="monNumber">
            <el-input
              v-model="form.monNumber"
              placeholder="请输入气象设备编号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装日期" prop="installationdata">
            <el-date-picker
              clearable
              v-model="form.installationdata"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择安装日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂家" prop="manufacture">
            <el-input v-model="form.manufacture" placeholder="请输入生产厂家" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="预警阈值" prop="alarmvlaue">
            <el-input v-model="form.alarmvlaue" placeholder="请输入预警阈值" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="监测数据单位" prop="monUnit">
            <el-input v-model="form.monUnit" placeholder="请输入监测数据单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="初始值" prop="initial">
            <el-input v-model="form.initial" placeholder="请输入传感器初始值" />
          </el-form-item>
        </el-col>
      </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 {
  pointmonList,
  eqmeList,
  eqmeDel,
  eqmeAdd,
  eqmeEdit,
} from "@/api/lclApi/lclapi";
import cardLcl from "@/components/lclCard/index.vue";
export default {
  name: "Eq",
  dicts: ["slope_mon_me_eq"],
  components: { "new-card": cardLcl },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测气象设备表格数据
      eqListData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        monNumber: null,
        monName: null,
        monType: null,
        installationdata: null,
        manufacture: null,
        initial: null,
        alarmvlaue: null,
        monUnit: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //监测点位
      mondicData: [{ pointName: "1-1测斜仪", id: 1 }],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getMonDic();
  },
  methods: {
    // 监测点位
    getMonDic() {
      let that = this;
      pointmonList({ pageNum: 1, pageSize: 9999 }).then((res) => {
        if (res && res.code === 200 && res.rows) {
          that.mondicData = res.rows;
        }
      });
    },
    /** 查询监测气象设备列表 */
    getList() {
      this.loading = true;
      eqmeList(this.queryParams).then((response) => {
        this.eqListData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        monNumber: null,
        monName: null,
        monType: null,
        installationdata: null,
        manufacture: null,
        initial: null,
        alarmvlaue: null,
        monUnit: null,
        createTime: null,
        updateTime: 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) {
      const id = row.id || this.ids;
      this.open = true;
      this.title = "修改监测气象设备";
      this.form = row;
      // getEq(id).then((response) => {
      //   this.form = response.data;

      // });
    },
    /** 提交按钮 */
    submitForm() {
      let that = this;
      if (this.form.id != null) {
        eqmeEdit(this.form).then((res) => {
          if (res && res.code === 200) {
            that.$modal.msgSuccess("修改成功");
            that.open = false;
            that.getList();
          }
        });
      } else {
        eqmeAdd(this.form).then((res) => {
          if (res && res.code === 200) {
            that.$modal.msgSuccess("新增成功");
            that.open = false;
            that.getList();
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let that = this;
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          eqmeDel(ids).then((res) => {
            if (res.code === 200 && res) {
              that.getList();
              that.$modal.msgSuccess("删除成功");
            }
          });
        })
        .catch(() => {});
    },
    test() {
      console.log(11);
    },
  },
};
</script>
