<template>
  <div class="app-container">
    <!-- 面包屑 -->
    <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"
    >
      <el-form-item label="水库名称" prop="resName">
        <el-input
          v-model="queryParams.data.resName"
          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="unit">
        <el-input
          v-model="queryParams.data.unit"
          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="unitName">
        <el-input
          v-model="queryParams.data.unitName"
          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>
        <!-- <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"
        >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <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:jfgl: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:jfgl: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:jfgl:remove']"
          >删除</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jfglList">
      <!-- <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="resCode"
        min-width="120"
      />
      <el-table-column
        label="水库名称"
        align="center"
        prop="resName"
        min-width="120"
      />
      <el-table-column
        label="水库类型"
        align="center"
        prop="resType"
        :formatter="resTypeFormat"
        min-width="120"
      />
      <!-- <el-table-column
        label="工程规模"
        align="center"
        prop="engScal"
        :formatter="projectScaleFormat"
        min-width="120"
      /> -->
<!--      <el-table-column
        label="水库主管部门"
        align="center"
        prop="unit"
        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="['df:jfgl:edit']"
            >经费管理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['df:jfgl: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="1200px"
      append-to-body
      :close-on-click-modal="false"
      v-if="open"
    >
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="8">
            <el-form-item label="水库名称" prop="resName">
              <link-res :form="form"/>

            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="水库编码" prop="resCode">
              <el-input
                v-model="form.resCode"
                placeholder="请输入水库编码"
                disabled
              />
            </el-form-item>
          </el-col>

          <!-- <el-form-item label="人员数量" prop="peopleNum">
          <el-input v-model="form.peopleNum" placeholder="请输入人员数量" />
        </el-form-item>
        <el-form-item label="经费" prop="funds">
          <el-input v-model="form.funds" placeholder="请输入经费" />
        </el-form-item>
        <el-form-item label="经费用途" prop="fundsPurpose">
          <el-input v-model="form.fundsPurpose" placeholder="请输入经费用途" />
        </el-form-item> -->
        </el-form>
      </el-row>
      <el-row>
        <div class="listTitle">
          <span>人员经费</span>
          <span class="addBtn" @click="handleAddList('0')">
            <i class="el-icon-plus"></i>
            添加
          </span>
        </div>
        <personnelExpenses
          ref="personnelExpenses"
          :resCode="this.form.resCode"
        />
      </el-row>
      <el-row>
        <div class="listTitle">
          <span>维修养护经费</span>
          <span class="addBtn" @click="handleAddList('1')">
            <i class="el-icon-plus"></i>
            添加
          </span>
        </div>
        <maintenanceFund ref="maintenanceFund" :resCode="this.form.resCode" />
      </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 {
  listJfgl,
  getJfgl,
  delJfgl,
  addJfgl,
  updateJfgl,
  exportJfgl,
} from "@/api/res/jfgl";
// import { listJbxx } from "@/api/yg/jbxx";
import personnelExpenses from "./components/personnelExpenses.vue";
import maintenanceFund from "./components/maintenanceFund.vue";
import linkDike from "@/components/linkNameSelect/linkDike.vue";
import LinkRes from "../../../../components/linkNameSelect/linkRes.vue";

export default {
  name: "Jfgl",
  components: {
    LinkRes,
    personnelExpenses,
    maintenanceFund,
    linkDike,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经费管理表格数据
      jfglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 水库类型字典
      resTypeOptions: [],
      // 工程规模字典
      projectScaleOptions: [],
      // 查询参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: null,
        data: {
          resCode: null,
          resName: null,
          unit: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sortBy: "desc",
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // resNameOptions: [],
      // 面包屑，路由信息
      routeList: [
        {
          path: "/sluice/managementGuarantee/fundGuarantee",
          routeName: "经费保障",
        },
      ],
    };
  },
  created() {
    this.getList();
    // listJbxx({ data: {} }).then((res) => {
    //   // console.log(111, res);
    //   this.resNameOptions = res.records;
    // });
    this.getDicts("engineering_scale").then((response) => {
      this.projectScaleOptions = response.data;
    });
    this.getDicts("embankment_type").then((response) => {
      this.resTypeOptions = response.data;
    });
  },
  methods: {
    handleAddList(key) {
      if (key == "0") this.$refs.personnelExpenses.handleAdd();
      if (key == "1") this.$refs.maintenanceFund.handleAdd();
    },
    /** 查询经费管理列表 */
    getList() {
      this.loading = true;
      listJfgl(this.queryParams).then((response) => {
        this.jfglList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 工程规模字典翻译
    projectScaleFormat(row, column) {
      return this.selectDictLabel(this.projectScaleOptions, row.engScal);
    },
    // 水库类型字典翻译
    resTypeFormat(row, column) {
      return this.selectDictLabel(this.resTypeOptions, row.resType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        resCode: null,
        resName: null,
        unitName: null,
        unit: null,
        peopleNum: null,
        funds: null,
        fundsPurpose: null,
        createUid: null,
        updateUid: null,
        createTime: null,
        updateTime: null,
        owerDept: null,
        remark: null,
        relation: null,
        type: null,
      };
      this.resetForm("form");
    },
    // 查询表单重置
    resetQueryForm() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        data: {
          id: null,
          resCode: null,
          resName: null,
          unitName: null,
          unit: null,
          peopleNum: null,
          funds: null,
          fundsPurpose: null,
          createUid: null,
          updateUid: null,
          createTime: null,
          updateTime: null,
          owerDept: null,
          remark: null,
          relation: null,
          type: 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;
      // getJfgl(id).then((response) => {
      this.form = row;
      this.open = true;
      this.title = "修改经费管理";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateJfgl(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addJfgl(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 delJfgl(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      } else {
        this.$message.warning("请选择要删除的数据！！");
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/dialog.scss";

.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;
    // color: #000;
  }
  .addBtn {
    color: #36b29e;
    cursor: pointer;
  }
  .addBtn:hover {
    color: #31a08e;
  }
}
.listTitle:nth-child(1) {
  margin-top: 0;
}
</style>
