<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="recordCode">
        <el-input
          v-model="queryParams.recordCode"
          placeholder="请输入记录编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对象类型" prop="objType">
        <el-select v-model="queryParams.objType" placeholder="请选择对象类型" clearable>
          <el-option
            v-for="dict in dict.type.device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="对象代码" prop="objCode">
        <el-input
          v-model="queryParams.objCode"
          placeholder="请输入对象代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对象名称" prop="objName">
        <el-input
          v-model="queryParams.objName"
          placeholder="请输入对象名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维护标题" prop="maintainTitle">
        <el-input
          v-model="queryParams.maintainTitle"
          placeholder="请输入维护标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维护人" prop="maintainPerson">
        <el-input
          v-model="queryParams.maintainPerson"
          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-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="['ems:rbook: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="['ems:rbook: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="['ems:rbook: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="['ems:rbook:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rbookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="记录编号" align="center" prop="recordCode" />
      <el-table-column label="对象类型" align="center" prop="objType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.objType"/>
        </template>
      </el-table-column>
      <el-table-column label="对象代码" align="center" prop="objCode" />
      <el-table-column label="对象名称" align="center" prop="objName" />
      <el-table-column label="安装位置" align="center" prop="insLocation" />
      <el-table-column label="维护标题" align="center" prop="maintainTitle" />
      <el-table-column label="维护内容" align="center" prop="maintainContent" />
      <el-table-column label="维护人" align="center" prop="maintainPerson" />
      <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="['ems:rbook:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ems:rbook: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="recordCode">
          <el-input v-model="form.recordCode" placeholder="请输入记录编号" />
        </el-form-item>
<!--        prop="objType"-->
        <el-form-item label="对象类型" >
          <el-select v-model="form.objType" placeholder="请选择对象类型" @change="changeObjOptions" >
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对象代码" prop="objCode" >
          <el-select v-model="form.objCode" placeholder="请输入对象代码" @change="handleSelect"  >
            <el-option
              v-for="item in AllCode"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="对象名称" prop="objName" >
          <el-input v-model="form.objName" placeholder="请输入对象名称" />
        </el-form-item>
        <el-form-item label="安装位置" prop="insLocation">
          <el-input v-model="form.insLocation" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="维护标题" prop="maintainTitle">
          <el-input v-model="form.maintainTitle" placeholder="请输入维护标题" />
        </el-form-item>
        <el-form-item label="维护内容">
          <editor v-model="form.maintainContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="维护人" prop="maintainPerson">
          <el-input v-model="form.maintainPerson" 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 { listRbook, getRbook, delRbook, addRbook, updateRbook } from "@/api/device/rbook";
import {listFacs} from '@/api/basecfg/emsfacs'
import { listDevRecursionByArea } from '@/api/device/device'
export default {
  name: "Rbook",
  dicts: ['device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备台账表格数据
      rbookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //能源设施全部数据
      AllName:[],
      AllCode:[],
      facsLocation:'',
      All:[],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordCode: null,
        objType: null,
        objCode: null,
        objName: null,
        insLocation: null,
        maintainTitle: null,
        maintainContent: null,
        maintainPerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recordCode: [
          { required: true, message: "记录编号不能为空", trigger: "blur" }
        ],
        objType: [
          { required: true, message: "对象类型 1：设施 2：设备不能为空", trigger: "change" }
        ],
        objCode: [
          { required: true, message: "对象代码不能为空", trigger: "blur" }
        ],
        maintainTitle: [
          { required: true, message: "维护标题不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备台账列表 */
    getList() {
      this.loading = true;
      listRbook(this.queryParams).then(response => {
        this.rbookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        recordCode: null,
        objType: null,
        objCode: null,
        objName: null,
        insLocation: null,
        maintainTitle: null,
        maintainContent: null,
        maintainPerson: 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
    },

    handleSelect(value){
      //获取能源设施所有信息
      this.loading = true
      listFacs(this.queryParams).then(response => {
        //总条数
        this.total = response.total
        //循环输出
        for (let i = 0; i <this.total ; i++) {
          if (value === response.rows[i].facsCode){
            this.form.objName = response.rows[i].facsName
            this.form.insLocation=response.rows[i].refAreaName
          }
        }
        this.loading = false
      })

      ///获取能源设备所有信息
      this.loading = true
      listDevRecursionByArea(this.queryParams).then(response => {
        this.total = response.total
        //循环输出
        for (let i = 0; i <this.total ; i++) {
          if (value === response.rows[i].deviceCode){
            this.form.objName = response.rows[i].deviceName
            this.form.insLocation=response.rows[i].areaPath
          }
        }
        this.loading = false
      })

    },

    changeObjOptions(objType) {
      if(objType == 1){
        //获取能源设施所有信息
        this.loading = true
        listFacs(this.queryParams).then(response => {
          //总条数
          this.total = response.total
          this.AllCode = [];
          //循环输出
          for (let i = 0; i <=this.total ; i++) {
            this.AllCode.push({ value:response.rows[i].facsCode, label: response.rows[i].facsCode}); // 添加到数组
          }
          this.loading = false
        })
      }else if(objType == 0){
        ///获取能源设备所有信息
        this.loading = true
        listDevRecursionByArea(this.queryParams).then(response => {
          this.total = response.total
          this.AllCode = [];
          //循环输出
          for (let i = 0; i <this.total ; i++) {
            this.AllCode.push({ value:response.rows[i].deviceCode, label: response.rows[i].deviceCode}); // 添加到数组
          }
          this.loading = false
        })
      }
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRbook(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) {
            updateRbook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRbook(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 delRbook(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ems/rbook/export', {
        ...this.queryParams
      }, `rbook_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
