<template>
  <div>
    <div>
      <add v-if="tool.checkAuth($route.name,'contacts')"  class="inline-16" :data="data" @insertSuccess="onSuccess"></add>
      <el-input
          placeholder="请输入搜索内容"
          :suffix-icon="params.content.where.condition?params.content.where.condition.length > 0?'':'':'el-icon-search'"
          v-model="params.content.where.condition"
          style="width:200px"
          size="small"
          class="input-with-select inline-16"
          @keyup.native.enter="listData(params.content.pageNumber = 1)"
          @clear="clearData"
          clearable>
      </el-input>
    </div>
    <div style="margin-top: 15px">
      <tableLayout :layout="tablecols" :data="list" :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">
          <edit :dataOld="data" :data="scope.data" class="inline-16" @editSuccess="onSuccess"></edit>
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              title="确定删除该联系人吗？"
              @confirm="delShow(scope.data.contactsid)"
          >
            <el-button slot="reference" size="mini" type="text" :disabled="!tool.checkAuth($route.name,'contacts')">删 除</el-button>
          </el-popconfirm>
<!--          <edit  :data="scope.data" class="inline-16" @editSuccess="onSuccess" :disabled="data.disabled"></edit>
          <delContact  :data="scope.data" @delSuccess="onSuccess" class="inline-16" :disabled="data.disabled"></delContact>-->
        </template>
      </tableLayout>
    </div>
    <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>
import add from './add'
import edit from './edit'
export default {
  props:["data"],
  name: "index",
  components:{add,edit},
  data(){
    return {
      search:"",
      tablecols:[],
      list:[],
      total:0,
      currentPage:0,
      params:{
        "id": "20221022165503",
        "content": {
          "sys_enterpriseid": '',
          "where":{
            "condition":"",
            "workaddress":0
          }
        }
      }
    }
  },
  methods:{
    async listData(){
      this.params.content.sys_enterpriseid = this.$route.query.id
      const res = await this.$api.requested(this.params)
      this.list = res.data
      this.total = res.total
    },
    onSuccess(){
      this.listData()
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.listData()
    },
    clearData(){
      this.listData(this.params.content.pageNumber = 1)
    },
    delShow(id){
      if (this.data.enterprisename === '' || this.data.abbreviation === '' || this.data.contact === ''){
        this.$message.error('请先将企业基础信息补充完整，再进行此操作');
      }else {
        this.onDel(id)
      }
    },
    async onDel(id){
      const res = await this.$api.requested({
        "id": "20221018145502",
        "content": {
          "contactsid": id,
          "sys_enterpriseid": this.$route.query.id,
          "deletereason":""
        }
      })
      this.tool.showMessage(res,()=>{
        this.listData()
      })
    }
  },
  mounted() {
    this.listData()
  },
  created() {
    this.tablecols = this.tool.tabelCol(this.$route.name).contactsTable.tablecols
  }
}
</script>

<style scoped>

</style>