<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="拼桌名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入拼桌名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拼桌偏好" prop="prefer">
        <el-select v-model="queryParams.prefer" placeholder="请选择拼桌偏好" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.team_prefer"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="拼桌类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择拼桌类型" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.team_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="拼桌状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择拼桌状态" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.team_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡座名称" prop="boothName">
        <el-input
          v-model="queryParams.boothName"
          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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange" :default-sort="defaultSort"
              @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center"/>
      <template v-for="column of showColumns">
        <el-table-column
          :key="column.key"
          :label="column.label"
          :prop="column.key"
          :align="column.align"
          :min-width="column.minWidth"
          :sort-orders="orderSorts"
          :sortable="column.sortable"
          :show-overflow-tooltip="column.overflow"
          :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{ d.row[column.key] }}
            </template>
            <template v-else-if="column.key === 'prefer'">
              <dict-tag :options="dict.type.team_prefer" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'status'">
              <dict-tag :options="dict.type.team_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'storeName'">
              <i class="el-icon-takeaway-box"/>
              <store-link :id="d.row.storeId" :name="d.row.storeName"/>
            </template>
            <template v-else-if="column.key === 'boothName'">
              <booth-link :id="d.row.boothId" :text="d.row.boothName"/>
            </template>
            <template v-else-if="column.key === 'sponsorName'">
              <i class="el-icon-user"/>
              <user-link :id="d.row.sponsorId" :text="d.row.sponsorName"/>
            </template>
            <template v-else-if="column.key === 'bookTime'">
              <span v-if="d.row.status === TeamType.BOOK">{{ d.row.bookTime }}</span>
            </template>
            <template v-else-if="column.key === 'name'">
              <div v-if="d.row.name">
                <team-link :id="d.row.id" :name="d.row.name"/>
                <el-tag
                  size="mini"
                  type="success"
                  style="margin-left: 5px"
                >
      <span v-if="d.row.limitNum !== null && d.row.limitNum !== undefined">
        {{ d.row.currentNum || 0 }}/{{ d.row.limitNum }}
      </span>
                  <span v-else>
                     无人数限制
                  </span>
                </el-tag>
              </div>
            </template>
            <template v-else-if="column.key === 'type'">
              <dict-tag :options="dict.type.team_type" :value="d.row[column.key]"/>
            </template>
            <template v-else>
              {{ d.row[column.key] }}
            </template>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleSee(scope.row)"
            v-has-permi="['bst:team:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleForcedDis(scope.row)"
            v-has-permi="['bst:team:forcedDis']"
            v-if="scope.row.status === TeamStatus.NORMAL"
          >强制解散
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-has-permi="['bst:team:edit']"
            v-if="scope.row.status === TeamStatus.NORMAL"
          >修改
          </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"
    />

    <!-- 添加或修改拼桌对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <form-col :span="span" label="选择卡座" prop="boothId">
            <el-select
              v-model="form.boothId"
              placeholder="请选择卡座"
              clearable
              filterable
              :loading="loading"
            >
              <el-option
                v-for="item in boothList"
                :key="item.boothId"
                :label="item.boothName"
                :value="item.boothId"
              />
            </el-select>
          </form-col>
          <form-col :span="span" label="拼桌名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入拼桌名称"/>
          </form-col>
          <form-col :span="span" label="拼桌类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择拼桌类型">
              <el-option
                v-for="dict in dict.type.team_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </form-col>
          <form-col :span="span" label="预约时间" prop="bookTime" v-if="form.type === TeamType.BOOK">
            <el-date-picker clearable

                            v-model="form.bookTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm"
                            placeholder="请选择预约时间">
            </el-date-picker>
          </form-col>
          <form-col :span="span" label="人数限制" prop="limitNum">
            <el-input-number :min="1" v-model="form.limitNum" placeholder="请输入人数限制"/>
          </form-col>
          <form-col :span="span" label="拼桌偏好" prop="prefer">
            <el-select v-model="form.prefer" placeholder="请选择拼桌偏好">
              <el-option
                v-for="dict in dict.type.team_prefer"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </form-col>
          <form-col :span="span" label="拼桌主题" prop="topic">
            <el-input v-model="form.topic" placeholder="请输入拼桌主题"/>
          </form-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTeam, getTeam, delTeam, addTeam, updateTeam, forcedDis} from "@/api/bst/team";
import {$showColumns} from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import StoreLink from "@/views/bst/store/StoreLink.vue";
import Booth from "@/views/bst/booth/index.vue";
import BoothLink from "@/views/bst/booth/components/BoothLink.vue";
import UserLink from "@/views/system/user/UserLink.vue";
import {TeamStatus, TeamType} from "@/utils/constants";
import {listBooth} from "@/api/bst/booth";
import TeamLink from "@/views/bst/team/TeamLink.vue";

// 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "Team",
  computed: {
    TeamType() {
      return TeamType
    },
    TeamStatus() {
      return TeamStatus
    }
  },
  mixins: [$showColumns],
  dicts: ['team_prefer', 'team_type', 'team_status'],
  components: {TeamLink, UserLink, BoothLink, Booth, StoreLink, FormCol},
  data() {
    return {
      span: 24,
      // 字段列表
      columns: [
        {
          key: 'name',
          visible: true,
          label: '拼桌名称',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: null
        },
        {
          key: 'sponsorName',
          visible: true,
          label: '发起人',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: null
        },
        {
          key: 'prefer',
          visible: true,
          label: '拼桌偏好',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: "120"
        },
        {
          key: 'type',
          visible: true,
          label: '拼桌类型',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: null
        },
        {
          key: 'status',
          visible: true,
          label: '拼桌状态',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: "120"
        },
        {
          key: 'storeName',
          visible: true,
          label: '店铺名称',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: null
        },
        {
          key: 'boothName',
          visible: true,
          label: '卡座名称',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: null
        },
        {
          key: 'bookTime',
          visible: true,
          label: '预约时间',
          minWidth: null,
          sortable: false,
          overflow: false,
          align: 'center',
          width: "120"
        },
        {
          key: 'createTime',
          visible: true,
          label: '创建时间',
          minWidth: null,
          sortable: true,
          overflow: false,
          align: 'center',
          width: "120"
        },
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼桌表格数据
      teamList: [],
      // 卡座列表
      boothList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        storeId: null,
        boothId: null,
        name: null,
        prefer: null,
        type: null,
        topic: null,
        limitNum: null,
        bookTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "拼桌名称不能为空", trigger: "blur"}
        ],
        boothId: [
          {required: true, message: "所属卡座不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "拼桌类型不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getBoothList();
  },
  methods: {
    // 查看拼桌拼桌详情
    handleSee(row) {
      this.$router.push({path: `/view/team/${row.id}`})
    },
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询拼桌列表 */
    getList() {
      this.loading = true;
      listTeam(this.queryParams).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询卡座列表列表 */
    getBoothList(storeId) {
      this.loading = true;
      listBooth({storeId: storeId}).then(response => {
        this.boothList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        boothId: null,
        name: null,
        prefer: null,
        type: null,
        topic: null,
        limitNum: null,
        bookTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼桌";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.getBoothList(row.storeId)
      getTeam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拼桌拼桌信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拼桌编号为"' + ids + '"的数据项？').then(function () {
        return delTeam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleForcedDis(row) {
      const id = row.id;
      this.$modal.confirm('确定强制解散id为"' + id + '"的拼桌拼桌吗').then(function () {
        return forcedDis(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("强制解散成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/team/export', {
        ...this.queryParams
      }, `team_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
