<template>
  <div>
    <el-button type="default" size="small" @click="onShow(dialogTableVisible = true)">添加人员</el-button>
    <el-dialog title="添加人员" append-to-body :visible.sync="dialogTableVisible" width="50%">
      <p class="normal-margin">年度:</p>
      <el-date-picker
        disabled
        class="normal-margin"
        v-model="form.year"
        type="year"
        value-format="yyyy"
        placeholder="选择年"
        size="small">
      </el-date-picker>
      <p class="normal-margin">请选择人员:</p>
      <tablelayout :layout="tablecols" :data="list" :custom="true" :checkbox="true" height="calc(100vh - 452px)" @checkboxCallBack="checkboxCallBack">
        <template v-slot:customcol="scope">
          <p>{{scope.column.data[scope.column.columnname]}}</p>
        </template>
      </tablelayout>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogTableVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" :disabled="selection.length === 0" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import tablelayout from '@/components/table/index.vue'

export default {
  props:['data'],
  components:{
    tablelayout
  },
  data () {
    return {
      dialogTableVisible:false,
      form:{
        "year": '',
        "sa_salestargetbillid": 0,//新建年度是为0，添加人员时为具体的数据id
        "sales":[]
      },
      tablecols:[],
      list:[],
      selection:[]
    }
  },
  methods:{
    onShow(){
      this.data.year = String(this.data.year)
      this.form = Object.assign({},this.form,this.data)
      this.listData()
    },
    async listData () {
      const res = await this.$api.requested({
        "id": 20220901091302,
        "content": {
            "sa_salestargetbillid": this.$route.query.id,
            "where": {
              "condition": ""
            }
        },
      })
      res.data.forEach(e => {
        e.marea = e.area.map(m=>{
          return `${m.province}${m.city}${m.county}`
        })
        e.marea = e.marea.join(',')
      });
      this.list = res.data
    },
    checkboxCallBack (val) {
      this.selection = val
    },
    async onSubmit () {
      if (this.form.year === '') return this.$message({
        message:'请选择年度',
        type:'error'
      })
      const res = await this.$api.requested({
        "id": 20220831165302,
        "content": {
          "year":this.form.year,
          "sa_salestargetbillid": this.form.sa_salestargetbillid,
          "sales": this.selection
        },
      })
      this.tool.showMessage(res,()=>{
        this.dialogTableVisible = false
        this.$emit('onSuccess')
      })
    }
  },
  mounted () {
  },
  created () {
    this.tablecols = this.tool.tabelCol(this.$route.name)['personalTable'].tablecols
  }
}

</script>
<style>
</style>