<template>
  <div>
    <addProject ref="addProject" :param="paramProject" class="inline-16" @add="addProject" @adds="addProjects"></addProject>
    <el-button :type="items.length > 0?'primary':''" size="small" @click="delProjects" :disabled="items.length === 0">删 除</el-button>
    <tableList style="margin-top: 10px" :checkbox="true" :loading="loading" :layout="projectCols" :data="list" :opwidth="200" :custom="true"  fixedName="operation" @selectionChange="selectionChange">
      <template v-slot:customcol="scope">
        <div v-if="scope.column.columnname === 'operation'">
          <span>{{scope.column.data[scope.column.columnname]}}</span>
        </div>
        <p v-else>{{scope.column.data[scope.column.columnname]?scope.column.data[scope.column.columnname]:'--'}}</p>
      </template>
      <template v-slot:opreation="scope">
        <el-button type="text" size="mini" @click="onProduct(scope.data)">添加产品</el-button>
        <el-button size="mini" type="text" @click="delProject(scope.data)">删 除</el-button>
      </template>
    </tableList>
    <tableList style="margin-top: 10px" :checkbox="true" :loading="loading" :layout="productCols" :data="productList" :opwidth="200" :custom="true"  fixedName="operation">
      <template v-slot:customcol="scope">
        <div v-if="scope.column.columnname === 'operation'">
          <span>{{scope.column.data[scope.column.columnname]}}</span>
        </div>
        <p v-else>{{scope.column.data[scope.column.columnname]?scope.column.data[scope.column.columnname]:'--'}}</p>
      </template>
      <template v-slot:opreation="scope">
        <el-button size="mini" type="text" @click="delProduct(scope.data)">删 除</el-button>
      </template>
    </tableList>
    <el-drawer
        title="添加商品"
        :visible.sync="dialogVisible"
        size="90%"
        direction="rtl"
        :show-close="false"
        append-to-body
        >
      <addProduct ref="productRef" :params="paramProduct" :tablecolsAdd="tablecolsAdd" :querySa_brandid="brandid"
                  :tradefield="tradefield" :discountrate="discountrate"  @addProduct="addProduct" @addSuccess="addProducts"></addProduct>
    </el-drawer>
  </div>
</template>

