<template>
  <div class="app-container scroll-auto">
    <el-container>
      <el-header style="text-align: center; display: block">
        <span style="font-weight: 600; font-size: 18px">配网参数信息</span>
      </el-header>
      <el-main class="nopadding">
        <div style="width: 50%; display: inline-block">
          <el-card header="停电超时长配置">
            <el-table
              ref="table"
              :data="tableData1"
              row-key="id"
              stripe
              height="300px"
            >
              <el-table-column prop="序号" label="序号" width="55">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="项目编码"
                prop="parCode"
                :show-overflow-tooltip="true"
                align="center"
              ></el-table-column>
              <el-table-column
                label="项目名称"
                prop="parName"
                width="150"
                align="center"
              ></el-table-column>
              <el-table-column
                label="项目对应值"
                prop="parValue"
                align="center"
              ></el-table-column>
              <el-table-column label="计量单位" prop="parUnit" align="center">
                <template slot-scope="scope">
                  <dict-tag
                    :options="dict.type.pdm_maintenance_time"
                    :value="scope.row.parUnit"
                  />
                </template>
              </el-table-column>
              <el-table-column label="状态" prop="status" align="center">
                <template slot-scope="scope">
                  <el-tag
                    type="success"
                    effect="dark"
                    v-if="scope.row.status == '1'"
                    >启用</el-tag
                  >
                  <el-tag type="info" effect="dark" v-else>禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                :render-header="renderHeader"
              >
                <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
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-card>
          <el-card header="重复停电风险告警次数设置">
            <el-table
              ref="table1"
              :data="tableData2"
              row-key="id"
              stripe
              height="300px"
            >
              <el-table-column prop="序号" label="序号" width="55">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="项目编码"
                prop="parCode"
                width="100"
                align="center"
                :show-overflow-tooltip="true"
              ></el-table-column>
              <el-table-column
                label="项目名称"
                prop="parName"
                width="180"
                align="center"
              ></el-table-column>
              <el-table-column
                label="项目对应值"
                prop="parValue"
                align="center"
              ></el-table-column>
              <el-table-column label="计量单位" prop="parUnit" align="center">
                <template slot-scope="scope">
                  <dict-tag
                    :options="dict.type.repeat_power_failure"
                    :value="scope.row.parUnit"
                  />
                </template>
              </el-table-column>
              <el-table-column label="状态" prop="status" align="center">
                <template slot-scope="scope">
                  <el-tag
                    type="success"
                    effect="dark"
                    v-if="scope.row.status == '1'"
                    >启用</el-tag
                  >
                  <el-tag type="info" effect="dark" v-else>禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                :render-header="renderHeader2"
              >
                <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
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </div>
        <div style="width: 50%; display: inline-block">
          <el-card header="无故障持续时间奖励设置">
            <el-table
              ref="table2"
              :data="tableData3"
              row-key="id"
              stripe
              height="300px"
            >
              <el-table-column prop="序号" label="序号" width="55">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="无故障持续时间"
                prop="parValue"
                align="center"
                :show-overflow-tooltip="true"
              ></el-table-column>
              <el-table-column label="计量单位" prop="parUnit" align="center">
                <template slot-scope="scope">
                  <dict-tag
                    :options="dict.type.pdm_maintenance_time"
                    :value="scope.row.parUnit"
                  />
                </template>
              </el-table-column>
              <el-table-column label="金额奖励" prop="parField" align="center">
                <template #default="scope">
                  {{ scope.row.parField }}/元
                </template>
              </el-table-column>
              <el-table-column label="状态" prop="status" align="center">
                <template slot-scope="scope">
                  <el-tag
                    type="success"
                    effect="dark"
                    v-if="scope.row.status == '1'"
                    >启用</el-tag
                  >
                  <el-tag type="info" effect="dark" v-else>禁用</el-tag>
                </template>
              </el-table-column>
              <!--el-table-column label="备注" prop="remark"> </el-table-column-->
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                :render-header="renderHeader3"
              >
                <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
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-card>
          <el-card header="扣分规则设置">
            <el-table
              ref="table3"
              :data="tableData4"
              row-key="id"
              stripe
              height="300px"
            >
              <el-table-column prop="序号" label="序号" width="55">
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="规则编号"
                prop="parCode"
              ></el-table-column>
              <el-table-column
                label="扣分项目"
                prop="parName"
              ></el-table-column>
              <el-table-column label="扣分" prop="parValue"></el-table-column>
              <el-table-column label="状态" prop="status">
                <template slot-scope="scope">
                  <el-tag
                    type="success"
                    effect="dark"
                    v-if="scope.row.status == '1'"
                    >启用</el-tag
                  >
                  <el-tag type="info" effect="dark" v-else>禁用</el-tag>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                :render-header="renderHeader4"
              >
                <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
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </div>
      </el-main>
    </el-container>
    <dic-dialog
      v-if="dialog.dic"
      ref="dicDialog"
      @success="handleDicSuccess"
      @closed="dialog.dic = false"
    ></dic-dialog>
    <fault-free-duration-dic-dialog
      v-if="dialog.info"
      ref="faultFreeDurationDicDialog"
      @success="handleDicSuccess"
      @closed="dialog.info = false"
    ></fault-free-duration-dic-dialog>
    <deduct-points-dialog
      v-if="dialog.info"
      ref="deductPointsDialog"
      @success="handleDicSuccess"
      @closed="dialog.deduct = false"
    ></deduct-points-dialog>
  </div>
