<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="其他项目名称" prop="xmName">
        <el-input v-model="queryParams.xmName" placeholder="请输入其他项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="上报人" prop="reporter">
        <el-select v-model="queryParams.reporter" filterable clearable @change="handleQuery">
          <el-option v-for="item in $store.state.user.userList" :key="item.userId" :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上报日期" prop="reportTime">
        <el-date-picker clearable v-model="queryParams.reportTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择上报日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</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:settle: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:settle: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:settle: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:settle:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settleList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="其他项目名称" align="center" prop="xmName" />
      <el-table-column label="上报人" align="center" prop="reporter">
        <template slot-scope="scope">
          {{ getUserName(scope.row.reporter) }}
        </template>
      </el-table-column>
      <el-table-column label="上报日期" align="center" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结算总额" align="center" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.amount ? scope.row.amount : 0}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="工作量上报说明" align="center" prop="workloadReport" /> -->
      <el-table-column label="结算说明" align="center" prop="settleComment" />
      <el-table-column label="总经理审批日期" align="center" prop="gmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmTime, '{y}-{m}-{d}') }}</span>
        </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-view" @click="handleView(scope.row)"
            v-hasPermi="['oa:settle:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:settle:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:settle: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="65%" append-to-body>
      <settle-other :taskForm="taskForm" :taskName="''" :flowDisabled="false" :disabled="true"></settle-other>
    </el-dialog>
  </div>
</template>

<script>
import { listSettle, getSettle, delSettle, addSettle, updateSettle } from "@/api/oa/settle/settle";
import { listProject } from '@/api/oa/project/project';
import settleOther from '../../flowable/form/settleOther.vue';
import { mapGetters } from 'vuex'

export default {
  components: { settleOther },
  name: "Settle",
  computed: {
    ...mapGetters(['roles', 'deptId']),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc结算审批表格数据
      settleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: '0',
        xmName: null,
        workloadReport: null,
        reporter: null,
        reportTime: null,
        settleComment: null,
        zhUserId: null,
        zhTime: null,
        zhComment: null,
        jsUserId: null,
        jsTime: null,
        jsComment: null,
        xmUserId: null,
        xmTime: null,
        xmComment: null,
        deptUserId: null,
        deptTime: null,
        deptComment: null,
        jyUserId: null,
        jyTime: null,
        jyComment: null,
        managerUserId: null,
        managerTime: null,
        managerComment: null,
        gmUserId: null,
        gmTime: null,
        gmComment: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectList: [],
      taskForm: {
        formId: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc结算审批列表 */
    getList() {
      this.loading = true;
      if (this.roles.some(element => element === "dept")) {
          this.queryParams.reportDept = this.deptId;        
      }
      if (this.roles.some(e => e === 'leader')) {
        this.queryParams.reportDept = null;
      }
      listSettle(this.queryParams).then(response => {
        this.settleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      listProject({
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }).then(res => {
        this.projectList = res.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        settleId: null,
        projectId: '0',
        xmName: null,
        workloadReport: null,
        reporter: null,
        reportTime: null,
        settleComment: null,
        zhUserId: null,
        zhTime: null,
        zhComment: null,
        jsUserId: null,
        jsTime: null,
        jsComment: null,
        xmUserId: null,
        xmTime: null,
        xmComment: null,
        deptUserId: null,
        deptTime: null,
        deptComment: null,
        jyUserId: null,
        jyTime: null,
        jyComment: null,
        managerUserId: null,
        managerTime: null,
        managerComment: null,
        gmUserId: null,
        gmTime: null,
        gmComment: 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.settleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc结算审批";
    },
    // 查看详情
    handleView(row) {
      this.open = true;
      this.taskForm.formId = row.settleId;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const settleId = row.settleId || this.ids
      getSettle(settleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc结算审批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.settleId != null) {
            updateSettle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSettle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const settleIds = row.settleId || this.ids;
      this.$modal.confirm('是否确认删除cmc结算审批编号为"' + settleIds + '"的数据项？').then(function () {
        return delSettle(settleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/settle/export', {
        ...this.queryParams
      }, `settle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
