<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent v-show="showSearch">
      <el-form-item label="结算批次" prop="batchName">
        <el-input v-model="queryParams.batchName" placeholder="请输入结算批次" @keyup.enter.native="handleQuery" clearable />
      </el-form-item>
      <el-form-item label="关联项目" prop="projectId">
        <projectSelect v-model="queryParams.projectId"></projectSelect>
      </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>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" icon="el-icon-download" @click="op('download')">下载模板</el-button>
      <el-button type="primary" icon="el-icon-document-add" @click="op('add')">创建结算单</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="response.rows" border height="calc(100vh - 270px)">
      <el-table-column type="index" label="序号" align="center" width="80" />
      <el-table-column label="上传文档名称" align="center" prop="fileName" />
      <el-table-column label="结算批次" align="center" prop="batchName" width="180" />
      <el-table-column label="关联项目" align="center" prop="projectName" width="180" />
      <el-table-column label="发放金额(元)" align="center" prop="money" width="110" />
      <el-table-column label="发放人数" align="center" prop="peoples" width="110" />
      <el-table-column label="生成状态" align="center" width="110">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.state == 0">待生成</el-tag>
          <el-tag type="success" v-if="scope.row.state == 1">已生成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" width="170" />
      <el-table-column label="操作" align="center" width="270">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="op('edit', scope.row)" v-if="scope.row.state === 0">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="op('detail', scope.row)" v-else>详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="op('generate', scope.row)" v-if="scope.row.state === 0">生成结算单</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="del(scope.row)" v-if="scope.row.state === 0">删除</el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty></el-empty>
      </template>
    </el-table>
    <pagination v-if="response.total > 0" :total="response.total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
  import edit from './edit';
  import record from './record';
  export default {
    data() {
      return {
        showSearch: true,
        response: {},
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          batchName: null,
          projectId: null,
          orderByColumn: 's.id',
          isAsc: 'desc'
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.ajax({ url: '/work/statement/make', data: this.queryParams }).then((response) => {
          this.response = response;
        });
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm('queryForm');
        this.handleQuery();
      },
      op(tag, row) {
        if (tag == 'add') {
          this.iframe({ obj: edit, param: {}, title: '手动创建结算单', width: '40%', height: '63%' });
          return;
        }
        if (tag == 'download') {
          this.download('work/statement/make/download', {}, '结算单模版.xlsx');
          return;
        }
        const id = row.id;
        if (tag == 'detail') {
          this.iframe({ obj: record, param: { form: row, detail: true }, title: '查看结算单明细', width: '75%', height: '70%' });
        }
        if (tag == 'edit') {
          this.iframe({ obj: record, param: { form: row }, title: '编辑结算单', width: '75%', height: '70%' });
        }
        if (tag == 'generate') {
          this.$confirm('是否确认生成结算单?', '警告', { type: 'warning' }).then(() => {
            this.ajax({ url: '/work/statement/make/generate/' + id }).then((response) => {
              this.$modal.msgSuccess('生成成功');
              this.getList();
            });
          });
        }
      },
      del(row) {
        this.$confirm('是否确认删除选中数据?', '警告', { type: 'warning' }).then(() => {
          this.get({ url: '/work/statement/make/remove/' + row.id }).then((response) => {
            this.$modal.msgSuccess('删除成功');
            this.getList();
          });
        });
      }
    }
  };
</script>
