<template>
  <div>
    <el-dialog
      title="添加角色"
      :visible.sync="drawer"
      append-to-body
      direction="rtl"
      width="60%">
      <div class="flex-align-center flex-between " style="margin-top:-10px;margin-bottom: 10px">
        <div class="flex-align-center">
          <el-input size="small"  suffix-icon="el-icon-search" v-model="param.content.where.condition" placeholder="产品名称,编号" @keyup.enter.native="listData(param.content.pageNumber = 1)" @clear="listData(param.content.pageNumber = 1)" clearable></el-input>&nbsp;
        </div>
        <el-button type="primary" size="mini" @click="onSbmit" :disabled="selectRowId.length == 0">批量添加</el-button>
      </div>
      <div class="produtMag-panel" style="margin-top: 10px">
        <el-table
            @select="selectionChange"
            border
            ref="tables"
            :data="list"
            style="width: 100%">
          <el-table-column
              type="selection"
              width="42"
              fixed>
          </el-table-column>
          <el-table-column
              prop="rolename"
              label="角色名称">
          </el-table-column>
          <el-table-column
              prop="remarks"
              label="角色描述">
          </el-table-column>
          <el-table-column
              prop="createby"
              label="创建人">
          </el-table-column>
          <el-table-column
              prop="createdate"
              label="创建时间"
              width="160">
          </el-table-column>
        </el-table>
        <div>
          <div style="float: left">已选:{{selectRowId.length}}个角色</div>
          <div style="margin-top:16px;text-align:right">
            <el-pagination
                background
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="param.content.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
          </div>
        </div>
      </div>
    </el-dialog>
    <slot name="input"></slot>
  </div>
</template>

<script>
export default {
  props:['data'],
  components:{
  },
  data () {
    return {
      drawer:false,
      param:{
        "id": 20221101131902,
        "content": {
          "pageSize":20,
          "pageNumber":1,
          "where": {
            "condition": "",
          }
        }
      },
      list:[],
      currentPage:0,
      total:0,
      selectRowId:[],
      selectAllData:[]
    }
  },
  watch: {
    list (val) {
      this.list.forEach((row) => {
        this.selectRowId.forEach(item => {
          if (row.roleid == item) {
            this.$nextTick(() => {
              this.$refs["tables"].toggleRowSelection(row, true);
            })
          }
        })
      });    
    },
  },
  methods:{
    async listData () {
      this.param.content.sa_salesforecastbillid = this.$route.query.id
      const res = await this.$api.requested(this.param)
      console.log(res.data,"角色列表")
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    selectionChange (arrs,arr) {
      let index = this.selectRowId.findIndex(item => item == arr.roleid)
      if (index != -1) {
        this.selectAllData.splice(index,1)
        this.selectRowId.splice(index,1)
      } else {
        this.selectRowId.push(arr.roleid)
        this.selectAllData.push(arr)
      }
    },
    normalSetId () {
      this.listData()
      this.selectAllData = this.data.roleids.map(item => {
        return {
          roleid: item,
          rolename: this.data.rolemsg[item]
        }
      })
      console.log(this.selectAllData);
      
      this.selectRowId = this.data.roleids
    },
    onSbmit () {
      this.$emit('onResult',this.selectAllData)
      this.drawer = false
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.listData()
    },
  },
  mounted () {
  }
}

</script>
<style>
</style>
