<template>
  <div class="app-container">
    <div class="btn-condition">
      <div class="condition-left" v-show="showSearch">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="设备名称" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="资产编号" prop="assetNum">
            <el-input
              v-model="queryParams.assetNum"
              placeholder="请输入资产编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备类型" prop="deviceType">
            <el-select
              v-model="queryParams.deviceType"
              placeholder="请选择设备类型"
              clearable
              filterable
            >
              <el-option
                v-for="dict in dict.type.sys_device_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="日期范围" prop="timeArr">
            <el-date-picker
              style="width: 220px"
              v-model="timeArr"
              type="daterange"
              unlink-panels
              range-separator="~"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="dateChange"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div class="primary-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </div>
        <div class="cancel-btn">
          <el-button
            type="cancel"
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
            >重置</el-button
          >
        </div>
      </div>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <div class="primary-btn" v-hasPermi="['device:ledger:add']">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </div>
      </el-col>
      <el-col :span="1.5">
        <div class="success-btn" v-hasPermi="['device:ledger:edit']">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            >修改</el-button
          >
        </div>
      </el-col>
      <el-col :span="1.5">
        <div class="delete-btn" v-hasPermi="['device:ledger:remove']">
          <el-button
            type="delete"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </div>
      </el-col>
      <!-- <el-col :span="1.5">
        <div class="warning-btn" v-hasPermi="['system:post:export']">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出</el-button
          >
        </div>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
      stripe
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="规格与型号" align="center" prop="modelSpecs" />
      <el-table-column label="资产编号" align="center" prop="assetNum" />
      <el-table-column label="关联二维码Id" align="center" prop="qrCodeId" />
      <el-table-column label="预计使用寿命" align="center" prop="serviceLife" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_device_type"
            :value="scope.row.deviceType"
          />
        </template>
      </el-table-column>
      <el-table-column label="技术参数描述" align="center" prop="techParams" />
      <el-table-column label="设备文档" align="center" prop="techDoc" />
      <el-table-column label="安装日期" align="center" prop="installTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.installTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否需要采集" align="center" prop="isCollection">
        <template slot-scope="scope">
          <span v-if="scope.row.isCollection === 0">是</span>
          <span v-if="scope.row.isCollection === 1">否</span>
        </template>
      </el-table-column>
      <el-table-column label="是否计算能效" align="center" prop="isCalcEnergy">
        <template slot-scope="scope">
          <span v-if="scope.row.isCalcEnergy === 0">是</span>
          <span v-if="scope.row.isCalcEnergy === 1">否</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0">正常</el-tag>
          <el-tag type="danger" v-if="scope.row.status === 1">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="系统类型" align="center" prop="systemType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_type"
            :value="scope.row.systemType"
          />
        </template>
      </el-table-column>
      <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="['device:ledger:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:ledger: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" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input
                v-model="form.deviceName"
                placeholder="请输入设备名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型" prop="deviceType">
              <el-select
                v-model="form.deviceType"
                placeholder="请选择设备类型"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in dict.type.sys_device_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="规格与型号" prop="modelSpecs">
              <el-input
                v-model="form.modelSpecs"
                placeholder="请输入规格与型号"
                clearable
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="资产编号" prop="assetNum">
              <el-input
                v-model="form.assetNum"
                placeholder="请输入资产编号"
                clearable
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="预计使用寿命" prop="serviceLife">
              <el-input
                v-model="form.serviceLife"
                placeholder="请输入预计使用寿命"
                clearable
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="技术参数描述" prop="techParams">
              <el-input
                v-model="form.techParams"
                placeholder="请输入技术参数描述"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="设备文档" prop="techDoc">
              <el-input
                v-model="form.techDoc"
                placeholder="请输入设备文档"
                clearable
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="设备安装时间" prop="installTime">
              <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="form.installTime"
                type="datetime"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="form.status"
                placeholder="请选择"
                clearable
                filterable
              >
                <el-option label="正常" value="0" />
                <el-option label="停用" value="1" /> </el-select></el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="是否需要采集" prop="isCollection">
              <el-select
                v-model="form.isCollection"
                placeholder="请选择是否"
                clearable
                filterable
              >
                <el-option label="是" value="0" />
                <el-option label="否" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-form-item label="是否计算能效" prop="isCalcEnergy">
              <el-select
                v-model="form.isCalcEnergy"
                placeholder="请选择是否"
                clearable
                filterable
              >
                <el-option label="是" value="0" />
                <el-option label="否" value="1" />
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="系统类型" prop="systemType">
              <el-select
                v-model="form.systemType"
                placeholder="请选择系统类型"
                clearable
                filterable
              >
                <el-option
                  v-for="dict in dict.type.sys_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  ledgerList,
  getLedger,
  delGateway,
  addLedger,
  updateLedger,
} from "@/api/device/ledger";
export default {
  name: "ledger",
  dicts: ["sys_device_type", "sys_type"],
  data() {
    return {
      timeArr: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网关表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beginTime: "",
        endTime: "",
        deviceName: "",
        assetNum: "",
        deviceType: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        assetNum: [
          { required: true, message: "资产编号不能为空", trigger: "blur" },
        ],
        installTime: [
          { required: true, message: "请选择安装时间", trigger: "blur" },
        ],
        deviceType: [
          { required: true, message: "请选择设备类型", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        isCollection: [
          { required: true, message: "请选择是否", trigger: "blur" },
        ],
        modelSpecs: [
          { required: true, message: "规格型号不能为空", trigger: "blur" },
        ],
        isCalcEnergy: [
          { required: true, message: "请选择是否", trigger: "blur" },
        ],
        systemType: [
          { required: true, message: "请选择系统类型", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 选中日期事件
    dateChange() {
      // console.log("打印时间", this.timeArr);
      if (!this.timeArr) {
        this.$nextTick(() => {
          this.timeArr = [];
          this.queryParams.beginTime = "";
          this.queryParams.endTime = "";
        });
      } else {
        this.queryParams.beginTime = this.timeArr[0];
        this.queryParams.endTime = this.timeArr[1];
      }
    },
    /** 查询设备台账列表 */
    getList() {
      this.loading = true;
      ledgerList(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.form.id = "";
    },
    // 表单重置
    reset() {
      this.form = {
        deviceName: "",
        deviceType: "",
        modelSpecs: "",
        assetNum: "",
        qrCodeId: "",
        serviceLife: "",
        techParams: "",
        techDoc: "",
        installTime: "",
        isCollection: "",
        isCalcEnergy: "",
        status: "",
        remark: "",
      };
      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();
      this.form.id = "";
      const ledgerId = row.id || this.ids;
      console.log("row", row);
      getLedger(ledgerId).then((response) => {
        // this.form = response.data;
        this.form = {
          id: response.data.id,
          deviceName: response.data.deviceName,
          deviceType: response.data.deviceType,
          modelSpecs: response.data.modelSpecs,
          assetNum: response.data.assetNum,
          qrCodeId: response.data.qrCodeId,
          serviceLife: response.data.serviceLife,
          techParams: response.data.techParams,
          techDoc: response.data.techDoc,
          installTime: response.data.installTime,
          isCollection:
            response.data.isCollection !== null
              ? response.data.isCollection.toString()
              : null,
          isCalcEnergy:
            response.data.isCalcEnergy !== null
              ? response.data.isCalcEnergy.toString()
              : null,
          status:
            response.data.status !== null
              ? response.data.status.toString()
              : null,
          systemType:
            response.data.systemType !== null
              ? response.data.systemType.toString()
              : null,
          remark: response.data.remark,
        };
        this.open = true;
        this.title = "修改设备台账信息";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateLedger(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("新增参数", this.form);
            addLedger(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 delGateway(Ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "device/gateway/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
/* 媒体查询，适配大于2000px分辨率的大屏样式 - element样式 */
@media (max-width: 2000px) {
  ::v-deep .el-dialog {
    width: 700px !important;
  }

  ::v-deep .el-dialog .el-form-item__label {
    width: 150px !important;
  }
  ::v-deep .el-dialog .el-form-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
@media (min-width: 2000px) {
  ::v-deep .el-dialog {
    width: 7.5rem !important;
  }

  ::v-deep .el-dialog .el-form-item__label {
    width: 1.5rem !important;
  }
  ::v-deep .el-dialog .el-form-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
}
</style>
