<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="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户手机号"
          clearable
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名"
          clearable
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名"
          clearable
          @input="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
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['bst:lightingNum:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lightingNumList" @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 === 'userName'">
              <div v-if="d.row.userName != null">
                <i class="el-icon-user"/>
                <user-link :id="d.row.userId" :text="d.row.nickName" size="mini"/>
                <br/>
                <i class="el-icon-phone"/>
                <user-link :id="d.row.userId" :text="d.row.userName" size="mini"/>
              </div>
            </template>
            <template v-else-if="column.key === 'storeName'">
              <div v-if="d.row.storeName != null">
                <i class="el-icon-takeaway-box"/>
                <store-link :id="d.row.storeId" :name="d.row.storeName" size="mini"/>
              </div>
            </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
              type="text"
              icon="el-icon-present"
              size="mini"
              @click="handleOpenGift(scope.row)"
              v-has-permi="['bst:lightingNum:gift']"
            >赠送次数</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-has-permi="['bst:lightingNum:remove']"
          >删除</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="赠送爆灯次数" :visible.sync="openGift" width="500px" append-to-body>
      <el-form ref="formGift" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="赠送次数" prop="number">
          <el-input-number
            v-model="form.number"
            :min="1"
            controls-position="right"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openGift = false">取 消</el-button>
        <el-button type="primary" @click="submitGift">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLightingNum,
  delLightingNum,
  giftLightingNum
} from "@/api/bst/lightingNum";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import UserLink from "@/views/system/user/UserLink.vue";
import StoreLink from "@/views/bst/store/StoreLink.vue";
import {listStore} from "@/api/bst/store";

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

export default {
  name: "LightingNum",
  mixins: [$showColumns],
  props: {
    query: {
      type: Object,
      default: () => ({})
    }
  },
  components: {StoreLink, UserLink, FormCol},
  data() {
    return {

      span: 24,
      // 字段列表
      columns: [
        {key: 'userName', visible: true, label: '用户名', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'storeName', visible: true, label: '店铺名', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'number', visible: true, label: '爆灯数', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      storeOptions: [], // 父区域树数据
      parentOptions: [], // 父区域树数据
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 爆灯次数列表表格数据
      lightingNumList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        userId: null,
        storeId: null,
        mchId: null,
        number: null,
      },
      openGift: false,
      // 表单参数
      form: {
        userId: null,
        storeId: null,
        number : 1
      },
      // 表单校验
      rules: {
        number: [
          { required: true, message: "爆灯次数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query
    }
    this.getList();
    this.getStoreList();
  },
  methods: {
    /** 当排序按钮被点击时触发 **/
    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;
      listLightingNum(this.queryParams).then(response => {
        this.lightingNumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        storeId: null,
        number: 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
    },

    handleOpenGift(row) {
      this.openGift = true
      this.form = {
        userId: row.userId,
        storeId: row.storeId,
        number: 1
      }
    },

submitGift() {
  this.$refs.formGift.validate(valid => {
    if (valid) {
      giftLightingNum({
        userId: this.form.userId,
        storeId: this.form.storeId,
        number: this.form.number
      }).then(() => {
        this.$modal.msgSuccess("赠送成功")
        this.openGift = false
        this.getList() // 刷新列表
      }).catch(error => {
        this.$modal.msgError(error.msg || "操作失败")
      })
    }
  })
},
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除爆灯次数列表编号为"' + ids + '"的数据项？').then(function() {
        return delLightingNum(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/lightingNum/export', {
        ...this.queryParams
      }, `lightingNum_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
