<!-- 搜索表单 -->
<template>
  <el-form
    label-width="77px"
    class="ele-form-search"
    @keyup.enter.native="search"
    @submit.native.prevent
  >
    <el-row :gutter="15">
      <el-col v-bind="styleResponsive ? { lg: 5, md: 10 } : { span: 5 }">
        <el-form-item label="门店:">
          <ele-tree-select
            :data="groupsList"
            label-key="groupsName"
            value-key="groupsId"
            v-model="where.bizId"
            :clearable="true"
            placeholder="请选择"
            :disabled="false"
            :default-expand-all="false"
            :expand-on-click-node="false"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 10 } : { span: 5 }">
        <el-form-item label="商品:">
          <el-select
            clearable
            v-model="where.itemId"
            placeholder="请选择"
            class="ele-fluid"
          >
            <el-option
              v-for="item in goodsList"
              :key="item.itemId"
              :value="item.itemId"
              :label="item.title"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 10 } : { span: 5 }">
        <el-form-item label="姓名:">
          <el-input clearable v-model="where.uname" placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 10 } : { span: 5 }">
        <el-form-item label="手机号:">
          <el-input clearable v-model="where.mobile" placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 4, md: 8 } : { span: 4 }">
        <el-form-item label="订单类型:">
          <el-select
            clearable
            v-model="where.type"
            placeholder="请选择"
            class="ele-fluid"
          >
            <el-option label="权益" :value="0" />
            <el-option label="商品" :value="1" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 4, md: 8 } : { span: 4 }">
        <el-form-item label="状态:">
          <el-select
            clearable
            v-model="where.status"
            placeholder="请选择"
            class="ele-fluid"
          >
            <el-option label="未支付" :value="0" />
            <el-option label="已支付" :value="1" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 4, md: 8 } : { span: 4 }">
        <el-form-item label="内部人员:">
          <el-select
            clearable
            v-model="where.insiders"
            placeholder="请选择"
            class="ele-fluid"
          >
            <el-option label="是" :value="1" />
            <el-option label="否" :value="2" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 10 } : { span: 5 }">
        <el-form-item label="创建时间:">
          <el-date-picker
            unlink-panels
            v-model="dateRange"
            range-separator="-"
            type="datetimerange"
            end-placeholder="结束日期"
            start-placeholder="开始日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            class="ele-fluid"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 4, md: 8 } : { span: 4 }">
        <div class="ele-form-actions">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="ele-btn-icon"
            @click="search"
          >
            查询
          </el-button>
          <el-button @click="reset">重置</el-button>
        </div>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
  import { listGroups } from '@/api/sylive/groups';
  import { listGoods } from '@/api/sylive/goods';

  export default {
    data() {
      // 默认表单数据
      const defaultWhere = {
        status: 1,
        uname: '',
        mobile: '',
        type: '',
        bizId: '',
        insiders: ''
      };
      return {
        activityId: null,
        // 门店数据
        groupsList: [],
        // 商品数据
        goodsList: [],
        // 表单数据
        where: { ...defaultWhere },
        // 日期范围选择
        dateRange: [],
        // 日期时间选择器快捷项
        pickerOptions: {
          shortcuts: [
            {
              text: '最近一周',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit('pick', [start, end]);
              }
            }
          ]
        }
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {},
    methods: {
      /* 搜索 */
      search() {
        if (!this.dateRange) {
          this.dateRange = [];
        }
        const [createTimeStart, createTimeEnd] = this.dateRange;
        this.$emit('search', {
          ...this.where,
          createTimeStart,
          createTimeEnd
        });
      },
      /*  重置 */
      reset() {
        this.where = { ...this.defaultWhere };
        this.dateRange = [];
        this.search();
      },
      /* 查询分组 */
      groupsQuery() {
        listGroups({ activityId: this.activityId })
          .then((list) => {
            this.groupsList = this.$util.toTreeData({
              data: list,
              idField: 'groupsId',
              parentIdField: 'parentId'
            });
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /* 查询商品 */
      goodsQuery() {
        listGoods({ activityId: this.activityId })
          .then((list) => {
            this.goodsList = list;
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      }
    },
    watch: {
      $route: {
        handler(route) {
          const { path } = route;
          if (path !== '/sylive/groups-order') {
            return;
          }
          const activityId = this.$route.query.id;
          if (!activityId || activityId == this.activityId) {
            return;
          }
          this.activityId = activityId;
          if (this.$refs.table) {
            this.$refs.table.reload({ page: 1 });
          }
          this.groupsQuery();
          this.goodsQuery();
        },
        immediate: true
      }
    }
  };
</script>
