<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent v-show="showSearch" label-width="auto">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" @keyup.enter.native="handleQuery" clearable />
      </el-form-item>
      <el-form-item label="项目编号" prop="num">
        <el-input v-model="queryParams.num" placeholder="请输入项目编号" @keyup.enter.native="handleQuery" clearable />
      </el-form-item>
      <el-form-item label="项目状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="充值状态" clearable style="width: 117px">
          <el-option value="0" label="启用"></el-option>
          <el-option value="1" label="禁用"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发包日期">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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-plus" :disabled="ids.length > 0" @click="op('add')" v-hasPermi="['work:project:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit" :disabled="ids.length != 1" @click="op('edit', ids)" v-hasPermi="['work:project:edit']">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" :disabled="ids.length == 0" @click="del" v-hasPermi="['work:project:remove']">删除{{ ids.length > 0 ? '(' + ids.length + ')' : '' }}</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="response.rows" border @selection-change="selects" height="calc(100vh - 270px)">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="left" prop="projectName" width="230" />
      <el-table-column label="项目编号" align="center" prop="num" width="180" />
      <el-table-column label="项目周期" align="center" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.startDate }} 至 {{ scope.row.finishDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务公司" align="center" width="180">
        <template slot-scope="scope">
          <div class="omit" :title="scope.row.serviceCompany">{{ scope.row.serviceCompany }}</div>
        </template>
      </el-table-column>
      <el-table-column label="接包人数" align="center" prop="peoples" width="120">
        <template slot-scope="scope">
          <div class="omit">{{ scope.row.peoples || 0 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="发包日期" align="center" prop="createTime" width="160" />
      <el-table-column label="项目状态" align="center" prop="state" width="100" fixed="right">
        <template slot-scope="scope">
          <div class="switch">
            <el-switch v-model="scope.row.state" :active-value="0" :width="50" :inactive-value="1" @change="op('change', scope.row)"></el-switch>
            <span class="zc" v-if="scope.row.state == 0">发布</span>
            <span class="ty" v-else>停用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="370" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-goods" @click="op('packages', scope.row)" v-hasPermi="['work:project:list']">接包明细</el-button>
          <el-button size="mini" type="text" icon="el-icon-document-copy" @click="op('generate', scope.row)" v-hasPermi="['work:statement:make']" v-if="scope.row.peoples>0">生成结算单</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="op('detail', scope.row)" v-hasPermi="['work:project:list']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="op('edit', scope.row)" v-hasPermi="['work:project:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="del(scope.row)" v-hasPermi="['work:project:remove']">删除</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 packages from './packages/index.vue';
  export default {
    name: 'Project',
    data() {
      return {
        ids: [],
        showSearch: true,
        response: {},
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          projectName: null,
          num: null,
          state: null,
          orderByColumn: 'id',
          isAsc: 'desc'
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        if (this.dateRange) {
          this.queryParams.dateBegin = this.dateRange[0];
          this.queryParams.dateEnd = this.dateRange[1];
        }
        this.ajax({ url: '/work/project/list', data: this.queryParams }).then((response) => {
          this.response = response;
        });
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm('queryForm');
        this.dateRange = [];
        this.handleQuery();
      },
      selects(rows) {
        this.ids = rows.map((item) => item.id);
      },
      op(tag, row) {
        if (tag == 'add') {
          this.iframe({ obj: edit, param: {}, title: '新增项目', width: '50%', height: '50%' });
          return;
        }
        const id = row.id || this.ids[0];
        if (tag == 'packages') {
          this.iframe({ obj: packages, param: { projectId: row.id }, title: '接包明细', width: '75%', height: '75%' });
        }
        if (tag == 'detail') {
          this.iframe({ obj: edit, param: { id: id, detail: true }, title: '项目详情', width: '55%', height: '55%' });
        }
        if (tag == 'edit') {
          this.iframe({ obj: edit, param: { id: id }, title: '编辑项目', width: '55%', height: '55%' });
        }
        //账号状态
        if (tag == 'change') {
          let text = row.state === 0 ? '发布' : '停用';
          this.$confirm('确认要' + text + '该项目吗?,停用后无法再接包', '警告', { type: 'warning' })
            .then(() => {
              this.post({ url: '/work/project/change', data: { id: row.id, state: row.state } })
                .then((response) => {
                  this.$modal.msgSuccess(text + '成功');
                })
                .catch(() => {
                  row.state = row.state === 0 ? 1 : 0;
                });
            })
            .catch(() => {
              row.state = row.state === 0 ? 1 : 0;
            });
        }
        if (tag == 'generate') {
          this.$prompt('是否确认生成结算单?', {
            type: 'warning',
            showInput: true,
            inputPlaceholder: '结算批次',
            inputValidator: (value) => {
              if (!value) {
                return '请输入结算批次';
              }
            }
          }).then(({ value }) => {
            this.ajax({ method: 'post', url: 'work/statement/make/generate/project', data: { id: row.id, batchName: value } }).then((response) => {
              this.$modal.msgSuccess('生成成功');
            });
          });
        }
      },
      del(row) {
        this.$confirm('是否确认删除选中项目? 会影响已经接包用户！', '警告', { type: 'warning' }).then(() => {
          this.get({ url: '/work/project/remove/' + (row.id || this.ids) }).then((response) => {
            this.$modal.msgSuccess('删除成功');
            this.getList();
          });
        });
      }
    }
  };
</script>
