<template>
  <div>
    <el-button size="small" type="primary" @click="onshow">新 建</el-button>
    <el-drawer
        title="新建关联客户"
        :visible.sync="visible"
        size="800px"
        direction="rtl"
        append-to-body
        :show-close="false"
        @close="onClose">
      <div class="drawer__panel">
        <el-row :gutter="20">
            <el-col :span="24">
                  <el-input
                      style="width:300px;margin-bottom: 10px"
                      placeholder="请输入内容"
                      v-model="customerParam.content.where.condition"
                      clearable
                      @clear="customerList(customerParam.content.pageNumber = 1)"
                      size="mini"
                      @keyup.enter.native="customerList(customerParam.content.pageNumber = 1)">
                    <i slot="prefix" class="el-icon-search" @click="customerList(customerParam.content.pageNumber = 1)"></i>
                  </el-input>
                  <el-table :data="customer.customerData" @row-click="customerData" height="802px" :header-cell-style="{background:'#EEEEEE',color:'#333'}" size="mini">
                    <el-table-column
                        label="客户编号"
                        width="180">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.sa_customersid?scope.row.sa_customersid:'--' }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="客户名称"
                        width="200">
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.enterprisename?scope.row.enterprisename:'--'}}</span>
                      </template>
                    </el-table-column>
                    <el-table-column
                        label="地址"
                    >
                      <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.address?scope.row.address:'--' }}</span>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div class="container normal-panel" style="text-align:right">
                    <el-pagination
                        background
                        small
                        @size-change="handleSizeChangeCustomer"
                        @current-change="handleCurrentChangeCustomer"
                        :page-sizes="[10,20,50,100,]"
                        layout="total,sizes, prev, pager, next, jumper"
                        :current-page="customer.currentPage"
                        :total="customer.total">
                    </el-pagination>
                  </div>
<!--                  <el-input slot="reference" :readonly="true" v-model="form.enterprisename" autocomplete="off" placeholder="请选择客户"  @input="selectCustomer"></el-input>-->
            </el-col>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="onClose" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
    <enterprise
        :visible="visibleEnterprise"
        :index="index"
        :sa_projectid="form.sa_projectid"
        v-if="visibleEnterprise"
        @onEnterprise="onEnterprise">
    </enterprise>
  </div>
</template>

<script>
import enterprise from './enterprise'
export default {
  name: "add",
  props:["data"],
  // inject:["enterpriseType"],
  components:{enterprise},
  data(){
    return {
      visible:false,
      visibleEnterprise:false,
      customerShow:false,
      customertype:'',
      index:'',
      form:{
        sa_project_partiesid: 0,
        sa_projectid: '',
        sys_enterpriseid:'',
        enterprisename:'',
        remarks:""
      },
      rules:{
        enterprisename:[
          { required: true, message: '请选择企业', trigger: 'change'},
        ],
      },
      customerParam:{
        "id": 20220920083901,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": "",
            "type":5,
            "sa_projectid":""
          }
        }
      },
      customer:{
        customerData:[],
        total:0,
        currentPage:0,
      },
    }
  },
  methods:{
    onshow(){
      console.log("新增企业缔约信息")
      console.log(this.data)
      this.visible = true
      console.log(this.enterpriseTypeList,'客户类型')
      this.selectCustomer()
    },
    async onSubmit(){
      this.form.sa_projectid = this.$route.query.id
      const res = await this.$api.requested({
        "id": 20221027143602,
        "content": this.form
      })
      this.tool.showMessage(res,()=>{
        this.$emit('addSuccess')
        this.onClose()
      })
    },
    onClose(){
      this.visible = false
      this.form={
        sa_project_partiesid: 0,
        sa_projectid: '',
        sys_enterpriseid:'',
        enterprisename:'',
        remarks:""
      }
    },
    enterpriseList(){
      this.visibleEnterprise = true
    },
    /*客户列表*/
    onEnterprise(data){
      this.visibleEnterprise = false
      console.log("企业信息",data)
      this.form.sys_enterpriseid = data.sys_enterpriseid
      this.form.enterprisename = data.enterprisename
    },
    async customerList(){
      this.customerParam.content.where.sa_projectid = this.$route.query.id
      const res = await this.$api.requested(this.customerParam)
      this.customer.customerData = res.data
      this.customer.total = res.total
      this.customer.currentPage = res.pageNumber
    },
    selectCustomer(){
      this.customerParam.content.where.condition = this.form.enterprisename
      this.customerList()
    },
    /*客户选择信息*/
    customerData(val){
      this.form.sys_enterpriseid = val.sys_enterpriseid
      this.form.enterprisename = val.enterprisename
      this.onSubmit()
    },
    /*客户分页*/
    handleSizeChangeCustomer(val) {
      // console.log(`每页 ${val} 条`);
      this.customerParam.content.pageSize = val
      this.customerList()
    },
    handleCurrentChangeCustomer(val) {
      // console.log(`当前页: ${val}`);
      this.customerParam.content.pageNumber = val
      this.customerList()
    },

  },
  mounted() {
    this.selectCustomer()
  }
}
</script>

<style scoped>
>>> .el-divider--horizontal {
  height: 1px;
  width: 100%;
  margin: 0px;
}
/deep/.el-input__prefix {
  display: flex;
  align-items: center;
}
</style>