<template>
  <div class="app-container">
    <!-- Start 列表页面 -->
    <div>
      <button-bar view-title="待预提专项返利列表" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">{{ searchxianshitit }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form ref="listQueryform" :inline="true" :model="listQuery" label-width="100px" class="tab-header">
              <el-form-item label="品牌">
                <el-select v-model="listQuery.params.brandSid" placeholder="请选择" filterable clearable>
                  <el-option v-for="item in brand_list" :key="item.sid" :label="item.brandName" :value="item.sid"/>
                </el-select>
              </el-form-item>
              <el-form-item label="创建日期">
                <el-date-picker v-model="listQuery.params.createStartTime" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="listQuery.params.createEndTime" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
              </el-form-item>
              <el-form-item label="返利类型">
                <el-select v-model="listQuery.params.rebateTypeKey" placeholder="请选择" filterable clearable>
                  <el-option v-for="item in rebateType_list" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"/>
                </el-select>
              </el-form-item>
              <el-form-item label="返利名称">
                <el-input v-model="listQuery.params.rebateName" placeholder="" clearable/>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" icon="el-icon-search" @click="handleFilter" size="small">查询</el-button>
              <el-button type="primary" icon="el-icon-refresh" @click="handleReset" size="small">重置</el-button>
            </div>
          </div>
        </div>
        <div class="listtop">
          <div class="tit">待预提专项返利列表</div>
          <el-button size="small" type="primary" @click="handleConfirm">确定</el-button>
        </div>
        <div class="">
          <el-table :key="tableKey" v-loading="listLoading" :data="list" border style="width: 100%;" @selection-change="handleSelectionChange">
            <el-table-column fixed type="selection" align="center" width="50px"/>
            <el-table-column fixed label="序号" type="index" :index="indexMethod" align="center" width="80px"/>
            <el-table-column label="品牌" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.brandName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="创建日期" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.createTime }}</span>
              </template>
            </el-table-column>
            <el-table-column label="返利类型" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.rebateTypeValue }}</span>
              </template>
            </el-table-column>
            <el-table-column label="返利名称" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.rebateName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="预计返利" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.estimateRebate }}</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pages">
          <!-- 翻页 -->
          <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import req from '@/api/specialrebate/specialrebatewithholding'
import Pagination from '@/components/pagination'
import ButtonBar from '@/components/ButtonBar'
import { brandDown, typeValues } from '@/api/cheliang/dictcommons'

export default {
  name: 'ZhuanXiangFanLiSelect',
  components: {
    Pagination,
    ButtonBar
  },
  data() {
    return {
      btndisabled: false,
      btnList: [
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      isSearchShow: false,
      searchxianshitit: '显示查询条件',
      // 查询条件 -----------
      tableKey: 0,
      list: [],
      sids: [],
      FormLoading: false,
      listLoading: false,
      rebateType_list: [],
      brand_list: [],
      listQuery: {
        params: {
          createOrgSid: '',
          brandSid: '',
          rebateTypeKey: '',
          rebateName: '',
          createEndTime: '',
          createStartTime: '',
          sidList: []
        },
        current: 1,
        size: 5,
        total: 0
      },
      rules: {}
    }
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  methods: {
    init() {
      typeValues({ type: 'rebateType' }).then((res) => {
        if (res.success) {
          this.rebateType_list = res.data
        }
      })
      brandDown({ useOrg: this.listQuery.params.createOrgSid }).then((res) => {
        if (res.success) {
          this.brand_list = res.data
        }
      })
    },
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    handleSelectionChange(row) {
      const aa = []
      row.forEach((element) => {
        aa.push({
          sid: element.sid,
          rebateTypeValue: element.rebateTypeValue,
          rebateTypeKey: element.rebateTypeKey,
          withRebate: element.estimateRebate,
          brandName: element.brandName,
          brandSid: element.brandSid
        })
      })
      this.sids = aa
      console.log('sids', this.sids)
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      req.pageList(this.listQuery).then((response) => {
        console.log('列表查询结果：', response)
        this.listLoading = false
        if (response.success) {
          this.list = response.data.records
          this.listQuery.total = response.data.total
        } else {
          this.list = []
          this.listQuery.total = 0
        }
      })
    },
    showData(value, createOrgSid, brandSid) {
      this.listQuery.params.createOrgSid = createOrgSid
      this.listQuery.params.brandSid = brandSid
      const aa = []
      if (value.length > 0) {
        for (var i = 0; i < value.length; i++) {
          for (var j = 0; j < value[i].specialRebateSids.length; j++) {
            aa.push(value[i].specialRebateSids[j])
          }
        }
        this.listQuery.params.sidList = aa
      } else {
        this.listQuery.params.sidList = []
      }
      this.init()
      this.getList()
    },
    // 查询按钮
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    handleReset() {
      this.listQuery.current = 1
      this.listQuery.total = 0
      this.listQuery.size = 5
      this.listQuery.params.createStartTime = ''
      this.listQuery.params.createEndTime = ''
      this.listQuery.params.rebateTypeKey = ''
      this.listQuery.params.rebateName = ''
      this.getList()
    },
    handleConfirm() {
      if (this.sids.length > 0) {
        this.$emit('backData', this.sids)
      } else {
        this.$notify({
          title: '提示',
          message: '请选择至少一条记录!',
          type: 'error',
          duration: 2000
        })
      }
    },
    doClose() {
      this.$emit('doback')
    }
  }
}
</script>
<style scoped>
</style>
