<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="所属驾校" prop="schoolId">
        <el-select v-model="queryParams.schoolId" filterable placeholder="请选择" value-key="schoolId" clearable size="small">
          <el-option v-for="(dict, index) in schoolOptions" :key="index" :label="dict.schoolName" :value="dict.schoolId" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属场地" prop="placeId">
        <el-select v-model="queryParams.placeId" filterable placeholder="请选择" clearable value-key="placeId" size="small">
          <el-option v-for="(dict, index) in placeOptions.filter(item => item.schoolId === queryParams.schoolId)" :key="index" :label="dict.name" :value="dict.placeId" />
        </el-select>
      </el-form-item>
      <el-form-item label="接待人" prop="coachName">
        <el-input v-model="queryParams.coachName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['sch:coach:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddAndUpdate(undefined)">新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="coachList">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="所属驾校" align="center" prop="schoolName" />
      <el-table-column label="所属场地" align="center" prop="placeName" />
      <el-table-column label="接待人" align="center" prop="coachName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="微信openid" align="center" prop="openId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['sch:coach:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleAddAndUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['sch:coach:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</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" />

    <!-- 添加或修改教练对话框 -->
    <coach-form v-if="dialogVisible" ref="dialogForm" :dialog-visible="dialogVisible" :school-options="schoolOptions" :place-options="placeOptions" @refreshDataList="getList" />
  </div>
</template>

<script>
import { listCoach, getCoach, delCoach } from '@/api/sch/coach';
import CoachForm from './components/CoachForm.vue';
import schoolAPi from '@/api/sch/school';
import { getAllPlaces } from '@/api/sch/place';

export default {
  name: 'Coach',
  components: { CoachForm },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教练表格数据
      coachList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        placeId: null,
        coachName: null,
        phone: null
      },
      dialogVisible: false,
      schoolOptions: [],
      placeOptions: []
    };
  },
  created () {
    this.getSchools();
    this.getPlaces();
    this.getList();
  },
  methods: {
    /** 查询教练列表 */
    getList () {
      this.loading = true;
      listCoach(this.queryParams).then(response => {
        this.coachList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    /** 新增或修改按钮操作 */
    handleAddAndUpdate (item) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.dialogForm.init(item);
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const coachIds = row.coachId || this.ids;
      this.$modal.confirm('是否确认删除教练编号为"' + coachIds + '"的数据项?').then(function () {
        return delCoach(coachIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('system/coach/export', {
        ...this.queryParams
      }, `coach_${new Date().getTime()}.xlsx`);
    },
    getSchools () {
      schoolAPi.allList().then((resp) => {
        this.schoolOptions = resp.data;
      });
    },
    getPlaces () {
      getAllPlaces({ status: '0', showInMap: true }).then((resp) => {
        this.placeOptions = resp.data;
      });
    }
  }
};
</script>