<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="meterType">-->
<!--        <el-select-->
<!--          v-model="queryParams.meterType"-->
<!--          placeholder="仪表类型"-->
<!--          clearable-->
<!--          style="width: 240px"-->
<!--        >-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.meter_type"-->
<!--            :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="['energy:communicate: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="['energy:communicate: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="['energy:communicate: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="['energy:communicate:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="communicateList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="自增标识"
        align="center"
        prop="objid"
        v-if="false"
      />
      <el-table-column
        label="集中器编号"
        align="center"
        prop="collectDeviceId"
        min-width="110"
      />
      <el-table-column
        label="终端时间"
        align="center"
        prop="terminalTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.terminalTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间间隔" align="center" prop="timeInterval" />
      <el-table-column label="仪表协议" align="center" prop="meterAgreement" />
      <el-table-column label="仪表类型" align="center" prop="meterType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.meter_type"
            :value="scope.row.meterType"
          />
        </template>
      </el-table-column>

<!--      能源也没找到对应的字典，也为使用，暂时先不使用-->
<!--      <el-table-column label="仪表状态" align="center" prop="meterStatus" />-->
      <el-table-column label="终端地址" align="center" prop="meterIp" />
      <el-table-column label="心跳频率" align="center" prop="heartInterval" />
      <el-table-column
        label="波特率一通道"
        align="center"
        prop="baudRateOne"
        min-width="110"
      />
      <el-table-column
        label="波特率二通道"
        align="center"
        prop="baudRateTwo"
        min-width="110"
      />
      <el-table-column
        label="波特率三通道"
        align="center"
        prop="baudRateThree"
        min-width="110"
      />
      <el-table-column
        label="波特率四通道"
        align="center"
        prop="baudRateFour"
        min-width="110"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="110"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:communicate:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:communicate: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="120px">
        <el-form-item label="集中器编号" prop="collectDeviceId">
          <el-row>
            <el-col :span="12">
              <el-input
                v-model="form.collectDeviceId"
                placeholder="请输入集中器编号"
              />
            </el-col>
            <el-col :span="12">
              <el-button type="primary" @click="selectDevice"
                >选择采集设备</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="终端时间" prop="terminalTime">
          <el-date-picker
            clearable
            v-model="form.terminalTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择终端时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时间间隔" prop="timeInterval">
          <el-input-number
            v-model="form.timeInterval"
            placeholder="请输入时间间隔"
          />
        </el-form-item>
        <el-form-item label="仪表协议" prop="meterAgreement">
          <el-input
            v-model="form.meterAgreement"
            placeholder="请输入仪表协议"
          />
        </el-form-item>
        <el-form-item label="仪表类型" prop="meterType">
          <el-radio-group v-model="form.meterType">
            <el-radio
              v-for="dict in dict.type.meter_type"
              :key="dict.value"
              :label="dict.value * 1"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="终端地址" prop="meterIp">
          <el-input v-model="form.meterIp" placeholder="请输入终端地址" />
        </el-form-item>
        <el-form-item label="心跳频率" prop="heartInterval">
          <el-input-number
            v-model="form.heartInterval"
            placeholder="请输入心跳频率"
          />
        </el-form-item>
        <el-form-item label="波特率一通道" prop="baudRateOne">
          <el-input-number
            v-model="form.baudRateOne"
            placeholder="请输入波特率一通道"
          />
        </el-form-item>
        <el-form-item label="波特率二通道" prop="baudRateTwo">
          <el-input-number
            v-model="form.baudRateTwo"
            placeholder="请输入波特率二通道"
          />
        </el-form-item>
        <el-form-item label="波特率三通道" prop="baudRateThree">
          <el-input-number
            v-model="form.baudRateThree"
            placeholder="请输入波特率三通道"
          />
        </el-form-item>
        <el-form-item label="波特率四通道" prop="baudRateFour">
          <el-input-number
            v-model="form.baudRateFour"
            placeholder="请输入波特率四通道"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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>

    <!-- 选择采集器对话框 -->
    <el-dialog
      :title="title1"
      :visible.sync="open1"
      width="800px"
      append-to-body
    >
      <el-form
        :model="queryParams1"
        ref="queryForm1"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="120px"
      >
        <el-form-item label="能源类型名称" prop="energyType">
          <el-select
            v-model="queryParams1.energyType"
            placeholder="能源类型名称"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.energy_type"
              :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="handleQuery1"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery1"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="deviceloading"
        :data="typeList"
        @selection-change="handleSelectionChange1"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="设备编号"
          align="center"
          prop="collectDeviceId"
        />
        <el-table-column
          label="设备名称"
          align="center"
          prop="collectDeviceName"
        />
        <el-table-column label="生产厂家" align="center" prop="manufacturer" />
        <el-table-column label="能源类型" align="center" prop="energyType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.energy_type"
              :value="scope.row.energyType"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="devicetotal > 0"
        :total="devicetotal"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getDeviceList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1"
          >确 定</el-button
        >
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCommunicate,
  getCommunicate,
  delCommunicate,
  addCommunicate,
  updateCommunicate,
} from "@/api/ems/base/communicate";
import { listInfo } from "@/api/ems/base/info";
export default {
  name: "Communicate",
  dicts: ["meter_type", "energy_type", "onlineState"],
  data() {
    return {
      // 遮罩层
      loading: true,
      deviceloading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 集中器通讯信息表格数据
      communicateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collectDeviceId: null,
        terminalTime: null,
        timeInterval: null,
        meterAgreement: null,
        meterType: null,
        meterStatus: null,
        meterIp: null,
        heartInterval: null,
        baudRateOne: null,
        baudRateTwo: null,
        baudRateThree: null,
        baudRateFour: null,
        deptId: null,
        userId: null,
      },
      // 表单参数
      form: {},
      title1: "",
      typeList: [],
      devicetotal: 0,
      // 表单校验
      rules: {},
      // 选中数组
      ids1: [],
      // 非单个禁用
      single1: true,
      // 非多个禁用
      multiple1: true,
      form1: {},
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        energyType: null,
      },
      open1: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询集中器通讯信息列表 */
    getList() {
      this.loading = true;
      listCommunicate(this.queryParams).then((response) => {
        this.communicateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        objid: null,
        collectDeviceId: null,
        terminalTime: null,
        timeInterval: null,
        meterAgreement: null,
        meterType: null,
        meterStatus: null,
        meterIp: null,
        heartInterval: null,
        baudRateOne: null,
        baudRateTwo: null,
        baudRateThree: null,
        baudRateFour: null,
        remark: null,
        deptId: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: 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.objid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.idsedit = null
      this.title = "添加集中器通讯信息";
    },

    /** 选择采集设备操作 */
    selectDevice() {
      if(this.form.objid != null){
      }else{
        this.reset();
      }

      this.getDeviceList();
      this.open1 = true;
      this.title1 = "选择采集器";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.idsedit = null
      const objid = row.objid || this.ids;
      console.log("row.objid", row.objid);
      console.log("this.ids", this.ids);
      console.log("objid", objid);
      getCommunicate(objid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改集中器通讯信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log("this.form.objid", this.form.objid);
          console.log(this.form);
          if (this.form.objid != null) {
            updateCommunicate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommunicate(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const objids = row.objid || this.ids;
      this.$modal
        .confirm('是否确认删除集中器通讯信息编号为"' + objids + '"的数据项？')
        .then(function () {
          return delCommunicate(objids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ems/communicate/export",
        {
          ...this.queryParams,
        },
        `communicate_${new Date().getTime()}.xlsx`
      );
    },
    // 取消按钮-------------------------------------------
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    // 表单重置
    reset1() {
      this.form1 = {
        energyType: null,
      };
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery1() {
      this.queryParams1.pageNum = 1;
      this.getDeviceList();
    },
    /** 重置按钮操作 */
    resetQuery1() {
      this.resetForm("queryForm1");
      this.handleQuery1();
    },
    /** 查询采集设备信息列表 */
    getDeviceList() {
      this.deviceloading = true;
      listInfo(this.queryParams1).then((response) => {
        this.typeList = response.rows;
        this.devicetotal = response.total;
        this.deviceloading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange1(selection) {
      this.ids1 = selection.map((item) => item.collectDeviceId);
      // if (this.form.objid != null) {
      // this.open1 = false;
      // } else {
      //   this.cancel1();
      // }
      // this.ids = selection.map((item) => item.objid);
      this.single1 = selection.length !== 1;
      this.multiple1 = !selection.length;
    },
    //
    submitForm1() {
      this.form.collectDeviceId = this.ids1.join();
      this.cancel1();
    },
  },
};
</script>
