
<template>
  <page-header-wrapper>
    <template v-slot:content>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="优惠券生成人姓名">
                <a-input v-model="queryParam.item_id" placeholder="优惠券生成人姓名" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="优惠券类型" has-feedback>
                <a-select v-model="queryParam.business_type_id" @change="onBusinessChange" placeholder="优惠券类型">
                  <a-select-option v-for="item in businessArray" :key="item.id" :value="item.id">{{ item.title }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="优惠券状态" has-feedback>
                <a-select v-model="queryParam.business_type_id" @change="onBusinessChange" placeholder="优惠券状态">
                  <a-select-option v-for="item in businessArray" :key="item.id" :value="item.id">{{ item.title }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-button type="primary" @click="search"><a-icon type="search" />搜索</a-button>
              <!-- <a-button class="ml8" t ype="primary" @click="onAddRole"><a-icon type="plus" />新增</a-button> -->
              <a-button class="ml8" @click="resetSearch"><a-icon type="undo" />重置</a-button>
              <a-button
                v-if="exports_can"
                class="ml8"
                type="dashed"
                shape="round"
                icon="download"
                @click="exports"/>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </template>

    <!-- 列表 -->
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(row) => row.order_item_id"
        showPagination="auto"
        bordered
        size="default"
        :scroll="{ x: 1200, y: 1000 }">
        <div slot="name">
          <div>zdx</div>
        </div>
        <div slot="phone">
          <div>14745798066</div>
        </div>
        <div slot="time">
          <div>2023-11-11~2029-11-11</div>
        </div>

        <div slot="type">
          <div>3瓶优惠券</div>
        </div>
        <div slot="status">
          <div>待使用</div>
        </div>
        <span slot="created_at" slot-scope="text, record">{{ record.created_at }}</span>
        <!-- 操作 -->
        <div slot="actions" slot-scope="text, record">
          <a class="ant-dropdown-link" @click="onEdit(record)">查看明细</a>
        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, UserInfo } from '@/components'
import DeriveButton from '@/components/OrderDetailInfo/DeriveButton'
import { institutionsExports } from '@/api/storage'
import moment from 'moment'
import Info from '@/views/home/components/Info'
import {
  getList,
  init,
  canExport
} from '@/api/organization'
import storage from 'store'
import StockInfo from './widgets/StockInfo.vue'
import AddRole from './widgets/AddRole.vue'

// 列表规格
const columns = [
  {
    title: '优惠券生成人',
    children: [
      {
        title: '姓名',
        scopedSlots: { customRender: 'name' },
        align: 'center'
      },
      {
        title: '手机号',
        scopedSlots: { customRender: 'phone' },
        align: 'center'
      }
    ],
    align: 'center'
  },
  {
    title: '使用人',
    children: [
      {
        title: '姓名',
        scopedSlots: { customRender: 'name' },
        align: 'center'
      },
      {
        title: '手机号',
        scopedSlots: { customRender: 'phone' },
        align: 'center'
      }
    ],
    align: 'center'
  },
  {
    title: '优惠券有效期',
    scopedSlots: { customRender: 'time' },
    width: 240,
    align: 'center'
  },
  {
    title: '优惠券类型',
    scopedSlots: { customRender: 'type' },
    width: 140,
    align: 'center'
  },
  {
    title: '优惠券状态',
    scopedSlots: { customRender: 'status' },
    width: 140,
    align: 'center'
  }
  // {
  //   title: '操作',
  //   scopedSlots: { customRender: 'actions' },
  //   align: 'center',
  //   width: 160,
  //   fixed: 'right'
  // }
]

export default {
  name: 'CouponIndex',
  components: {
    STable,
    UserInfo,
    Info,
    DeriveButton
  },
  data () {
    return {
      exports_can: false,
      columns,
      searchType: false, // 展示全部的搜索
      businessArray: [], // 业务类型
      statusArray: [], // 机构列表

      queryParam: {
        order_no: '', // 订单编号
        institution: '', // 机构名称
        item_id: '', // 机构编号
        nickname: '', // 下单人
        business_type_id: '', // 业务类型
        status: '', // 机构状态
        paid_at: undefined, // 下单时间
        audit_at: undefined, // 执行时间
        settle_at: undefined // 结案时间
      },
      listCount: 0,
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters)
      }
    }
  },
  watch: {
    $route (to, from) {
      if (
        (from.name === 'Detail' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'Home' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'AssignrderIndex' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'SignInrderIndex' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'ReCheckrderIndex' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'HandleService' && to.name === 'FristCheckrderIndex') ||
        (from.name === 'CompleteOrder' && to.name === 'FristCheckrderIndex')
      ) {
        this.$refs.table.loadData()
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    moment,
    // 获取检索的基本样式
    init () {
      init().then(res => {
        this.statusArray = res.status // 机构状态
        this.businessArray = res.type // 业务类型
        // this.canExports()
      })
    },
    // 更改创建时间
    onCreateChange (dates, dateStrings) {
      this.queryParam.paid_at = dateStrings
    },
    // 更改执行时间
    onCarryChange (dates, dateStrings) {
      this.queryParam.audit_at = dateStrings
    },
    // 更改结案时间
    onSettleChange (dates, dateStrings) {
      this.queryParam.settle_at = dateStrings
    },
    // 处理状态
    onDisposeChange (e) {
      this.queryParam.dispose = e
    },
    // 业务类型
    onBusinessChange (e) {
      this.queryParam.business_type_id = e
    },
    // 机构状态
    onOrganizationChange (e) {
      this.queryParam.status = e
    },
    // 搜索列表
    search () {
      this.$refs.table.refresh(true) // 重置接口且刷先到第一页
    },
    // 重置搜索列表
    resetSearch () {
      this.queryParam = {
        order_no: '', // 订单编号
        institution: '', // 机构名称
        item_id: '', // 机构编号
        nickname: '', // 下单人
        business_type_id: '', // 业务类型
        status: '', // 机构状态
        paid_at: undefined, // 下单时间
        audit_at: undefined, // 执行时间
        settle_at: undefined// 结案时间
      }
      this.$refs.table.refresh(true) // 重置接口且刷先到第一页
    },
    // 机构是否能导出
    canExports () {
      console.log('canExports')
      canExport(storage.get('userid')).then(res => {
        this.exports_can = res.exports
      })
    },
    // 导出列表
    exports () {
      const params = this.queryParam
      params.audit_at = this.queryParam.audit_at || ''
      params.paid_at = this.queryParam.paid_at || ''
      params.settle_at = this.queryParam.settle_at || ''
      if (this.listCount > 0) {
        let downUrl = institutionsExports() + '?user_id=' + storage.get('userid')
        for (const key in params) {
          downUrl = downUrl + '&' + key + '=' + params[key]
        }
        window.open(downUrl)
      } else {
        this.$notification.error({
          message: '温馨提醒',
          description: '数据为空不能下载！'
        })
      }
    },
    // 新增用户
    onAddRole () {
      console.log('add....')
      const success = {
        title: '新增用户',
        width: 600,
        maskClosable: true,
        closable: true,
        okText: '确认新增',
        cancelText: '取消'
      }
      this.$dialog(
        AddRole,
        {
          on: {
            ok: () => {
              console.log('成功了。。')
              this.$refs.table.loadData()
            }
          }
        },
        success
      )
    },
    // 修改用户
    onEdit (item) {
      const success = {
        title: '库变详情',
        width: 1000,
        maskClosable: true,
        closable: true,
        footer: '',
        okText: '知道了'
      }
      this.$dialog(
        StockInfo,
        {
          item,
          on: {
            ok: () => {
              console.log('成功了。。')
              this.$refs.table.loadData()
            }
          }
        },
        success
      )
    }
  }
}
</script>

<style lang="less" scoped>
.ml8 {
  margin-left: 8px;
}
</style>
