<template>
  <div class="policy-selector">
    <div class="policy-selector-content">
      <div style="border: 1px solid #E5E5E5;padding: 1px" id="huodong">
        <div
          style="padding: 10px 13px;font-size: 14px;border-bottom: 1px solid #E5E5E5;display: flex;justify-content: space-between;">
          <div style="line-height:200%">选择策略</div>
          <div style="display: flex">
            <el-input placeholder="请输入" suffix-icon="el-icon-search" style="margin-right:15px;width: 40%;"
              v-model="pageindex.policyName">
            </el-input>
            <el-button @click="onSearch" style="background-color: #3F9B6A;color: #fff">查询
            </el-button>
            <el-button @click="resetting" class="buttonHover"
              style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;">重置
            </el-button>
          </div>
        </div>
        <div style="padding: 15px;">
          <div style="padding: 0px 20px 0px 0px;max-height:40vh;overflow-y: auto;">
            <el-table ref="multipleTable" :data="celueData" tooltip-effect="dark"
              @selection-change="handleSelectionChange"
              :header-cell-style="{fontSize: '14px',color:'#0009',fontWeight: 'normal',backgroundColor:'#F2F3F5'}">
              <el-table-column label="选择" type="selection" width="55">
              </el-table-column>
              <el-table-column label="策略名称" prop="policyName" show-overflow-tooltip></el-table-column>
              <el-table-column label="适用资源" prop="applicableResources" show-overflow-tooltip></el-table-column>
              <el-table-column label="租金价格" prop="rentalPrice" show-overflow-tooltip></el-table-column>
              <el-table-column label="付款周期" prop="leaseTerm" show-overflow-tooltip></el-table-column>
              <!-- <el-table-column label="付款日" prop="payDay" show-overflow-tooltip></el-table-column> -->
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <div style="display: flex;justify-content: space-between;margin: 20px 0;" class="bom">
      <div style="font-size: 14px;">共 <span style="color: #3F9B6A;">{{total}}</span> 项数据</div>
      <el-pagination :current-page="pageindex.pageNumber+1" :page-sizes="[5,10, 20, 50, 100]"
        :page-size="pageindex.pageSize" background small layout="prev, pager, next" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <div class="policy-selector-footer" style="display: flex; justify-content: flex-end; align-items: center;">
      <el-button @click="confirm" style="background-color: #3F9B6A;color: #fff;">确定</el-button>
      <el-button @click="cancel" class="buttonHover"
        style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;">取消</el-button>
    </div>
  </div>
</template>

<script>
    import {
    cereResourceStrategylist
  } from '@/api/newly.js'
  import {
    ceGetAll
  } from '@/api/sam.js'

  export default {
    name: 'PolicySelector',
    data() {
      return {
        total: 0,
        pageindex: {
          isTemplate: "1",
          pageNumber: 0,
          pageSize: 5,
          policyName: ''
        },
        selectedRows: [],
        celueData: [],
        changelist: [],
        yelist: [

        ], //页码处理数据
        isone:false,
        datalist:[]
      };
    },
    async created() {

      await this.getAll()
      this.intdatalist()
    },
    props: {
      resourcesId: {
        type: String,
        default: function () {
          return ''
        }
      },
      type: {
        type: String,
        default: function () {
          return ''
        }
      },
    },
    methods: {
      async intdatalist() {
        let c1 = {
          pageNumber: 0,
          pageSize: 10,
          resourcesId: this.resourcesId
        }
        await cereResourceStrategylist(c1).then(res => {
          console.error(res)
          if (!Array.isArray(res.data)) {
            this.datalist = [];
            return;
          }
          this.datalist = res.data
        })
        let listfy = []
        const celue = await ceGetAll({
          pageNumber: 0,
          pageSize: 100
        })
        const total = celue.data.totalElements > 0 ? Math.ceil(celue.data.totalElements / this.pageindex.pageSize) : 0
        for (let index = 0; index < total; index++) {
          const element = {
            num: index,
            list: []
          }
          for (let i = 0; i < this.pageindex.pageSize; i++) {
            if ((index * (this.pageindex.pageSize)) + i < celue.data.totalElements) {
              const element1 = celue.data.content[(index * (this.pageindex.pageSize)) + i];
              var iscz = this.datalist.findIndex(item => item.id === element1.id)
              if (iscz == -1) { //说明arr中不存在id为objid的对象
              } else {
                element.list.push(element1)
              }
            }

          }

          listfy.push(element)
        }
        console.log(listfy)
        this.yelist = listfy
        if (this.yelist.length > 0) {
          this.setchange()
        }

      },
      toggleSelection(rows) {
        // console.error(rows)
        for (let index = 0; index < rows.length; index++) {
          const element = rows[index];
          this.changelist.push(element)
        }
      },
      setchange() {
        // console.error('改变已选中')
        // console.error(this.yelist[this.pageindex.pageNumber])
        for (let index = 0; index < this.yelist[this.pageindex.pageNumber].list.length; index++) {
          const element = this.yelist[this.pageindex.pageNumber].list[index];
          // console.error(element)
          for (let i = 0; i < this.celueData.length; i++) {
            const element1 = this.celueData[i];
            if (element.id == element1.id) {
              // console.error(element1.id)
              this.$refs.multipleTable.toggleRowSelection(element1);
            }
          }
        }
        this.isone =  true
      },
      async getAll() {
        const celue = await ceGetAll({...this.pageindex,applicableResources:this.type})
        this.celueData = celue.data.content
        this.total = celue.data.totalElements

      },
      async handleCurrentChange(val) {
        this.isone = false
        this.pageindex.pageNumber = val - 1
        await this.getAll()
        this.$nextTick(() => {
          this.setchange()
        })
      },
      handleSizeChange(val) {
        this.pageindex.pageSize = val
      },
      onSearch() {
        this.pageindex.pageNumber = 0
        this.getAll()
      },
      resetting() {
        this.pageindex = {
          pageNumber: 0,
          pageSize: 5,
          policyName: ''
        }
        this.getAll()
      },
      handleSelectionChange(selection) {
        this.selectedRows = selection;
        // console.error('改变')
        // console.error(this.selectedRows)
        if(this.isone) {
          // console.error('--------')
          this.yelist[this.pageindex.pageNumber].list = JSON.parse(JSON.stringify(this.selectedRows))
        }
        
      },
      confirm() {
        var list = []
        for (let i = 0; i < this.yelist.length; i++) {
          let item = this.yelist[i]
          if(item.list) {
            for (let index = 0; index < item.list.length; index++) {
              const element = item.list[index];
              list.push(element)
            }
          }
        }
        this.$emit('minSev',list);
      },
      cancel() {
        this.$emit('mingClose');
      }
    }
  };
</script>

<style scoped>
  .policy-selector {
    padding: 20px;
    /* padding: 20px;
    width: 100%;
    margin: 0 auto;
    border: 1px solid #ddd; */
    /* border-radius: 4px; */
    /* box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); */
  }

  .policy-selector-header {
    text-align: left;
    margin-bottom: 20px;
  }

  .policy-selector-header h3 {
    margin: 0;
    font-size: 18px;
    color: #303133;
  }

  .policy-selector-content {
    margin-bottom: 20px;
  }

  .policy-selector-footer {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
  ::v-deep .el-dialog__body {
    padding: 0 0 !important;
  }
</style>
