<template>
  <div class="app-container new-box">
    <el-card>
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <div class="filter-box">
          <div class="filter-lf">
            <el-form-item label="报销类型" prop="billType">
              <el-select v-model="queryParams.billType" placeholder="请选择报销类型" clearable size="small">
                <el-option v-for="dict in applyTypeList" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <!-- 操作工具栏 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-dropdown trigger="click" v-hasPermi="['bs:expense-claim:create']" @command="handleCommand">
            <el-button type="primary" size="mini" style="margin-right:5px;">
              <i class="el-icon-plus"></i> 创建报销
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :command="beforeHandleCommand(1)">
                <el-button type="text">出差报销</el-button>
              </el-dropdown-item>
              <el-dropdown-item :command="beforeHandleCommand(3)">
                <el-button type="text">日常报销</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading" v-hasPermi="['bs:expense-claim:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <!-- 列表 -->
      <el-table stripe border v-loading="loading" :data="list">
        <el-table-column label="报销类型" align="center" prop="billType">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_BILL_TYPE" :value="scope.row.billType" />
          </template>
        </el-table-column>
        <el-table-column label="报销单号" align="center" prop="claimNo" />
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="报销人" align="center" prop="nickname" />
        <el-table-column label="报销金额" align="center" prop="amount" />
        <el-table-column label="费用归属部门" align="center" prop="costDeptId">
          <template v-slot="scope">
            <span>{{ handleFindDept(scope.row.costDeptId) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="报销事由" align="center" prop="reason" />
        <el-table-column label="报销状态" align="center" prop="status">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_EXPENSE_APPLY_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="结算状态" align="center" prop="billType">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.BS_SETTLE_STATUS" :value="scope.row.settleStatus" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" :disabled="scope.row.status!=0" @click="handleUpdate(scope.row)" v-hasPermi="['bs:expense-claim:update']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)" v-hasPermi="['bs:expense-claim:show']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" :disabled="scope.row.status!=0" @click="handleDelete(scope.row)" v-hasPermi="['bs:expense-claim:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script>
import { deleteExpenseClaim, getExpenseClaimPage, exportExpenseClaimExcel } from "@/api/bs/expenseClaim";
import { findAreaIdName } from '@/utils'
export default {
  name: "ExpenseClaim",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报销单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        billType: null,
        claimNo: null,
        userId: null,
        deptId: null,
        projectName: null,
        reason: null,
        amount: null,
        status: null,
        remark: null,
        files: null,
        createTime: [],
        processInstanceId: null,
        costDeptId: null,
        invoiceIds: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billType: [{ required: true, message: "报销类型不能为空", trigger: "change" }],
        claimNo: [{ required: true, message: "报销单号不能为空", trigger: "blur" }],
        userId: [{ required: true, message: "申请者ID不能为空", trigger: "blur" }],
        deptId: [{ required: true, message: "部门ID不能为空", trigger: "blur" }],
        reason: [{ required: true, message: "报销事由不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        files: [{ required: true, message: "附件不能为空", trigger: "blur" }],
        costDeptId: [{ required: true, message: "费用归属部门不能为空", trigger: "blur" }],
        invoiceIds: [{ required: true, message: "关联发票ID不能为空", trigger: "blur" }],
      },
      applyTypeList: []
    };
  },
  activated() {
    this.getList();
  },
  computed: {
    userDeptTree() {
      return this.$store.getters.userDeptTree
    }
  },
  created() {
    this.getList();
    this.handleInitList()
  },
  methods: {
    handleFindDept(val) {
      return (findAreaIdName(this.userDeptTree, val) || {}).name || '-'
    },
    handleInitList() {
      this.applyTypeList = this.getDictDatas(this.DICT_TYPE.BS_BILL_TYPE).filter(item => ["CLBX", "RCBX"].includes(item.value))
      console.log('userDeptTree', findAreaIdName(this.userDeptTree, 100));
    },
    handleUpdate(row) {
      const { billType } = row
      if (billType === 'CLBX') {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: row.id, flag: 'edit', title: '出差报销单修改', typeStr: 'business' } });
      } else {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: row.id, flag: 'edit', title: '日常报销单修改', typeStr: 'daily' } });
      }
    },
    beforeHandleCommand(index, data) {
      return {
        'index': index,
        'row': data
      }
    },
    handleCommand(val) {
      const { index } = val
      //  1 出差 2 报销 3 日常
      if (index === 1) {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: null, flag: 'add', title: '出差报销添加', typeStr: 'business' } });
      } else {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: null, flag: 'add', title: '日常报销添加', typeStr: 'daily' } });
      }
    },
    // 查看按钮
    handleShow(row) {
      const { billType } = row
      if (billType === 'RCBX') {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: row.id, flag: 'show', typeStr: 'daily', title: '日常报销单查看' } });
      } else {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: row.id, flag: 'show', typeStr: 'business', title: '出差报销查看' } });
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getExpenseClaimPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除报销单编号为"' + row.claimNo + '"的数据项?').then(function () {
        return deleteExpenseClaim(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有报销单数据项?').then(() => {
        this.exportLoading = true;
        return exportExpenseClaimExcel(params);
      }).then(response => {
        this.$download.excel(response, '报销单.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
<style lang='scss' scoped>
@import '@/assets/styles/header/index.scss';
@import '@/assets/styles/common/index.scss';
</style>
