<!--
 * @Author: ysh
 * @Date: 2024-03-21 17:49:15
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-07-16 16:09:58
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目编号" prop="projectId">
        <el-select
          v-model="queryParams.projectId"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in projectList"
            :key="item.projectId"
            :label="item.projectNumber"
            :value="item.projectId"
          >
          </el-option>
        </el-select>
      </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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oa:budget:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oa:budget:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oa:budget:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oa:budget:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="budgetList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="预算id" align="center" prop="budgetId" /> -->
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" />
      <el-table-column
        label="项目名称"
        align="center"
        prop="project.projectName"
        width="200px"
      />
      <el-table-column label="人员成本" align="center" prop="staffCost" />
      <el-table-column label="车辆成本" align="center" prop="carCost" />
      <el-table-column label="设备成本" align="center" prop="deviceCost" />
      <el-table-column label="间接成本" align="center" prop="fixCost" />
      <el-table-column label="预结算额" align="center" prop="settleExpense" />
      <el-table-column label="外协费用" align="center" prop="outExpense" />
      <el-table-column label="商务费用" align="center" prop="businessExpense" />
      <el-table-column label="车船租赁" align="center" prop="rentExpense" />
      <el-table-column label="其他费用" align="center" prop="otherExpense" />
      <el-table-column label="直接成本" align="center" prop="directExpense" />
      <el-table-column label="预算总额" align="center" prop="totalBudget" />
      <el-table-column label="编制人" align="center" prop="compilerUser.nickName" />
      <el-table-column label="审核人" align="center" prop="auditorUser.nickName" />
      <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="handleUpdate(scope.row)"
            v-hasPermi="['oa:budget:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetails(scope.row)"
            >详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oa:budget: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"
    />

    <!-- 添加或修改cmc预算管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="人员成本" prop="staffCost">
          <el-input v-model="form.staffCost" placeholder="请输入人员成本" />
        </el-form-item>
        <el-form-item label="车辆成本" prop="carCost">
          <el-input v-model="form.carCost" placeholder="请输入车辆成本" />
        </el-form-item>
        <el-form-item label="设备成本" prop="deviceCost">
          <el-input v-model="form.deviceCost" placeholder="请输入设备成本" />
        </el-form-item>
        <el-form-item label="间接成本" prop="fixCost">
          <el-input v-model="form.fixCost" placeholder="请输入间接成本" />
        </el-form-item>
        <el-form-item label="预结算额" prop="settleExpense">
          <el-input v-model="form.settleExpense" placeholder="请输入预结算额" />
        </el-form-item>
        <el-form-item label="外协费用" prop="outExpense">
          <el-input v-model="form.outExpense" placeholder="请输入外协费用" />
        </el-form-item>
        <el-form-item label="商务费用" prop="businessExpense">
          <el-input v-model="form.businessExpense" placeholder="请输入商务费用" />
        </el-form-item>
        <el-form-item label="车船租赁" prop="rentExpense">
          <el-input v-model="form.rentExpense" placeholder="请输入车船租赁" />
        </el-form-item>
        <el-form-item label="其他费用" prop="otherExpense">
          <el-input v-model="form.otherExpense" placeholder="请输入其他费用" />
        </el-form-item>
        <el-form-item label="直接成本" prop="directExpense">
          <el-input v-model="form.directExpense" placeholder="请输入直接成本" />
        </el-form-item>
        <el-form-item label="预算总额" prop="totalBudget">
          <el-input v-model="form.totalBudget" placeholder="请输入预算总额" />
        </el-form-item>
        <el-form-item label="编制人" prop="compiler">
          <el-input v-model="form.compiler" placeholder="请输入编制人" />
        </el-form-item>
        <el-form-item label="审核人" prop="auditor">
          <el-input v-model="form.auditor" placeholder="请输入审核人" />
        </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="title" :visible.sync="detailsOpen" width="1000px" append-to-body>
      
    </el-dialog>
  </div>
</template>

<script>
import {
  listBudget,
  getBudget,
  delBudget,
  addBudget,
  submitBudget,
  updateBudget,
} from "@/api/oa/budget/budget";
import { listProject } from "@/api/oa/project/project";
import budgetTable from './components/budgetTable.vue';
export default {
  name: "Budget",
  components:{ budgetTable }, 
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc预算管理表格数据
      budgetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        staffCost: null,
        carCost: null,
        deviceCost: null,
        fixCost: null,
        settleExpense: null,
        outExpense: null,
        businessExpense: null,
        rentExpense: null,
        otherExpense: null,
        directExpense: null,
        totalBudget: null,
        compiler: null,
        auditor: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      projectList: [],
      detailsOpen: false,
    };
  },
  created() {
    this.getList();
    this.getProjectList();
  },
  methods: {
    /** 查询cmc预算管理列表 */
    getList() {
      this.loading = true;
      listBudget(this.queryParams).then(response => {
        this.budgetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getProjectList() {
      listProject({
        pageNum: 1,
        pageSize: 99999999,
      }).then(response => {
        this.projectList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        budgetId: null,
        projectId: null,
        staffCost: null,
        carCost: null,
        deviceCost: null,
        fixCost: null,
        settleExpense: null,
        outExpense: null,
        businessExpense: null,
        rentExpense: null,
        otherExpense: null,
        directExpense: null,
        totalBudget: null,
        compiler: null,
        auditor: null,
        createTime: 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.budgetId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加cmc预算管理";
      this.$router.push({ path: "/budget/add" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const budgetId = row.budgetId || this.ids;
      getBudget(budgetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc预算管理";
      });
    },
    // 详情按钮
    handleDetails(row) {
      this.detailsOpen = true;
      this.title = row.project.projectNumber + "预算详情";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.budgetId != null) {
            updateBudget(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBudget(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const budgetIds = row.budgetId || this.ids;
      this.$modal
        .confirm('是否确认删除cmc预算管理编号为"' + budgetIds + '"的数据项？')
        .then(function () {
          return delBudget(budgetIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "oa/budget/export",
        {
          ...this.queryParams,
        },
        `budget_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
