<template>
  <div class="container normal-panel">
    <!-- 表格搜索 -->
    <p class="normal-title normal-margin">预测模板</p>
    <div class="flex-align-center  search-panel normal-margin">
      <p>搜索:</p>
      <el-input style="width:200px" size="small" placeholder="模板名称,模板编号" @keyup.native.enter="listData(params.content.pageNumber = 1)" @clear="listData(params.content.pageNumber = 1)" v-model="params.content.where.condition" prefix-icon="el-icon-search" clearable></el-input>
    </div>
    <!-- 表格主题 -->
    <tableLayout :layout="tablecols" :data="list" :opwidth="200" :custom="true" :fixedName="'operation'" height="300px" @rowClick="rowClick">
      <template v-slot:customcol="scope">
        <div v-if="scope.column.columnname === 'status'">
          <span :style="scope.column.data.status === '发布'?'color:#52C41A':''">{{scope.column.data.status}}</span>
        </div>
        <div v-else-if="scope.column.columnname === 'baseonproject'">
          <span>{{scope.column.data.baseonproject === 1?'按产品类别及项目预测':'按产品类别预测'}}</span>
        </div>
        <div v-else-if="scope.column.columnname === 'taskdays'">
          <span>提前{{scope.column.data.taskdays}}天</span>
        </div>
        <div v-else-if="scope.column.columnname === 'periodpoint'">
          <span v-for="i in scope.column.data.periodpointchange" :key="i.index">
            {{scope.column.data.periodtype === '周'?'周'+ `${i===7?'日':i}`:i+'日'}},
          </span>
        </div>
        <p v-else>{{scope.column.data[scope.column.columnname]}}</p>
      </template>
      <template v-slot:opreation="scope">
<!--        <slot v-if="scope.data.status === '新建'" name="edit" :data="scope.data"></slot>-->
<!--        <slot v-if="scope.data.status !== '结束'" name="release" :data="scope.data"></slot>-->
<!--        <slot v-if="scope.data.status === '新建'" name="del" :data="scope.data"></slot>-->
<!--        <slot v-if="scope.data.status === '发布'" name="finish" :data="scope.data"></slot>-->
        <slot v-if="scope.data.status === '新建' || scope.data.status === '结束'"  name="edit" :data="scope.data"></slot>
        <slot   name="release" :data="scope.data"></slot>
        <slot v-if="scope.data.status === '新建' || scope.data.status === '结束' "  name="del" :data="scope.data"></slot>
        <slot v-if="scope.data.status === '发布' "  name="finish" :data="scope.data"></slot>
      </template>
    </tableLayout>
    <div style="margin-top:16px;text-align:right">
      <el-pagination
        background
        small
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="params.content.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

export default {
  components:{
  },
  data () {
    return {
      params:{
        "id": 20220906150203,
        "version":1,
        "content": {
          "pageNumber":1,
          "pageSize":10,
          "where":{
            "condition":""
          }
        }
      },
      tablecols:{},
      list:[],
      total:0,
      currentPage:0,
      lastDate:""
    }
  },
  methods:{
    async listData () {
      const res = await this.$api.requested(this.params)
      console.log(res)
      for (var i = 0;i<res.data.length;i++){
        var date = new Date(res.data[i].enddate)
        var nowDate = new Date()
        if (date < nowDate){
          if (res.data[i].status !='结束') {
            // this.changeStatus(res.data[i].sa_salesforecastmodelid)
            res.data[i].status = '结束'
          }
        }
      }
      console.log(res.data)
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.listData()
    },
    rowClick (row) {
      this.$emit('tableRowClick',row)
    },
    async changeStatus(id){
      console.log(id)
      const res = await this.$api.requested({
        "id": 20220906151103,
        "version":1,
        "content": {
          "sa_salesforecastmodelids": [id]
        }
      })
      console.log(res)
    }
  },
  mounted () {
    this.listData()
    this.tablecols = this.tool.tabelCol(this.$route.name)['templateTable'].tablecols
  }
}

</script>
<style>
</style>
<style scoped>
.search-panel p{
  width:40px;
  font-size:14px
}
</style>