<template>
  <div>
    <el-button size="small" type="primary" @click="onshow">新 建</el-button>
    <el-drawer
        title="新建联系人"
        :visible.sync="dialogFormVisible"
        size="600px"
        direction="rtl"
        :show-close="false"
        append-to-body
        @close="onClose">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules"  ref="form"  size="mini" label-position="right" label-width="75px">
            <el-col :span="24">
              <el-form-item  label="姓名:" prop="name">
                <el-popover
                    placement="bottom-start"
                    width="900"
                    v-model="enterpriseContact.show"
                    trigger="click">
                  <el-tabs type="border-card" @tab-click="changeTab">
                    <el-tab-pane label="企业联系人">
                      <el-table
                          border
                          :data="enterpriseContact.listData"
                          @row-click="enterpriseContactSelect"
                          :header-cell-style="{background:'#EEEEEE',color:'#333'}"
                          size="mini">
                        <el-table-column width="80" label="姓名">
                          <template slot-scope="scope">
                            <span >{{ scope.row.name?scope.row.name:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="120" label="手机号">
                          <template slot-scope="scope">
                            <span >{{ scope.row.phonenumber?scope.row.phonenumber:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="200"  label="邮箱">
                          <template slot-scope="scope">
                            <span >{{ scope.row.email?scope.row.email:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="120"  label="部门">
                          <template slot-scope="scope">
                            <span >{{ scope.row.position?scope.row.position:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="120"  label="职位">
                        <template slot-scope="scope">
                          <span >{{ scope.row.depname?scope.row.depname:'--' }}</span>
                        </template>
                        </el-table-column>
                        <el-table-column width="80"  label="性别">
                          <template slot-scope="scope">
                            <span >{{ scope.row.sex?scope.row.sex:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="120"  label="生日">
                          <template slot-scope="scope">
                            <span >{{ scope.row.birthday?scope.row.birthday:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="300"  label="家庭住址">
                          <template slot-scope="scope">
                            <span >{{ scope.row.address?scope.row.address:'--' }}</span>
                          </template>
                        </el-table-column>
                        <el-table-column width="200"  label="备注">
                          <template slot-scope="scope">
                            <span >{{ scope.row.remarks?scope.row.remarks:'--' }}</span>
                          </template>
                        </el-table-column>
                      </el-table>
                      <div style="display:flex;align-items:center;flex-direction:row-reverse;justify-content:space-between">
                        <div class="container normal-panel" style="text-align:right">
                          <el-pagination
                              background
                              @size-change="handleSizeChangeEnterprise"
                              @current-change="handleCurrentChangeEnterprise"
                              :current-page="enterpriseContact.currentPage"
                              :page-sizes="[20, 50, 100, 200]"
                              layout="total,sizes, prev, pager, next, jumper"
                              :total="enterpriseContact.total">
                          </el-pagination>
                        </div>
                        <slot name="footerLeft"></slot>
                      </div>
                    </el-tab-pane>
                    <el-tab-pane label="我的联系人" >
                        <el-table
                            border
                            :data="myContacts.listData"
                            @row-click="myContactSelect"
                            :header-cell-style="{background:'#EEEEEE',color:'#333'}"
                            size="mini">
                          <el-table-column width="80" label="姓名">
                            <template slot-scope="scope">
                              <span >{{ scope.row.name?scope.row.name:'--' }}</span>
                            </template>
                          </el-table-column>
                          <el-table-column width="120" label="手机号">
                            <template slot-scope="scope">
                              <span >{{ scope.row.phonenumber?scope.row.phonenumber:'--' }}</span>
                            </template>
                          </el-table-column>
                          <el-table-column width="200"  label="邮箱">
                            <template slot-scope="scope">
                              <span >{{ scope.row.email?scope.row.email:'--' }}</span>
                            </template>
                          </el-table-column>
                          <el-table-column width="120"  label="部门">
                            <template slot-scope="scope">
                              <span >{{ scope.row.position?scope.row.position:'--' }}</span>
                            </template>
                          </el-table-column><el-table-column width="120"  label="职位">
                          <template slot-scope="scope">
                            <span >{{ scope.row.depname?scope.row.depname:'--' }}</span>
                          </template>
                        </el-table-column><el-table-column width="80"  label="性别">
                          <template slot-scope="scope">
                            <span >{{ scope.row.sex?scope.row.sex:'--' }}</span>
                          </template>
                        </el-table-column><el-table-column width="120"  label="生日">
                          <template slot-scope="scope">
                            <span >{{ scope.row.birthday?scope.row.birthday:'--' }}</span>
                          </template>
                        </el-table-column>
                          <el-table-column width="300"  label="家庭住址">
                            <template slot-scope="scope">
                              <span >{{ scope.row.address?scope.row.address:'--' }}</span>
                            </template>
                          </el-table-column>
                          <el-table-column width="200"  label="备注">
                            <template slot-scope="scope">
                              <span >{{ scope.row.remarks?scope.row.remarks:'--' }}</span>
                            </template>
                          </el-table-column>
                        </el-table>
                        <div style="display:flex;align-items:center;flex-direction:row-reverse;justify-content:space-between">
                          <div class="container normal-panel" style="text-align:right">
                            <el-pagination
                                background
                                @size-change="handleSizeChangeMy"
                                @current-change="handleCurrentChangeMy"
                                :current-page="myContacts.currentPage"
                                :page-sizes="[20, 50, 100, 200]"
                                layout="total,sizes, prev, pager, next, jumper"
                                :total="myContacts.total">
                            </el-pagination>
                          </div>
                          <slot name="footerLeft"></slot>
                        </div>
                    </el-tab-pane>
                  </el-tabs>
                  <el-input  slot="reference"  autosize v-model="form.name" placeholder="请输入姓名" @input="searchData"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="手机号:" prop="phonenumber">
                <el-input   autosize v-model="form.phonenumber" placeholder="请输入手机号码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="邮箱:" :rules="[
              { pattern:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: '请输入有效的邮箱',trigger: 'blur' }
            ]">
                <el-input   autosize v-model="form.email" placeholder="请输入邮箱"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="电话:" >
                <el-input  autosize v-model="form.telephone" placeholder="请输入电话" ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="传真:" >
                <el-input  autosize v-model="form.fax" placeholder="请输入传真" ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="单位:" >
                <el-input  autosize v-model="form.enterprisename" placeholder="请输入单位" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="部门:" >
                <el-input  autosize v-model="form.depname" placeholder="请输入部门"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="职位:" >
                <el-input  autosize v-model="form.position" placeholder="请输入职位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="性别:" >
                <el-radio v-model="form.sex" label="男">男</el-radio>
                <el-radio v-model="form.sex" label="女">女</el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="生日:" >
                <el-date-picker
                    v-model="form.birthday"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择日期"
                    style="width: 100%"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="家庭住址:" >
                <el-input type="textarea" rows="5" v-model="form.address" placeholder="请输入家庭住址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="备注:" >
                <el-input type="textarea"  rows="5" v-model="form.remarks" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </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>
  </div>
</template>

<script>
export default {
  name: "add",
  props:["data"],
  data(){
    var validateMobilePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('手机号不可为空'));
      } else {
        if (value !== '') {
          var reg=/^1[3456789]\d{9}$/;
          if(!reg.test(value)){
            callback(new Error('请输入有效的手机号码'));
          }
        }
        callback();
      }
    };
    return {
      dialogFormVisible:false,
      visible:false,
      form:{
        contactsid: 0,
        sys_enterpriseid: "",
        name: "",
        sex: "",
        depname: "",
        position: "",
        birthday: "",
        phonenumber: "",
        email: "",
        address: "",
        remarks: "",
        enterprisename:'',
        province: "",
        city: "",
        county: "",
        workaddress: 0,
        isdefault: 1,
        isprimary: 1,
        sys_phonebookid:'',
        telephone:'',
        fax:'',
        ownerid: this.$route.query.id,
        ownertable: "sa_customers"
      },
      rules:{
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur,change' },
        ],
        phonenumber: [
          { required: true, validator: validateMobilePhone, trigger: 'blur' },
        ]
      },
      enterpriseContact:{
        param:{
          "content": {
            "sys_enterpriseid": '',
            "pageNumber": 1,
            "pageSize": 10,
            "where": {
              "condition": ""
            }
          },
          "id": 20221219193002,
        },
        listData:[],
        total:0,
        currentPage:0,
        show:false
      },
      myContacts:{
        param:{
          "id": 20221219161202,
          "content": {
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
              "condition": ""
            }
          }
        },
        listData:[],
        total:0,
        currentPage:0,
        show:false
      }
    }
  },
  methods:{
    onshow(){
      this.dialogFormVisible = true
      this.enterpriseContactList()
      this.myContactList()
      this.form.sys_enterpriseid = this.data.sys_enterpriseid
      this.form.enterprisename = this.data.enterprisename
    },
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221018141802",
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit("insertSuccess")
          this.$refs['form'].resetFields()
          this.form = {
            contactsid: 0,
            sys_enterpriseid: "",
            name: "",
            sex: "",
            depname: "",
            position: "",
            birthday: "",
            phonenumber: "",
            email: "",
            address: "",
            remarks: "",
            enterprisename:'',
            province: "",
            city: "",
            county: "",
            workaddress: 0,
            isdefault: 1,
            isprimary: 1,
            sys_phonebookid:'',
            ownerid: 1,
            ownertable: "sa_customers"
          }
          this.dialogFormVisible = false
        })
      })
    },
    searchData(){
      this.changeTab()
    },
    changeTab(){
      this.enterpriseContactList()
      this.myContactList()
    },
    /*企业联系人*/
    async enterpriseContactList(){
      this.enterpriseContact.param.content.where.condition = this.form.name
      this.enterpriseContact.param.content.sys_enterpriseid = this.data.sys_enterpriseid
      const res = await this.$api.requested(this.enterpriseContact.param)
      this.enterpriseContact.listData = res.data
      this.enterpriseContact.total = res.total
      this.enterpriseContact.currentPage = res.pageNumber
    },
    /*选择企业联系人*/
    enterpriseContactSelect(val){
      this.form.name = val.name
      this.form.phonenumber = val.phonenumber
      this.form.email = val.email
      this.form.position = val.position
      this.form.depname = val.depname
      this.form.sex = val.sex
      this.form.birthday = val.birthday
      this.form.address = val.address
      this.form.remarks = val.remarks
      this.enterpriseContact.show = false
    },
    handleSizeChangeEnterprise(val) {
      // console.log(`每页 ${val} 条`);
      this.enterpriseContact.param.content.pageSize = val
      this.enterpriseContactList()
    },
    handleCurrentChangeEnterprise(val) {
      // console.log(`当前页: ${val}`);
      this.enterpriseContact.param.content.pageNumber = val
      this.enterpriseContactList()
    },
    /*我的联系人*/
    async myContactList(){
      this.myContacts.param.content.where.condition = this.form.name
      this.myContacts.param.content.sys_enterpriseid = this.data.sys_enterpriseid
      const res = await this.$api.requested(this.myContacts.param)
      console.log(res,"我的联系人")
      this.myContacts.listData = res.data
      this.myContacts.total = res.total
      this.myContacts.currentPage = res.pageNumber
    },
    /*选择我的联系人*/
    myContactSelect(val){
      console.log(val,"选择的联系人")
      this.form.name = val.name
      this.form.phonenumber = val.phonenumber
      this.form.email = val.email
      this.form.position = val.position
      this.form.depname = val.depname
      this.form.sex = val.sex
      this.form.birthday = val.birthday
      this.form.address = val.address
      this.form.remarks = val.remarks
      this.form.sys_phonebookid = val.sys_phonebookid
      this.enterpriseContact.show = false
    },
    handleSizeChangeMy(val) {
      // console.log(`每页 ${val} 条`);
      this.enterpriseContact.param.content.pageSize = val
      this.enterpriseContactList()
    },
    handleCurrentChangeMy(val) {
      // console.log(`当前页: ${val}`);
      this.enterpriseContact.param.content.pageNumber = val
      this.enterpriseContactList()
    },
    onClose(){
      this.$refs['form'].resetFields()
      this.form = {
        contactsid: 0,
        sys_enterpriseid: "",
        name: "",
        sex: "",
        depname: "",
        position: "",
        birthday: "",
        phonenumber: "",
        email: "",
        address: "",
        remarks: "",
        enterprisename:'',
        province: "",
        city: "",
        county: "",
        workaddress: 0,
        isdefault: 1,
        isprimary: 1,
        sys_phonebookid:''
      }
      this.dialogFormVisible = false
    },
  }
}
</script>

<style scoped>

</style>