<script>
import tableList from '@/components/table/index5'
import addProject from '@/template/prediction/addProjectTemp'
import addProduct from '@/template/prediction/addProductTemp'
export default {
  name: "projectList",
  components:{tableList,addProject,addProduct},
  data(){
    return {
      dialogVisible:false,
      brandid:'',
      tradefield:'',
      sa_brandid:'',
      discountrate:'',
      projectCols:[],
      productCols:[],
      tablecolsAdd:[],
      list:[],
      productList:[],
      loading:false,
      items:[],
      selectProject:'',
      projectParam:{
        "id": 20230705144904,
        "content": {
          "sa_salesforecastbillid":this.$route.query.id,
          "where":{
            "condition":""
          }
        }
      },
      productParam:{
        "id": 20230705145004,
        "content": {
          "sa_salesforecastprojectid": '',
          "where":{
            "condition":""
          }
        }
      },
      paramProject:{
        "id": 20230705145604,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "sa_salesforecastbillid":this.$route.query.id,
          "where": {
            "condition": ""
          }
        }
      },
      paramProduct:{
        "id": 20230705145704,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "sa_salesforecastbillid":this.$route.query.id,
          "sa_salesforecastprojectid":'',
          "sa_projectid":"",
          "where": {
            "condition": ""
          }
        }
      }
    }
  },
  methods:{
    /*项目列表*/
    async listData(){
      const res = await this.$api.requested(this.projectParam)
      this.list = res.data
      this.selectProject = res.data[0].sa_salesforecastprojectid
      this.productData(res.data[0].sa_salesforecastprojectid)
    },
    /*产品列表*/
    async productData(id){
      this.productParam.content.sa_salesforecastprojectid = id
      const res = await this.$api.requested(this.productParam)
      this.productList = res.data
    },
    /*添加项目*/
    async addProject(id){
      const res = await this.$api.requested({
        "id": 20230705145104,
        "content": {
          "sa_salesforecastbillid": this.$route.query.id,
          "ownertable": "sa_project",
          "salesforecastproject": [
            {
              "sa_salesforecastprojectid": 0,
              "ownerid": id,
              "discountrate": 0.1
            }
          ]
        }
      })
      this.tool.showMessage(res,()=>{
        this.projectParam.content.pageNumber = 1
        this.listData()
      })
    },
    /*批量添加项目*/
    async addProjects(ids){
      const res = await this.$api.requested({
        "id": 20230705145104,
        "content": {
          "sa_salesforecastbillid": this.$route.query.id,
          "ownertable": "sa_project",
          "salesforecastproject": ids
        }
      })
      this.tool.showMessage(res,()=>{
        this.projectParam.content.pageNumber = 1
        this.listData()
      })
    },
    /*删除项目*/
    async delProject(val){
      const res = await this.$api.requested({
        "id": 20230705145304,
        "content": {
          "sa_salesforecastbillid":this.$route.query.id,
          "sa_salesforecastprojectids":[val.sa_salesforecastprojectid]
        }
      })
      this.tool.showMessage(res,()=>{
        this.projectParam.content.pageNumber = 1
        this.listData()
      })

    },
    /*批量删除项目*/
    async delProjects(){
      console.log(this.items)
      let ids = []
      ids = this.items.map(item=>item.sa_salesforecastprojectid)
      console.log(ids)
      const res = await this.$api.requested({
        "id": 20230705145304,
        "content": {
          "sa_salesforecastbillid":this.$route.query.id,
          "sa_salesforecastprojectids":ids
        }
      })
      this.tool.showMessage(res,()=>{
        this.items = []
        this.projectParam.content.pageNumber = 1
        this.listData()
      })
    },
    /*勾选的项目*/
    selectionChange(val){
      this.items = val
    },
    /*添加产品*/
    onProduct(val){
      console.log(val,'项目信息')
      this.paramProduct.content.sa_salesforecastprojectid = val.sa_salesforecastprojectid
      this.paramProduct.content.sa_projectid = val.sa_projectid
      this.tradefield = val.tradefield
      this.sa_brandid = val.sa_brandid
      this.discountrate = val.discountrate
      this.dialogVisible = true
      this.$nextTick(()=>{
        this.$refs.productRef.onShow()
      })

    },
    async addProduct(val){
      console.log(val,'需要添加的产品')
      const res = await this.$api.requested({
        "id": 20230705145204,
        "content": {
          "sa_salesforecastbillid": this.$route.query.id,
          "sa_salesforecastprojectid": this.paramProduct.content.sa_salesforecastprojectid,
          "salesforecast": [
            {
              "itemid": val.itemid,
              "orderqty": val.qty,
              "orderamount": val.orderamount,
              "price": val.price,
              "sa_salesforecastid":0
            }
          ]
        }
      })
      this.tool.showMessage(res,()=>{
        this.dialogVisible = false
        this.productData(this.selectProject)
      })
    },
    async addProducts(val){
      console.log(val,'需要添加的产品')
      let items = []
      items = val.map(item=>{
        return {
          "itemid": item.itemid,
          "orderqty": item.qty,
          "orderamount": (item.marketprice * this.discountrate)*item.qty,
          "price": item.marketprice * this.discountrate,
          "sa_salesforecastid":0
        }
      })
      console.log(items,'item')
      const res = await this.$api.requested({
        "id": 20230705145204,
        "content": {
          "sa_salesforecastbillid": this.$route.query.id,
          "sa_salesforecastprojectid": this.paramProduct.content.sa_salesforecastprojectid,
          "salesforecast": items
        }
      })
      this.tool.showMessage(res,()=>{
        this.dialogVisible = false
        this.productData(this.selectProject)
      })
    },
    /*删除商品*/
    async delProduct(val){
      console.log(val)
      const res = await this.$api.requested({
        "id": 20230705145404,
        "content": {
          "sa_salesforecastbillid":this.$route.query.id,
          "sa_salesforecastids":[val.sa_salesforecastid]
        }
      })
      this.tool.showMessage(res,()=>{
        this.productData(this.selectProject)
      })
    }
  },
  mounted() {
    this.listData()
  },
  created() {
    this.productCols = this.tool.tabelCol(this.$route.name).productList.tablecols
    this.projectCols = this.tool.tabelCol(this.$route.name).projectList.tablecols
    this.tablecolsAdd = this.tool.tabelCol(this.$route.name).addProductTable.tablecols
  },
}
</script>

<style scoped>

</style>
