<template>
  <div class="">
    <el-breadcrumb separator="/" style="margin-bottom: 20px">
      <el-breadcrumb-item
        v-for="(item, index) in routeList"
        :key="item + index"
        :to="{ path: item.path }"
      >
        {{ item.routeName }}
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="关联堤防" prop="dikeName">
        <el-input
          v-model="queryParams.cv.value"
          placeholder="请输入堤防名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-button
            type="primary"
            slot="append"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
          ></el-button>
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="关联堤防" prop="dikeCode">
        <el-select
          v-model="queryParams.data.dikeCode"
          placeholder="请选择关联堤防"
          style="width: 100%"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dikeNameOptions"
            :key="dict.id"
            :label="dict.dikeName"
            :value="dict.dikeCode"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="备用电源种类" prop="powerType" label-width="96px">
        <el-select
          v-model="queryParams.data.powerType"
          placeholder="请选择备用电源种类"
          clearable
          size="small"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in powerTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <!-- <el-button
          type="cyan"
          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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['df:bydyzb:add']"
          >新增电源种类</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['df:bydyzb:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['df:bydyzb:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bydyzbList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center"
        fixed
      />
      <el-table-column
        label="关联堤防"
        align="center"
        prop="dikeName"
        min-width="120"
      />
      <!-- <el-table-column
        label="关联堤防"
        align="center"
        prop="dikeCode"
        :formatter="dikeCodeFormat"
        min-width="120"
      /> -->
      <el-table-column
        label="备用电源数量（字段未有）"
        align="center"
        prop="powerNumber"
        min-width="120"
      />
      <el-table-column
        label="备用电源种类"
        align="center"
        prop="powerType"
        :formatter="powerTypeFormat"
        min-width="120"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="210"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['df:bydyzb:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="addPowerList(scope.row)"
            v-hasPermi="['df:bydyzb:addPowerList']"
            >新增电源</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['df:bydyzb: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-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="关联堤防" prop="dikeCode">
          <linkDike :form="form" v-if="open" />
          <!-- <el-select
            v-model="form.dikeCode"
            placeholder="请选择关联堤防"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dikeNameOptions"
              :key="dict.id"
              :label="dict.dikeName"
              :value="dict.dikeCode"
            ></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="备用电源种类" prop="powerType">
          <el-select
            v-model="form.powerType"
            placeholder="请选择备用电源种类"
            style="width: 100%"
          >
            <el-option
              v-for="dict in powerTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </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="addTitle"
      :visible.sync="addOpen"
      width="1200px"
      append-to-body
      :close-on-click-modal="false"
      @close="close"
    >
      <el-tabs v-model="activeName" style="margin-bottom: 20px" v-if="addOpen">
        <el-tab-pane label="备用电源信息" name="powerList" :lazy="true">
          <powerList ref="powerList" :prowerTypeId="prowerTypeId" />
        </el-tab-pane>
        <el-tab-pane label="试运行记录" name="powerRecords" :lazy="true">
          <powerRecords
            :prowerTypeId="prowerTypeId"
            v-if="activeName === 'powerRecords'"
          />
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBydyzb,
  getBydyzb,
  delBydyzb,
  addBydyzb,
  updateBydyzb,
  exportBydyzb,
} from "@/api/yg/dike/bydyzb";
import { listJbxx, getJbxx } from "@/api/yg/dike/jbxx";
import powerList from "./components/powerList.vue";
import powerRecords from "./components/powerRecords.vue";
import linkDike from "@/components/linkNameSelect/linkDike.vue";

export default {
  name: "Bydyzb",
  components: {
    powerList,
    powerRecords,
    linkDike,
  },
  data() {
    return {
      // 面包屑，路由信息
      routeList: [
        {
          path: "/dike/safetyManage/floodControlManagement/power",
          routeName: "防汛电源管理",
        },
      ],
      activeName: "powerList",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 堤防备用电源主表格数据
      bydyzbList: [],
      // 弹出层标题
      title: "",
      addTitle: "",
      // 是否显示弹出层
      open: false,
      addOpen: false,
      // 电源种类字典
      powerTypeOptions: [],
      // 堤防项目
      dikeNameOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: null,
        data: {
          dikeCode: null,
          powerType: null,
          powerModel: null,
          dikeName: null,
          createUid: null,
          updateUid: null,
          owerDept: null,
          relation: null,
        },
        cv: {
          type: "like",
          name: "dikeName",
          value: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sortBy: "desc",
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dikeCode: [
          { required: true, message: "请选择关联堤防", trigger: "change" },
        ],
        powerType: [
          { required: true, message: "请选择备用电源种类", trigger: "change" },
        ],
      },
      prowerTypeId: "",
    };
  },
  created() {
    this.getList();
    listJbxx({ data: {} }).then((res) => {
      this.dikeNameOptions = res.records;
    });
    this.getDicts("type_of_power").then((response) => {
      this.powerTypeOptions = response.data;
    });
  },
  methods: {
    close() {
      this.activeName = "powerList";
    },
    addPowerList(row) {
      this.prowerTypeId = row.id;
      this.addTitle = "管理备用电源信息";
      this.addOpen = true;
    },
    /** 查询堤防备用电源主列表 */
    getList() {
      this.loading = true;
      listBydyzb(this.queryParams).then((response) => {
        this.bydyzbList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    dikeCodeFormat(row) {
      const dikeCode = row.dikeCode;
      let data = this.dikeNameOptions.find((res) => res.dikeCode == dikeCode);
      if (data) return data.dikeName;
    },
    // 电源种类字典翻译
    powerTypeFormat(row, column) {
      return this.selectDictLabel(this.powerTypeOptions, row.powerType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dikeCode: null,
        powerType: null,
        powerModel: null,
        dikeName: 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,
          dikeCode: null,
          powerType: null,
          powerModel: null,
          dikeName: null,
          createUid: null,
          updateUid: null,
          createTime: null,
          updateTime: null,
          owerDept: null,
          remark: null,
          relation: null,
        },
        cv: {
          type: "like",
          name: "dikeName",
          value: 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;
      getBydyzb(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) {
            updateBydyzb(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addBydyzb(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 delBydyzb(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: 2vh !important;
  // }
  .el-tabs__content {
    height: 500px;
    overflow: auto;
  }
}
</style>
