<template>
  <div>
    <el-button size="small" type="primary" :disabled="isEdit()" @click="dialogFormVisible=true">新 增</el-button>
    <el-drawer title="新 增 商 品" append-to-body :visible.sync="dialogFormVisible" size="1000px">
      <div class="container">
        <div>
          <selectClass @brandChange="brandChange" @onClassChange="onClassChange" @Search="Search" @clearSearch="clearSearch" @clickField="clickField" :default="true"></selectClass>
          <el-input style="width:200px;margin-bottom:10px;margin-top:10px" size="small" placeholder="请输入商品" @clear="getProductList(params.content.pageNumber=1)"
            @keyup.native.enter="getProductList(params.content.pageNumber=1)" v-model="params.content.where.condition" clearable />
        </div>
        <Table ref="table" :layout="tablecols" :data="productList" :custom="false" height="500px">
        </Table>
        <el-pagination
          style="text-align:right;margin-top:10px"
          background
          small
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.content.pageNumber"
          :page-size="params.content.pageSize"
          layout="total, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import Table from './table'
import selectClass from './selectClass/Select'
export default {
  components:{Table,selectClass},
  inject:['isEdit'],
  name: "index",
  data() {
    return {
      options:[],
      dialogFormVisible: false,
      productList:'',
      selectProduct:[],
      params: {
        "id": 20221028154004,
        "content": {
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            },
            "sa_itempriceadjustid":this.$route.query.id,
            "itemclassid":57
        }
      },
      total:0,
      tablecols:[]
    }
  },
  provide () {
    return {
    }
  },
  watch: {
    dialogFormVisible (val) {
      !val ? this.$refs.table.$refs.tables.clearSelection() : ''
    }
  },
  mounted () {
    this.tablecols = this.tool.tabelCol(this.$route.name)['productList'].tablecols
    this.getProductList()
  },
  methods: {
    brandChange(id) {
      this.brandId = id
      this.getProductList()
    },
    onClassChange (n) {
      this.params.content.where.itemclassid = n.itemclassid
      this.getProductList()
      console.log(n);
      
    },
    pageChange(n) {
      this.params.content.pageNumber = n
      this.getProductList()
    },
    Search(data) {
      this.params.content.where.condition = data
      this.params.content.pageNumber = 1
      this.getProductList()
    },
    clearSearch() {
      this.params.content.where.condition = ''
      this.params.content.pageNumber = 1
      this.getProductList()
    },
    clickField (item) {
      this.params.content.tradefield = item.tradefield
      this.getProductList()
    },
    /* 提交 */
    async onSubmit () {
      let content = this.$refs.table.allArr.map(item => {
        return {
          "sa_itempriceadjust_itemsid":0,
          "itemid":item.itemid,
          "remarks":""
        }
      })
      let res = await this.$api.requested({
        "id": "20221025095104",
        "content": {
            "sa_itempriceadjustid": this.$route.query.id,
            "sa_itempriceadjust_items": content
        }
      })
      this.tool.showMessage(res,() => {
        this.$emit('onSuccess')
        this.dialogFormVisible = false
      })
    },
    checkboxCallBack (data) {
      this.selectProduct = data
    },
    /* 获取可添加的商品 */
    async getProductList () {
      let res = await this.$api.requested(this.params)
      this.productList = res.data
      this.total = res.total
      console.log(this.productList);
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.getProductList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.getProductList()
    },
  }
}
</script>

<style scoped>

</style>