<template>
  <div>
    <div class="flex-align-center flex-between mt-10">
      <el-input style="width:200px" prefix-icon="el-icon-search" size="small" placeholder="搜索项目名称" v-model="param.content.where.condition" @keyup.native.enter="listData(param.content.pageNumber = 1)" @clear="listData(param.content.pageNumber = 1)" clearable></el-input>
      <addProject v-if="checkDateOut && tool.checkAuth($route.name,'projectProductListManage')" :mainData="mainData" @onSuccess="listData(param.content.pageNumber = 1)"></addProject>
    </div>
    <el-table
      :data="list"
      style="width: 100%"
      ref="tableProject"
      size="mini"
      border
      :highlight-current-row = true
      @row-click="rowClick">
      <el-table-column
        prop="projectnum"
        label="项目编号">
      </el-table-column>
      <el-table-column
        prop="projectname"
        label="项目名称">
      </el-table-column>
      <el-table-column
        prop="projecttype"
        label="项目类型">
      </el-table-column>
      <el-table-column
        prop="sumprojectamount"
        label="金额">
      </el-table-column>
      <el-table-column
        label="操作"
        width="150">
        <template slot-scope="scope">
          <addProduct v-if="checkDateOut && tool.checkAuth($route.name,'projectProductListManage')" class="inline-16" :id="scope.row.sa_projectid" :mainData="mainData"  @onSuccess="rowClick(act_row)"></addProduct>
          <el-button size="small" type="text" :disabled="!checkDateOut" @click="deleteProject(scope.row)">删 除{{scope.row.sa_salesforecastid}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="container normal-panel flex-align-center flex-between">
      <p v-if="list[0]">金额合计:{{list[0].sumamount}}</p>
      <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>
</template>

<script>
import addProject from '@/template/salesForecastUseProject/index.vue'
import addProduct from '@/template/salesForecastUseProject/addProduct.vue'
export default {
  props:['mainData'],
  components:{
    addProject,
    addProduct
  },
  data () {
    return {
      param:{
        "id": 20220916115203,
        "version":1,
        "content": {
          "pageNumber":1,
          "pageSize":20,
          "sa_salesforecastbillid":this.$route.query.id,
          "where":{
            "condition":""
          }
        }
      },
      act_row:null,
      list:[],
      currentPage:0,
      total:0,
      checkDateOut:false,
      discountrate:0
    }
  },
  methods:{
    async listData () {
      const res = await this.$api.requested(this.param)
      console.log(res.data)
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
      this.$emit('queryProduct',this.act_row?this.act_row.sa_projectid:res.data[0].sa_projectid, this.act_row?res.data[this.act_row.rowindex -1].discountrate:res.data[0].discountrate,this.act_row?this.act_row.sa_salesforecastprojectid:res.data[0].sa_salesforecastprojectid)
      this.$nextTick(()=>{
        this.act_row?this.$refs.tableProject.setCurrentRow(this.list[this.act_row.rowindex -1]):this.$refs.tableProject.setCurrentRow(this.list[0])
      })
    },
    async deleteProject (row) {
      const res = await this.$api.requested({
        "id": 20220906155103,
        "content": {
            "sa_salesforecastid":0,
            "sa_projectid":row.sa_projectid,
            "sa_salesforecastbillid":this.$route.query.id
        }
      })
      this.tool.showMessage(res,()=>{
        this.listData()
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.listData()
    },
    rowClick (row) {
      console.log(row)
      this.act_row = row
      // this.listData()
      this.$emit('queryProduct',row.sa_projectid,row.discountrate,row.sa_salesforecastprojectid)
    },
    /*是否过期*/
    isOverdue(){
      console.log(this.mainData,"获取数据")
      let date = new Date(this.mainData.periodend)
      console.log(date)
      let now = Date.now()
      if (date.getTime() < now) {
        console.log('已过期')
        this.checkDateOut =  false
      } else {
        console.log('未过期')
        this.checkDateOut = true
      }
    }
  },
  watch:{
    mainData (val) {
      let date = new Date(this.mainData.periodend)
      let now = Date.now()
      if (date.getTime() < now) {
        console.log('已过期')
        this.checkDateOut =  false
      } else {
        console.log('未过期')
        this.checkDateOut = true
      }
    }
  },
  mounted () {
    this.listData()
    this.isOverdue()
  }
}

</script>
<style>
</style>