<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="所属老化柜编号" prop="fEquipmentCode">
        <el-input
          v-model="queryParams.fEquipmentCode"
          placeholder="请输入所属老化柜编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="层编号" prop="fStoreyCode">
        <el-input
          v-model="queryParams.fStoreyCode"
          placeholder="请输入层编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP地址" prop="fIp">
        <el-input
          v-model="queryParams.fIp"
          placeholder="请输入IP地址"
          clearable
          size="small"
          @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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="所属老化柜编号" align="center" prop="fEquipmentCode" />
      <el-table-column label="层编号" align="center" prop="fStoreyCode" />
      <el-table-column label="IP地址" align="center" prop="fIp" />
      <el-table-column label="端口号" align="center" prop="fPort" />
      <el-table-column label="状态" align="center" prop="fStatus" >
        <template slot-scope="scope">
          <!-- 空闲状态 - 灰色（info） -->
          <el-tag type="info" v-if="scope.row.fStatus === '0'">空闲</el-tag>
          <!-- 运行状态 - 绿色（success） -->
          <el-tag type="success" v-else-if="scope.row.fStatus === '1'">运行</el-tag>
          <!-- 故障状态 - 红色（danger） -->
          <el-tag type="danger" v-else-if="scope.row.fStatus === '2'">故障</el-tag>
          <!-- 断电状态 - 橙色（warning） -->
          <el-tag type="warning" v-else-if="scope.row.fStatus === '3'">断电</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报警时间" align="center" prop="fAlarmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fAlarmTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="老化开始时间" align="center" prop="fAgingStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fAgingStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下料指令" align="center" prop="blankingCommand" />
      <el-table-column label="上料指令" align="center" prop="feedingCommand" />
      <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>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlePowerOn(scope.row)"
          >上电</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlePowerOutage(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"
    />

    <!-- 添加或修改柜层信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属老化柜编号" prop="fEquipmentId">
              <el-input v-model="form.fEquipmentCode" :disabled="form.fStoreyId !== null"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="层编号" prop="fStoreyCode">
              <el-input v-model="form.fStoreyCode" placeholder="请输入层编号" :disabled="form.fStoreyId !== null"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="IP地址" prop="fIp">
              <el-input v-model="form.fIp" placeholder="请输入IP地址" :disabled="form.fStoreyId !== null"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口号" prop="fPort">
              <el-input v-model="form.fPort" placeholder="请输入端口号" :disabled="form.fStoreyId !== null"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下料指令" prop="blankingCommand">
              <el-input v-model="form.blankingCommand" placeholder="请输入这一层的下料的指令" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上料指令" prop="feedingCommand">
              <el-input v-model="form.feedingCommand" placeholder="请输入这一层的上料的指令" />
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listStorey, getStorey, delStorey, addStorey, updateStorey, exportStorey,PowerOn,PowerOutage } from "@/api/storey/storey";

export default {
  name: "Info",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 柜层信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fEquipmentCode: null,
        fStoreyCode: null,
        fIp: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fEquipmentCode: [
          { required: true, message: "设备编号不能为空", trigger: "blur" },
        ],
        fStoreyCode: [
          { required: true, message: "楼层编号不能为空", trigger: "blur" },
        ],
        fIp: [
          { required: true, message: "IP地址不能为空", trigger: "blur" },
        ],
        fPort: [
          { required: true, message: "端口号不能为空", trigger: "blur" },
        ],
        blankingCommand: [
          { required: true, message: "下料指令不能为空", trigger: "blur" }
        ],
        feedingCommand: [
          { required: true, message: "上料指令不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询柜层信息列表 */
    getList() {
      this.loading = true;
      listStorey(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fStoreyId: null,
        fEquipmentId: null,
        fStoreyCode: null,
        fIp: null,
        fPort: null,
        fStatus: "0",
        fUpdateTime: null,
        fCreateTime: null,
        fAlarmTime: null,
        fAgingStartTime: null,
        blankingCommand: null,
        feedingCommand: 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.fStoreyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加柜层信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fStoreyId = row.fStoreyId || this.ids
      getStorey(fStoreyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改柜层信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fStoreyId != null) {
            updateStorey(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorey(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //上电
    handlePowerOn(row) {
      const fIp = row.fIp;
      this.$confirm('是否确认给设备IP为"' + fIp + '"的设备上电?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return PowerOn(row);
      }).then(() => {
        this.getList();
        this.msgSuccess("上电成功");
      }).catch(() => {});
    },
    // 断电
    handlePowerOutage(row) {
      const fIp = row.fIp;
      this.$confirm('是否确认给设备IP为"' + fIp + '"的设备断电?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return PowerOutage(row);
      }).then(() => {
        this.getList();
        this.msgSuccess("断电成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fStoreyIds = row.fStoreyId || this.ids;
      this.$confirm('是否确认删除柜层信息编号为"' + fStoreyIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStorey(fStoreyIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有柜层信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportStorey(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
