<template>
  <div>
    <el-button type="primary" size="small" @click="dialogTableVisible = true">新 建</el-button>
    <el-dialog title="新建目标" :visible.sync="dialogTableVisible" width="80%">
      <p class="normal-margin">年度:</p>
      <el-date-picker
        class="normal-margin"
        v-model="form.year"
        type="year"
        value-format="yyyy"
        placeholder="选择年"
        size="small">
      </el-date-picker>
      <div class="flex-align-center normal-margin flex-between">
        <p>请选择人员:</p>
        <div class="flex-align-center">
          <p>搜索:&nbsp;</p>
          <el-input style="width:250px" size="small" placeholder="人员姓名" v-model="condition" @keyup.native.enter="listData()" @clear="listData()" clearable></el-input>
        </div>
      </div>
      <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 {
  components:{
    tablelayout
  },
  data () {
    return {
      dialogTableVisible:false,
      form:{
        "year": '',
        "sa_salestargetbillid": 0,//新建年度是为0，添加人员时为具体的数据id
        "sales":[]
      },
      tablecols:[],
      list:[],
      selection:[],
      condition:''
    }
  },
  methods:{
    async listData () {
      const res = await this.$api.requested({
        "id": 20220901091302,
        "content": {
            "sa_salestargetbillid": 1,
            "where": {
              "condition": this.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": 20220903162902,
        "content": {
          "year":this.form.year,
          "sa_salestargetbillid": 0,
          "sales": this.selection
        },
      })
      this.tool.showMessage(res,()=>{
        this.dialogTableVisible = false
        this.$emit('onSuccess')
      })
    }
  },
  mounted () {
    this.listData()
  },
  created () {
    this.tablecols = this.tool.tabelCol(this.$route.name)['personalTable'].tablecols
  }
}

</script>
<style>
</style>