<template>
  <div>
    <el-button size="small" type="primary" @click="onShow(drawer = true)">添加项目</el-button>
    <el-drawer
      title="添加项目"
      :visible.sync="drawer"
      append-to-body
      direction="rtl"
      size="80%">
      <div class="drawer__panel">
        <el-table
          :data="list"
          style="width: 100%"
          size="mini"
          border>
          <el-table-column
            prop="status"
            label="状态">
            <template slot-scope="scope">
              <span style="color:#52c41a" v-if="scope.row.status == '跟进中'">{{scope.row.status}}</span>
              <span style="color:#fa8c16" v-else-if="scope.row.status == '已成交'">{{scope.row.status}}</span>
              <span style="color:#999999" v-else-if="scope.row.status == '已失败'">{{scope.row.status}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="projectnum"
            label="项目编号"
            width="180">
          </el-table-column>
          <el-table-column
            prop="projectname"
            label="项目名称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="address"
            label="项目地址">
          </el-table-column>
          <el-table-column
            label="操作"
            width="90">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="batchSelect(scope.row)">添 加</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="container normal-panel" style="text-align:right">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20, 50, 100, 200]"
            layout="total,sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  props:['mainData'],
  data () {
    return {
      drawer:false,
      param:{
        "id": 20220906154803,
        "content": {
          "pageNumber":1,
          "pageSize":20,
          "sa_salesforecastbillid":this.$route.query.id,
          "where":{
            "condition":""
          }
        }
      },
      list:[],
      currentPage:0,
      total:0,
      timer:0,
    }
  },
  methods:{
    async listData () {
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.listData()
    },
    onShow () {
      console.log(this.mainData)
      this.listData()
    },
    batchSelect(row){
      this.debounce(() => {
        this.addProject(row)
      }, 300)()
    },
    async addProject (row) {
      console.log(row,"row")
      const res = await this.$api.requested({
        "id": 20220913154403,
        "content": {
          "sa_salesforecastmodelid":this.mainData.sa_salesforecastmodelid,
          "sa_salesforecastbillid":this.$route.query.id,
          "sa_projectids": [{
            "sa_salesforecastprojectid":row.sa_salesforecastprojectid,
            "sa_projectid":row.sa_projectid,
            "discountrate":1
          }]
        }
      })
      this.tool.showMessage(res,()=>{
        this.$emit('onSuccess')
        this.listData()
      })
    },
    debounce (fn, wait) {
      let that = this
      return function () {
        if (that.timer !== null) clearTimeout(that.timer)
        that.timer = setTimeout(fn, wait)
      }
    },
  },
  mounted () {
    
  }
}

</script>
<style>
</style>