<template>
  <div class="L-unitInfo enter-contacts-list" id="L-contacts-main">
    <div id="L-header-list" class="project-info-title">【联络人列表】</div>
    <section>
      <el-table v-loading="loading" class="tabTwo-table" border :data="tableData" :row-class-name="tableRowClassName" >  <!-- :max-height="tabHeader" -->
        <el-table-column label="联络人姓名" prop="llrxm" />
        <el-table-column label="联络人职务" prop="llrzw" />
        <el-table-column label="电子邮箱" prop="dzyx" />
        <el-table-column label="移动电话" prop="yddh" />
        <el-table-column label="固定电话" prop="gddh" />
        <el-table-column label="联络人编号" prop="llrbh" />
        <el-table-column label="入库时间" prop="dwTimestamp" />
        <el-table-column label="来源机构" prop="dwLyjg" />
      </el-table>
    </section>
    <el-row>
      <el-form :inline="true" :model="formInline" size="small" ref="queryFrom">
        <el-col :span="9" style="display: flex; align-items: center;">
          <div id="L-header-list" class="project-info-title" style="margin-top: 20px;margin-right: 10px;">【常用联系人列表】</div>
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="importBtn" style="margin-bottom: 5px;">新增常用联系人</el-button>
        </el-col>
        <el-col :span="15" v-if="roles.includes('gov') || roles.includes('other-gov') ">
          <el-form-item label="联络人姓名">
            <el-input v-model.trim="formInline.contactName" placeholder="请输入联络人姓名"></el-input>
          </el-form-item>
          <el-form-item label="联络人职务">
            <el-input v-model.trim="formInline.contactOffice" placeholder="请输入联络人职务"></el-input>
          </el-form-item>
          <el-form-item class="query-item">
              <el-button size="mini" @click="resetQuery('queryFrom')">重置</el-button>
              <el-button size="mini" type="primary" @click="handleQuery('queryFrom')">查询</el-button>
            </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    
    <section>
      <el-table v-loading="loadingTwo" class="tabTwo-table" border :data="tableTwoData" :row-class-name="tableRowClassName" >  <!-- :max-height="tabHeader" -->
        <el-table-column label="联络人姓名" prop="contactName" />
        <el-table-column label="联络人职务" prop="contactOffice" />
        <el-table-column label="手机号码" prop="contactPhone" />
        <el-table-column label="邮箱" prop="contactEmail" />
        <el-table-column label="操作" prop="userId" class-name="table-operation" width="180px">
          <template slot-scope="scope">
            <span class="look-info" @click="goInfo(scope.row)">查看详情</span>
            <span class="edit-info" @click="editInfo(scope.row)">编辑</span>
            <span class="del-info" @click="delInfo(scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </section>

    <my-dialog :title="title" @close="importClose" @confirm="confirm" ref="importProject" closeText="关闭" :myclass="true">
      <el-form :model="addFrom" size="small" ref="importFrom" label-width="180px" :rules="rules" class="newArticle" :disabled="title == '查看常用联系人' ? true : false">
        <el-row>
          <el-col :span="24">
            <el-form-item label="企业统一社会信用代码：" class="importFormItem">
              <el-input v-model.trim="addFrom.enterpriseCode" placeholder="请输入内容" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="企业名称：" class="importFormItem">
              <el-input v-model.trim="addFrom.enterpriseName" placeholder="请输入内容" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="常用联系人名称：" class="importFormItem" prop="contactName">
              <el-input v-model.trim="addFrom.contactName" placeholder="请输入常用联系人名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="常用联系人公司职务：" class="importFormItem" prop="contactOffice">
              <el-input v-model.trim="addFrom.contactOffice" placeholder="请输入常用联系人公司职务"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="常用联系人手机号码：" class="importFormItem" prop="contactPhone">
              <el-input v-model.trim="addFrom.contactPhone" placeholder="请输入常用联系人手机号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="常用联系人邮箱：" class="importFormItem" prop="contactEmail">
              <el-input v-model.trim="addFrom.contactEmail" placeholder="请输入常用联系人邮箱"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </my-dialog>
  </div>
