<template>
  <div style="padding: 10px">
    <div class="listTitle">
      <span>备用电源信息</span>
      <span class="addBtn" @click="handleAdd">
        <i class="el-icon-plus"></i>
        添加
      </span>
    </div>

    <el-table v-loading="loading" :data="bydyList">
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center"
        fixed
      />

      <el-table-column
        label="备用电源名称"
        align="center"
        prop="backupPowerName"
        min-width="120"
      />
      <el-table-column
        label="电源功率"
        align="center"
        prop="powerSupplyPower"
        min-width="120"
      />
      <el-table-column
        label="采购时间"
        align="center"
        prop="procurementTime"
        min-width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.procurementTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="负责人名称"
        align="center"
        prop="personName"
        min-width="120"
      />
      <el-table-column
        label="负责人电话"
        align="center"
        prop="personPhone"
        min-width="120"
      />
      <el-table-column
        label="负责人职务"
        align="center"
        prop="personLevel"
        min-width="120"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sz:bydy:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sz:bydy: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="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="110px">
          <el-col :span="12">
            <el-form-item label="备用电源名称" prop="backupPowerName">
              <el-input
                v-model="form.backupPowerName"
                placeholder="请输入备用电源名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电源功率" prop="powerSupplyPower">
              <el-input
                v-model="form.powerSupplyPower"
                placeholder="请输入电源功率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购时间" prop="procurementTime">
              <el-date-picker
                clearable
                size="small"
                style="width: 100%"
                v-model="form.procurementTime"
                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="personName">
              <el-input
                v-model="form.personName"
                placeholder="请输入负责人名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人电话" prop="personPhone">
              <el-input
                v-model="form.personPhone"
                placeholder="请输入负责人电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人职务" prop="personLevel">
              <el-input
                v-model="form.personLevel"
                placeholder="请输入负责人职务"
              />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <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 {
  listBydy,
  getBydy,
  delBydy,
  addBydy,
  updateBydy,
  exportBydy,
} from "@/api/yg/bydy";

export default {
  name: "Bydy",
  props: ["prowerTypeId"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水闸备用电源信息表格数据
      bydyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: null,
        data: {
          bydyzbId: null,
          wagaId: null,
          backupPowerName: null,
          wagaName: null,
          powerSupplyPower: null,
          procurementTime: null,
          personName: null,
          personPhone: null,
          personLevel: null,
          createUid: null,
          updateUid: null,
          owerDept: null,
          relation: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sortBy: "desc",
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        backupPowerName: [
          { required: true, message: "请输入备用电源名称", trigger: "blur" },
        ],
        powerSupplyPower: [
          { required: true, message: "请输入电源功率", trigger: "blur" },
          {
            pattern:
              /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            message: "请输入数字,可保留两位小数",
            trigger: "blur",
          },
        ],
        procurementTime: [
          { required: true, message: "选择采购时间", trigger: "blur" },
        ],
        personName: [
          { required: true, message: "请输入负责人名称", trigger: "blur" },
        ],
        personPhone: [
          { required: true, message: "请输入负责人电话", trigger: "blur" },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        personLevel: [
          { required: true, message: "请输入负责人职务", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询水闸备用电源信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.data.bydyzbId = this.prowerTypeId;
      listBydy(this.queryParams).then((response) => {
        this.bydyList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wagaId: null,
        backupPowerName: null,
        wagaName: null,
        powerSupplyPower: null,
        procurementTime: null,
        personName: null,
        personPhone: null,
        personLevel: null,
        createUid: null,
        updateUid: null,
        createTime: null,
        updateTime: null,
        owerDept: null,
        remark: null,
        relation: null,
      };
      this.resetForm("form");
    },
    // 查询表单重置
    resetQueryForm() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        data: {
          id: null,
          wagaId: null,
          backupPowerName: null,
          wagaName: null,
          powerSupplyPower: null,
          procurementTime: null,
          personName: null,
          personPhone: null,
          personLevel: null,
          createUid: null,
          updateUid: null,
          createTime: null,
          updateTime: null,
          owerDept: null,
          remark: null,
          relation: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sortBy: "desc",
        },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryForm();
      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();
      const id = row.id || this.ids;
      getBydy(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) {
            updateBydy(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            this.form.bydyzbId = this.prowerTypeId;
            addBydy(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (ids) {
        this.$confirm("是否删除选中的数据？", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return delBydy(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      } else {
        this.$message.warning("请选择要删除的数据！！");
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/dialog.scss";
::v-deep {
  .el-dialog {
    margin-top: 25vh !important;
  }
}
.listTitle {
  font-size: 14px;
  padding-left: 10px;
  padding-right: 30px;
  margin: 20px 0;
  border-left: 2px solid #36b29e;
  display: flex;
  justify-content: space-between;
  span:nth-child(1) {
    font-weight: bold;
  }
  .addBtn {
    color: #36b29e;
    cursor: pointer;
  }
  .addBtn:hover {
    color: #31a08e;
  }
}
.listTitle:nth-child(1) {
  margin-top: 0;
}
</style>
