<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent v-show="showSearch">
      <el-form-item label="企业名称" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入企业名称" @keyup.enter.native="handleQuery" clearable />
      </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:company:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit" :disabled="ids.length != 1" @click="op('edit', ids)" v-hasPermi="['work:company:edit']">修改</el-button>
      <el-button type="danger" icon="el-icon-delete" :disabled="ids.length == 0" @click="del" v-hasPermi="['work:company: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="center" prop="companyName" width="250" />
      <el-table-column label="企业方负责人" align="center" prop="director" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="账户余额(元)" align="center" prop="money" />
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <el-table-column label="账号状态" align="center" prop="state" width="100">
        <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="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="op('detail', scope.row)" v-hasPermi="['work:company:list']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="op('edit', scope.row)" v-hasPermi="['work:company:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="del(scope.row)" v-hasPermi="['work:company: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';
export default {
  name: 'Company',
  data() {
    return {
      ids: [],
      showSearch: true,
      response: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        director: null,
        phone: null,
        state: null,
        orderByColumn: 'id',
        isAsc: 'desc'
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.ajax({ url: '/work/company/list', data: this.queryParams }).then((response) => {
        this.response = response;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    selects(rows) {
      this.ids = rows.map((item) => item.id);
    },
    op(tag, row) {
      if (tag == 'add') {
        this.iframe({ obj: edit, param: {}, title: '新增企业', width: '60%', height: '85%' });
      }
      const id = row.id || this.ids[0];
      if (tag == 'detail') {
        this.iframe({ obj: edit, param: { id: id, detail: true }, title: '查看企业信息', width: '60%', height: '75%' });
      }
      if (tag == 'edit') {
        this.iframe({ obj: edit, param: { id: id }, title: '编辑企业', width: '60%', height: '80%' });
      }
      //账号状态
      if (tag == 'change') {
        let text = row.state === 0 ? '启用' : '禁用';
        this.$confirm('确认要' + text + '该企业吗，停用后该企业的所有账号都不能登录？', '警告', { type: 'warning' })
          .then(() => {
            this.post({ url: '/work/company/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;
          });
      }
    },
    del(row) {
      this.$confirm('是否确认删除选中数据?', '警告', { type: 'warning' }).then(() => {
        this.get({ url: '/work/company/remove/' + (row.id || this.ids) }).then((response) => {
          this.$modal.msgSuccess('删除成功');
          this.getList();
        });
      });
    }
  }
};
</script>
