<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="projectId" label="是否勘察项目" label-width="100px">
        <el-radio-group v-model="isSurvey" @change="handleIsSurveyChange">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="projectId">
        <el-select v-model="queryType" style="width: 100px;">
          <el-option label="项目编号" value="1"></el-option>
          <el-option label="项目名称" value="2"></el-option>
        </el-select>
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入关键字"
          :remote-method="remoteMethod" :loading="loading" style="width: 300px;">
          <el-option v-for="project in projectList" :key="project.projectId"
            :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="编制人" prop="compiler">
        <el-select v-model="queryParams.compiler" filterable clearable>
          <el-option :key="item.userId" v-for="item of $store.state.user.userList" :label="item.nickName"
            :value="item.userId"></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="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:budget:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="budgetList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" />
      <el-table-column label="项目名称" align="center" prop="project.projectName" />
      <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="siteCost" />
      <el-table-column label="经营相关" align="center" prop="businessCost">
        <template slot-scope="scope">
          {{ scope.row.outExpense + scope.row.letterExpense + scope.row.winExpense + scope.row.travelExpense + scope.row.taxExpense }}
        </template>
      </el-table-column>
      <el-table-column label="预算总额" align="center" prop="totalBudget" />
      <el-table-column label="编制人" align="center" prop="compiler">
        <template slot-scope="scope">
          {{ getUserName(scope.row.compiler) }}
        </template>
      </el-table-column>
      <el-table-column label="编制时间" align="center" prop="createTime" />
      <el-table-column label="审核人" align="center" prop="auditor">
        <template slot-scope="scope">
          {{ getUserName(scope.row.auditor) }}
        </template>
      </el-table-column>
      <el-table-column label="是否核算" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isAdjust" type="success">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </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-tickets" @click="handleNewView(scope.row)"
            v-hasPermi="['oa:budget:query']">新版预算</el-button>
          <el-button size="mini" type="text" icon="el-icon-document-remove" @click="handleView(scope.row)"
            v-hasPermi="['oa:budget:query']">旧版预算</el-button>
          <el-button size="mini" type="text" icon="el-icon-document-checked" @click="handleAdjust(scope.row)"
            v-hasRole="['leader']" v-if="scope.row.isAdjust">查看核算</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="70%" append-to-body>
      <form-data :form="form" @close="open = false"></form-data>
    </el-dialog>

    <el-dialog title="新版预算" :visible.sync="newInfoOpen" width="70%" append-to-body>
      <new-budget-info :taskForm="taskForm" :taskName="''"></new-budget-info>
    </el-dialog>
    <el-dialog title="旧版预算" :visible.sync="infoOpen" width="70%" append-to-body>
      <budget-info :taskForm="taskForm" :taskName="''"></budget-info>
    </el-dialog>
    <el-dialog title="项目核算" :visible.sync="adjustOpen" width="70%" append-to-body>
      <budget-adjust :taskForm="taskForm" :taskName="''" :row="clickRow"></budget-adjust>
    </el-dialog>
  </div>
</template>

<script>
import { listBudget, getBudget, delBudget, addBudget, updateBudget } from "@/api/oa/budget/budget";
import { listCheck, getCheck, delCheck, addCheck, updateCheck } from "@/api/oa/budget/check";
import { listProject } from '@/api/oa/project/project';
import budgetInfo from '@/views/flowable/form/budget/budgetInfo.vue';
import newBudgetInfo from '@/views/flowable/form/budget/adjust/newBudgetInfo.vue';
import budgetAdjust from '@/views/flowable/form/budget/adjust/budgetAdjust.vue';
import FormData from './formData.vue';
export default {
  name: "Budget",
  components: { budgetInfo, FormData, newBudgetInfo, budgetAdjust },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc预算管理表格数据
      budgetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        staffCost: null,
        carCost: null,
        deviceCost: null,
        fixCost: null,
        settleExpense: null,
        outExpense: null,
        businessExpense: null,
        taxExpense: null,
        rentExpense: null,
        otherExpense: null,
        outRemark: null,
        businessRemark: null,
        taxRemark: null,
        rentRemark: null,
        otherRemark: null,
        directExpense: null,
        totalBudget: null,
        compiler: null,
        auditor: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: '',
        procInsId: '',
        budgetId: '',
      },
      projectList: [],
      queryType: '1',
      isOld: false,
      clickRow: {},
      isAdjust: false,
      adjustOpen: false,
      newInfoOpen: false,
      isSurvey: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc预算管理列表 */
    getList() {
      this.loading = true;
      listBudget(this.queryParams).then(async response => {
        this.budgetList = response.rows;
        for (let i of this.budgetList) {
          let res = await listCheck({ budgetId: i.budgetId });
          if (res.rows.length > 0) {
            this.$set(i, 'isAdjust', true);
          } else {
            this.$set(i, 'isAdjust', false);
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      let params1 = {
        pageNum: 1,
        pageSize: 20,
        projectNumber: val,
        undertakingDept: ''
      }
      let params2 = {
        pageNum: 1,
        pageSize: 20,
        projectName: val,
        undertakingDept: ''
      }
      if (this.isSurvey == 1) {
        params1.undertakingDept = '113'
        params2.undertakingDept = '113'
      } else {
        params1.undertakingDept = ''
        params2.undertakingDept = ''
      }
      let params = {};
      if (this.queryType == '1') {
        params = params1
      } else {
        params = params2
      }
      listProject(params).then(res => {
        this.projectList = res.rows;
      })
    },
    handleIsSurveyChange(val) {
      this.isSurvey = val;
    },
    // 取消按钮
    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,
        taxExpense: null,
        rentExpense: null,
        otherExpense: null,
        outRemark: null,
        businessRemark: null,
        taxRemark: null,
        rentRemark: null,
        otherRemark: 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 = "添加预算";
    },
    async handleView(row) {
      this.clickRow = row;
      this.taskForm.formId = row.projectId;
      this.taskForm.procInsId = row.procInstId;
      this.infoOpen = true;
    },
    handleNewView(row) {
      this.taskForm.formId = row.projectId;
      this.taskForm.procInsId = row.procInstId;
      this.clickRow = row;
      this.newInfoOpen = true;
    },
    async handleAdjust(row) {
      let res = await listCheck({ budgetId: row.budgetId });
      if (res.rows.length > 0) {
        this.taskForm.formId = res.rows[0].checkId;
      }
      this.clickRow = row;
      this.taskForm.budgetId = row.budgetId;
      this.adjustOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const budgetId = row.budgetId || this.ids
      getBudget(budgetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预算";
      });
    },
    /** 提交按钮 */
    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>
