<template>
  <!-- 奖惩规则配置 -->
  <div class="app-container">
    <!-- <el-form
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="编码名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
        ></el-input>
      </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" v-if="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="dataList.length == 0"
          @click="handleAddDetail"
          >新增明细</el-button
        >
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="!selectedRows.length"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="12">
        <el-card>
          <div slot="header" class="clearfix">
            <span>计划时户数</span>
            <el-button
              style="float: right; padding: 3px 0"
              type="text"
              icon="el-icon-plus"
              @click="handleAdd"
              >新增
            </el-button>
          </div>
          <div class="table">
            <el-table
              ref="renewalTable"
              v-loading="loading"
              :data="dataList"
              row-key="id"
              @current-change="handleCurrentChange"
              highlight-current-row
              height="640px"
            >
              <el-table-column width="25">
                <template slot-scope="scope">
                  <el-radio
                    class="radio"
                    :label="scope.row"
                    v-model="currentRow"
                    >{{ "" }}</el-radio
                  >
                </template>
              </el-table-column>
              <!-- <el-table-column type="selection" width="55" align="center" /> -->
              <el-table-column
                prop="date"
                label="序号"
                width="70"
                align="center"
              >
                <template slot-scope="scope">{{ scope.$index + 1 }}</template>
              </el-table-column>
              <el-table-column
                label="目标时户数"
                align="center"
                prop="targetName"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="最小时户数"
                align="center"
                prop="minNum"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="最大时户数"
                prop="maxNum"
                align="center"
                :show-overflow-tooltip="true"
              >
              </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.stop="handleUpdate(scope.row)"
                    >修改</el-button
                  >
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click.stop="handleDelete(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card header="完成时户数">
          <div slot="header" class="clearfix">
            <span>完成时户数</span>
            <el-button
              style="float: right; padding: 3px 0"
              type="text"
              icon="el-icon-plus"
              :disabled="dataList.length == 0"
              @click="handleAddDetail"
              >新增
            </el-button>
          </div>
          <div class="table">
            <el-table
              :data="childrenList"
              v-loading="childrenLoading"
              height="640px"
            >
              <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                  {{ scope.$index + 1 }}
                </template>
              </el-table-column>
              <el-table-column
                label="完成时户数"
                align="center"
                prop="detailName"
                width="130"
                :show-overflow-tooltip="true"
              />
              <el-table-column label="考核金额" align="center">
                <el-table-column
                  label="党政一把手"
                  prop="manageAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                >
                </el-table-column>
                <el-table-column
                  label="分管副经理"
                  prop="manageAssistantAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                >
                </el-table-column>
                <el-table-column
                  label="部室主任"
                  prop="deptDirectorAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                />
                <el-table-column
                  label="部室分管主任"
                  prop="deptDirectorAssistantAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                >
                </el-table-column>
                <el-table-column
                  label="部室责任专责"
                  prop="deptManageAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                />
                <el-table-column
                  label="总负责人"
                  prop="principAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                />
                <el-table-column
                  label="工作负责人"
                  prop="workPrincipAmt"
                  align="center"
                />
                <el-table-column
                  label="设备主人"
                  prop="ownerAmt"
                  align="center"
                  :show-overflow-tooltip="true"
                >
                </el-table-column>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                fixed="right"
                width="130"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdateDetail(scope.row)"
                    >修改</el-button
                  >
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDeleteDetail(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <UpdateModel ref="addModelRef" @refresh="getList()"></UpdateModel>
    <UpdateModelDetail
      ref="addModelDetailRef"
      @refresh="handleCurrentChange(currentRow)"
    ></UpdateModelDetail>
  </div>
</template>

<script>
import UpdateModel from "./components/UpdateModel";
import UpdateModelDetail from "./components/UpdateModelDetail";
import {
  examineRoleTargetList,
  dleExamineRoleTarget,
  examineRoleDetailList,
  dleExamineRoleDetail,
} from "@/api/powerdistribution/maintenance";
export default {
  components: { UpdateModel, UpdateModelDetail },
  data() {
    return {
      dataList: [],
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      total: 0,
      // 查询参数
      queryParams: {
        roleName: "",
        pageNum: 1,
        pageSize: 10,
      },
      // 选中数组
      selectedRows: [],
      childrenList: [],
      childrenLoading: true,
      currentRow: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async handleCurrentChange(row) {
      this.currentRow = row;
      this.childrenLoading = true;
      const data = await examineRoleDetailList({
        targetId: row.id,
      });
      this.childrenList = data.rows;
      this.childrenLoading = false;
    },
    // 多选框选中数据
    handleSelectionChange(selectedRows) {
      // 记录选择的行
      this.selectedRows = selectedRows;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.addModelRef.openModel({ ...row });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addModelRef.openModel({});
    },
    handleAddDetail() {
      this.$refs.addModelDetailRef.openModel({ targetId: this.currentRow.id });
    },
    handleUpdateDetail(row) {
      this.$refs.addModelDetailRef.openModel({ ...row });
    },
    handleDeleteDetail(row) {
      this.$modal
        .confirm("是否确认删除该数据项？")
        .then(function () {
          return dleExamineRoleDetail(row.id);
        })
        .then(() => {
          this.handleCurrentChange(this.currentRow);
          this.$modal.msgSuccess("删除成功");
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id || this.selectedRows.map((ite) => ite.id);
      this.$modal
        .confirm('是否确认删除编号为"' + roleIds + '"的数据项？')
        .then(function () {
          return dleExamineRoleTarget(roleIds);
        })
        .then(() => {
          this.getList();
          this.selectedRows = [];
          this.$refs.renewalTable.clearSelection();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      examineRoleTargetList({ ...this.queryParams }).then((response) => {
        this.dataList = response.rows;
        if (this.dataList.length) {
          this.handleCurrentChange(
            this.dataList.length ? this.dataList[0] : null
          );
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
.table {
  height: calc(100% - 150px);
}
::v-deep {
  .el-table {
    height: 100%;
    width: 100%;
    .el-table__body-wrapper {
      width: 100% !important;
      height: calc(100% - 50px) !important; // 表格高度减去表头的高度
      overflow-y: scroll;
    }
  }
}
</style>