</template>

<script>
import {
  listPar,
  getPar,
  delPar,
  listByParType,
} from "@/api/powerdistribution/par";
import dicDialog from "./dic";
import faultFreeDurationDicDialog from "./fault-free-duration-dic";
import deductPointsDialog from "./deduct-points";
export default {
  name: "Par",
  dicts: ["pdm_maintenance_time", "repeat_power_failure"],
  components: {
    dicDialog,
    faultFreeDurationDicDialog,
    deductPointsDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配网参数配置表格数据
      parList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parCode: null,
        parName: null,
        parValue: null,
        parUnit: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tableData1: [],
      tableData2: [],
      tableData3: [],
      tableData4: [],
      dialog: {
        dic: false,
        info: false,
        deduct: false,
      },
    };
  },
  created() {
    this.getParList(0);
    this.getParList(1);
    this.getParList(2);
    this.getParList(3);
  },
  methods: {
    /** 查询配网参数配置列表 */
    async getParList(parType) {
      this.queryParams.parType = parType;
      listByParType(this.queryParams).then((response) => {
        if (parType == 0) this.tableData1 = response.data;
        if (parType == 1) this.tableData2 = response.data;
        if (parType == 2) this.tableData3 = response.data;
        if (parType == 3) this.tableData4 = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parCode: null,
        parName: null,
        parValue: null,
        parUnit: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: 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;
    },
    /** 新增按钮操作 */
    handleAdd(parType) {
      this.dialog.dic = true;
      this.$nextTick(() => {
        this.form = {
          id: null,
          parCode: null,
          parName: null,
          parValue: null,
          parUnit: null,
          parType: parType,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
        };
        this.$refs.dicDialog.open().setData(this.form);
      });
    },
    /** 新增按钮操作 */
    handleAddDeductPoints() {
      this.dialog.info = true;
      this.$nextTick(() => {
        this.form = {
          id: null,
          parCode: null,
          parName: null,
          parValue: null,
          parUnit: null,
          parType: 3,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
        };
        this.$refs.deductPointsDialog.open().setData(this.form);
      });
    },
    /** 新增按钮操作 */
    handleAddFaultFreeDuration() {
      this.dialog.info = true;
      this.$nextTick(() => {
        this.form = {
          id: null,
          parCode: null,
          parName: "无故障持续时间",
          parValue: null,
          parUnit: null,
          parType: 2,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
        };
        this.$refs.faultFreeDurationDicDialog.open().setData(this.form);
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPar(id).then((response) => {
        if (row.parType == 2) {
          this.dialog.info = true;
          this.$nextTick(() => {
            this.$refs.faultFreeDurationDicDialog
              .open("edit")
              .setData(response.data);
          });
        } else if (row.parType == 3) {
          this.dialog.deduct = true;
          this.$nextTick(() => {
            this.$refs.deductPointsDialog.open("edit").setData(response.data);
          });
        } else {
          this.dialog.dic = true;
          this.$nextTick(() => {
            this.$refs.dicDialog.open("edit").setData(response.data);
          });
        }
      });
    },
    /** 修改按钮操作 */
    async handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids;
      getPar(id).then((response) => {
        this.info = true;
        this.$nextTick(() => {
          this.$refs.faultFreeDurationDicDialog
            .open("edit")
            .setData(response.data);
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePar(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPar(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(
          "是否确认删除" + row.parName + '编号为"' + row.parCode + '"的数据项？'
        )
        .then(function () {
          return delPar(ids);
        })
        .then(() => {
          this.getParList(row.parType);
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "powerdistribution/par/export",
        {
          ...this.queryParams,
        },
        `par_${new Date().getTime()}.xlsx`
      );
    },
    renderHeader() {
      return (
        <div>
          <span>操作</span>
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="table-icon"
            size="mini"
            onClick={() => this.handleAdd(0)}
          ></el-button>
        </div>
      );
    },
    renderHeader2() {
      return (
        <div>
          <span>操作</span>
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="table-icon"
            size="mini"
            onClick={() => this.handleAdd(1)}
          ></el-button>
        </div>
      );
    },
    renderHeader3() {
      return (
        <div>
          <span>操作</span>
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="table-icon"
            size="mini"
            onClick={() => this.handleAddFaultFreeDuration()}
          ></el-button>
        </div>
      );
    },
    renderHeader4() {
      return (
        <div>
          <span>操作</span>
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="table-icon"
            size="mini"
            onClick={() => this.handleAddDeductPoints()}
          ></el-button>
        </div>
      );
    },
    //本地更新数据
    async handleDicSuccess(data, mode) {
      this.getParList(data.parType);
    },
  },
};
</script>
