<template>
  <div>
    <div class="flex-align-center normal-margin">
      <add v-if="tool.checkAuth($route.name,'address')" 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>
    <tableLayout :layout="tablecols" :data="list" :opwidth="200" :custom="true"  :height="tableHieght" fixedName="operation">
      <template v-slot:customcol="scope">
        <div v-if="scope.column.columnname === 'province'">
          <p>{{ scope.column.data.province + '-' +scope.column.data.city + '-' + scope.column.data.county}}</p>
        </div>
        <div v-else-if="scope.column.columnname === 'isdefault'">
          <p>{{ scope.column.data.isdefault === 0?'否':'是'}}</p>
        </div>
        <div v-else-if="scope.column.columnname === 'type'">
          <div v-for="item in  scope.column.data.tag" :key="item.index"  style="float: left;margin-left: 5px">
            <el-tag color="#FAAB16" size="mini" type="warning" effect="dark">
              <span>{{item}}</span>
            </el-tag>
          </div>
        </div>
        <p v-else>{{scope.column.data[scope.column.columnname]}}</p>
      </template>
      <template v-slot:opreation="scope">
        <edit :data="scope.data" @updateSuccess="onSuccess" :dataOld="data"></edit>
        <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            title="确定删除该企业地址吗？"
            @confirm="delShow(scope.data.contactsid)"
        >
          <el-button slot="reference" type="text" size="mini" :disabled="!tool.checkAuth($route.name,'address')">删 除</el-button>
        </el-popconfirm>
      </template>
    </tableLayout>
    <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 {
  name: "index",
  props:["data"],
  data(){
    return {
      tableHieght:"600px",
      tablecols:[],
      list:[],
      listGet:[],
      total:0,
      currentPage:0,
      search:"",
      sys_enterpriseid:"",
      params:{
        "id": "20221022165503",
        "content": {
          "sys_enterpriseid":'',
          "where":{
            "condition":"",
            "workaddress":1
          }
        }
      }
    }
  },
  components:{add,edit},
  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
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.params.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.content.pageNumber = val
      this.listData()
    },
    onSuccess(){
      this.listData()
    },
    clearData(){
      this.params.content.pageNumber = 1
      this.listData()
    },
    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.data.sys_enterpriseid,
          "deletereason":""

        }
      })
      this.tool.showMessage(res,()=>{
        this.listData()
      })
    }
  },
  mounted() {
    this.listData()
  },
  created() {
    this.tablecols = this.tool.tabelCol(this.$route.name).customerAddressTable.tablecols
  }
}
</script>

<style scoped>

</style>