</template>
<script>
import { getjContacts } from "@/api/jin_ji_hu/enterList"
import myDialog from "@/views/components/myDialog/index.vue"
import { getJEnterpriseContact, jEnterpriseContactNew, jEnterpriseContactChange, jEnterpriseContactId, jEnterpriseContactDel } from "@/api/jin_ji_hu/infoMaintain"
export default {
  components:{ myDialog},
  name:"tabTwo",
  data() {
    //验证邮箱的规则
    var checkEmail = (rule,value,cb) => {
      //验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if(regEmail.test(value)){
        //合法的邮箱
        return cb();
      }
      //邮箱不合法
      cb(new Error('请输入合法的邮箱！'));
    }
    //验证手机号的规则
    var checkMobile = (rule,value,cb) =>{
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if(regMobile.test(value)){
        //合法的手机号
        return cb();
      }
      //手机号不合法
      cb(new Error('请输入合法的手机号！'));
    }
    return {
      tableData:[],
      tableTwoData:[],
      loading:false,
      loadingTwo:false,
      tabHeader: null,
      roles: this.$store.state.user.roles,
      userType: this.$store.state.user.userType,
      formInline:{
        contactName:"",
        contactOffice:""
      },
      addFrom: {
        contactName:"",
        contactOffice:"",
        contactPhone:"",
        contactEmail:"",
        enterpriseCode:"",
        enterpriseName:"",
      },
      rules:{
        contactName:[
          { required: true, message: '请填写常用联系人姓名', trigger: 'blur' }
        ],
        contactOffice:[
          { required: true, message: '请填写常用联系人公司职务', trigger: 'blur' }
        ],
        contactPhone:[
          { required: true, message: '请填写常用联系人手机号码', trigger: 'blur' },
          {validator: checkMobile, trigger: 'blur'}
        ],
        contactEmail:[
          { required: true, message: '请填写常用联系人邮箱', trigger: 'blur' },
          {validator: checkEmail, trigger: 'blur'}
        ],
      },
      title:"新增常用联系人",
      infoVisible:false,
    }
  },
  props:{
    creditCode:{
      type:String,
      default:""
    },
    detailedInfo:{
      type:Object,
      default:()=>{
        return {};
      }
    },
  },
  mounted() {
    this.cancalDebounce();
    window.addEventListener('resize', this.cancalDebounce);
    this.getList()
    this.getTwoList()
  },
  destroyed() {
    window.removeEventListener('resize', this.cancalDebounce);
  },
  methods:{
    // 查询联络人列表
    getList(){
      this.loading = true;
      getjContacts({tyshxydm: this.creditCode || this.$store.state.user.name}).then(res=>{
        this.tableData = res.data
        this.loading = false;
      })
    },
    // 查询常用联系人
    getTwoList(){
      this.loadingTwo = true;
      getJEnterpriseContact({enterpriseCode: this.creditCode || this.$store.state.user.name, ...this.formInline}).then(res=>{
        this.tableTwoData = res.data.records;
        this.loadingTwo = false;
      })
    },
    // 重置
    resetQuery(){
      this.formInline = {
        contactName:"",
        contactOffice:"",
      }
      this.getTwoList();
    },
    // 搜索
    handleQuery(){
      this.getTwoList();
    },
    // 新增联系人
    importBtn(){
      this.title = "新增常用联系人";
      // if(this.userType == "01"){

      // } else {
      //   this.addFrom.enterpriseCode = this.creditCode;
      //   this.addFrom.enterpriseName = this.info.qymc;
      // }
      this.addFrom.enterpriseCode = this.detailedInfo.tyshxydm;
      this.addFrom.enterpriseName = this.detailedInfo.qymc;
      console.log(this.detailedInfo);
      this.$refs.importProject.open();
    },
    // 查看详情
    goInfo(row){
      this.title = "查看常用联系人"
      jEnterpriseContactId(row.id).then((res=>{
        if(res.code == 200) {
          this.addFrom = res.data;
          this.$refs.importProject.open();
        }
      }))
    },
    // 编辑
    editInfo(row){
      this.title = "修改常用联系人"
      // this.addFrom.enterpriseCode = this.name;
      // this.addFrom.enterpriseName = this.nickName;
      jEnterpriseContactId(row.id).then((res)=>{
        if(res.code == 200) {
          this.addFrom = res.data;
          this.$refs.importProject.open();
        }
      })
    },
    // 关闭事件触发
    importClose(){
      this.$nextTick(()=>{
        this.$refs.importFrom.resetFields();
        this.$refs.importProject.close();
        this.addFrom = {
          contactName:"",
          contactOffice:"",
          contactPhone:"",
          contactEmail:"",
          enterpriseCode:"",
          enterpriseName:"",
        }
      })
    },
    // 提交按钮
    confirm(){
      this.$refs.importFrom.validate((valid) => {
        if (valid) {
          // console.log(this.addFrom);
          let id = this.addFrom.id ? this.addFrom.id : null;
          if(id) {
            jEnterpriseContactChange(this.addFrom).then((res)=>{
              if(res.code == 200) {
                this.getTwoList();
                this.importClose();
                this.$message({
                  type: "success",
                  message: "修改成功!",
                });
              }
            })
          } else {
            jEnterpriseContactNew(this.addFrom).then((res)=>{
              if(res.code == 200) {
                this.getTwoList();
                this.importClose();
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    // 删除
    delInfo(row){
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          jEnterpriseContactDel(row.id).then((res) => {
            this.getTwoList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 修改table背景色
    tableRowClassName({row, rowIndex}){
      if (rowIndex % 2 !== 0) {
        return 'evenNumber-row';
      }
      return '';
    },
    // 屏幕尺寸变化
    cancalDebounce(){
      const element = document.getElementById('L-contacts-main'); // 通过元素的 ID 获取元素
      const header = document.getElementById('L-header-list'); // 通过元素的 ID 获取元素
      const elementHeight = element.offsetHeight;
      const headerHeight = header.offsetHeight;
      this.tabHeader = elementHeight - headerHeight;
    }
  },
}
</script>
