<template>
  <div class="inline-16">
    <el-button type="primary" size="mini" @click="productBtn">添 加</el-button>
    <el-drawer
        title="添加授权范围"
        :visible.sync="drawer"
        size="70%"
        direction="rtl"
        append-to-body
        @close="drawer = false"
        :show-close="false">
      <div class="drawer__panel">
        <div style="display:flex;align-items:center">
          <el-input
              placeholder="请输入搜索内容"
              suffix-icon="el-icon-search"
              v-model="params.content.where.condition"
              style="width:200px"
              size="mini"
              class="input-with-select inline-16"
              @keyup.native.enter="listData(params.content.pageNumber=1)"
              @clear="listData(params.content.where.condition='')"
              clearable>
          </el-input>
        </div>
        <div style="margin-top: 15px">
          <selectTable v-if="drawer" ref="table" idName="sys_enterpriseid" @selectChange="selectChange" @upDateData="selectChange" :layout="tablecols" :data="list" :custom="true" height="500px">
            <template v-slot:customcol="scope">
              <div>{{scope.column.data[scope.column.columnname]}}</div>
              <div v-if="!scope.column.data[scope.column.columnname] && scope.column.columnname != 'operation' && scope.column.data[scope.column.columnname] !== 0">--</div>
            </template>
            <template v-slot:opreation="scope">
              <el-button type="text" size="mini" @click="addArea(scope.data)">添 加</el-button>
            </template>
          </selectTable>
        </div>
        <div style="margin-top:16px;text-align:right">
          <el-pagination
              background
              small
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="params.content.pageNumber"
              :page-size="params.content.pageSize"
              layout="total, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
        <div class="fixed__btn__panel">
          <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
          <el-button size="small" :disabled="!selectEnterprise.length" type="primary" @click="submit" class="normal-btn-width">确 定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import selectTable from '@/components/selectTable/index'
export default {
  name: '',
  props:['data'],
  components:{selectTable},
  data() {
    return {
      drawer:false,
      tableHieght:"calc(100vh - 380px)",
      tablecols:[],
      list:[],
      total:0,
      params:{
        "id": 20230102102803,
        "version":1,
        "content": {
            "sa_promotionid":'',
          "where":{
              "condition":""
          }
        }
      },
      selectEnterprise:[]
    };
  },
  computed:{
  },
  watch:{
  },
  methods: {
    async listData(){
      this.params.content.sa_promotionid = this.$route.query.id
      const res = await this.$api.requested(this.params)
      this.list = res.data
      this.total = res.total
      console.log(this.list)
    },
    async addArea (data) {
      let res = await this.$api.requested({
        "id": 20230102095903,
        "version":1,
        "content": {
            "sa_promotionid":this.$route.query.id,     //sat_notice_classid<=0时 为新增
            "enterpriseinfos":[
              {
                  "sa_promotion_authid":0,
                  "sys_enterpriseid":data.sys_enterpriseid,
                  "islimit":1,
                  "limitamount":'0',
                  "limitgroupqty":'0',
                  "limitordercount":'0'
              },
            ]
        }
      })
      this.tool.showMessage(res,() => {
        this.$emit('onSuccess')
        this.listData()
        this.drawer = false
      })
    },
    async submit () {
      let arr = this.$refs.table.allArr.map(item => {
        return {
          "sa_promotion_authid":0,
          "sys_enterpriseid":item.sys_enterpriseid,
          "islimit":1,
          "limitamount":0,
          "limitgroupqty":0,
          "limitordercount":0
        }
      })
      console.log('触发');
      
      if (arr.length) {
        console.log(arr);
        
        let res = await this.$api.requested({
          "id": 20230102095903,
          "version":1,
          "content": {
              "sa_promotionid":this.$route.query.id,     //sat_notice_classid<=0时 为新增
              "enterpriseinfos":arr
          }
        })
        this.tool.showMessage(res,() => {
          this.drawer = false
          this.$emit('onSuccess')
        })
      }
    },
    selectChange (data) {
      console.log(data,111);
      
      this.selectEnterprise = data
    },
    productBtn () {
      this.drawer = true
      this.listData()
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.listData()
    },
  },
  created () {
    this.tablecols = this.tool.tabelCol(this.$route.name).addPowerAreaTable.tablecols
  }
};
</script>

<style scoped>

</style>
