<template>
  <div>
    <basicDetails
      ref="details"
      :titleText="`${mainData.accountno}-${mainData.name}`"
      :oldFormPath="{edit:'HManagement/accountManage/modules'}"
      :editData="mainData"
      :mainAreaData="mainAreaData"
      turnPageId="20221031141102"
      delApiId="20221031141202"
      idname="userid"
      ownertable="sys_users"
      tags=""
      :tabs="[this.mainData.usertype === 1 || this.mainData.usertype === 0 ? '人员信息' : '基本信息','团队信息','角色信息']"
      @pageChange="pageChange"
      @onEditSuccess="queryMainData($route.query.id)">
      <div slot="customOperation" >
        <useTemp v-if="tool.checkAuth($route.name,'use')" :data="mainData" @onSuccess="queryMainData"></useTemp>
        <unuseTemp v-if="tool.checkAuth($route.name,'use')" :data="mainData" @onSuccess="queryMainData"></unuseTemp>
      </div>
      <div slot="slot0" >
        <detailInfo :more="true" :data="detailInfo"></detailInfo>
      </div>
      <div slot="slot1" >
        <teamInformation></teamInformation>
      </div>
      <div class="container normal-panel" slot="slot2" >
        <tableLayout :layout="table_roleinfo" :data="roleData" :opwidth="200" :custom="false">
          <!-- <template v-slot:opreation="scope">
            <el-button type="text" size="small" @click="$router.push({path:'/roleDetail',query:{id:scope.data.roleid}})">详 情</el-button>
          </template> -->
        </tableLayout>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import unuseTemp from './unuseAccount.vue'
import useTemp from './useAccdount.vue'

import detailInfo from '@/components/detailInfo/index'
import teamInformation from './teamInformation/index'

export default {
  name: "detail",
  data() {
    return {
      mainData:{},
      mainAreaData:{},
      table_userinfo:[],
      table_hrinfo:[],
      table_roleinfo:[],
      list:[],
      hrmsg:[],
      roleData:[],
      accountInfo:{},
      detailInfo:[]
    }
  },
  components:{
    unuseTemp,
    useTemp,
    detailInfo,
    teamInformation
  },
  methods:{
    async queryMainData(id) {
      const res = await this.$api.requested({
        "classname": "webmanage.users.users",
        "method": "query_userMain",
        "content": {
          "userid": this.$route.query.id
        }
      })
      this.mainData = res.data
      let arr = [{
        accountno:res.data.accountno,
        phonenumber:res.data.phonenumber,
        usertypename:res.data.usertypename,
        status:res.data.status
      }]
      this.list = arr
      // 插入人员数据
      if (typeof(res.data.hrmsg) === 'object'){
        res.data.hrmsg.systemname = res.data.name
        let hrmsg = [res.data.hrmsg]
        this.hrmsg = hrmsg
      }
      // 插入角色数据
      this.roleData = res.data.userrole
      this.changeDataStructure()

      this.getAccountInfo()
    },
    /* 获取企业员工信息 || 合作商/合作商员工信息 */
    async getAccountInfo () {
      let res
      if (this.mainData.usertype === 1 || this.mainData.usertype === 0) {
        res = await this.$api.requested({
          "classname":"webmanage.hr.hr",
          "method": "query_hrMain",
          "content": {
            hrid:this.mainData.hrmsg.hrid
          }
        })
        this.accountInfo = res.data
        console.log('企业员工',this.accountInfo);
        
      } else {
        res = await this.$api.requested({
          "classname":"webmanage.sale.agents.agents",
          "method": "query_agentMain",
          "content": {
            sa_agentsid:this.mainData.agentmsg.sa_agentsid
          }
        })
        this.accountInfo = res.data
        console.log('合作商',this.accountInfo);
      }

      this.detailInfo = this.mainData.usertype === 1 || this.mainData.usertype === 0 
      ? 
        [{
          title:'基本信息',
          info: [
            {label:'登录账号',value:this.accountInfo.accountno},
            {label:'账号名称',value:this.accountInfo.username},
            {label:'账号类型',value:this.accountInfo.usertypename},
            {label:'授权角色',value:this.accountInfo.userrole instanceof Array ? this.accountInfo.userrole.map(item => item.remarks).join(',') : this.accountInfo.userrole},
            {label:'电话',value:this.accountInfo.accountno !== ''?this.accountInfo.userphonenumber:this.accountInfo.phonenumber},
            {label:'办公电话',value:this.accountInfo.officetelephone},
            {label:'部门',value:this.accountInfo.depname},
            {label:'职位',value:this.accountInfo.position},
            {label:'是否部门负责人',value:this.accountInfo.isleader === 1?'是':'否'},
            {label:'电子邮箱',value:this.accountInfo.email},
            {label:'汇报对象',value:this.accountInfo.reporthrname},
            {label:'身份证号',value:this.accountInfo.idcard},
            {label:'性别',value:this.accountInfo.sex},
            {label:'是否服务人员',value:this.accountInfo.isworker === 1?'是':'否'},
          ]
        },{
          title:'系统信息',
          info: [
            {label:'创建人',value:this.accountInfo.createby},
            {label:'创建时间',value:this.accountInfo.createdate},
            {label:'提交人',value:this.accountInfo.submitby},
            {label:'提交时间',value:this.accountInfo.submitdate},
          ]
        }]
      :
        [{
          title:'基本信息',
          info: [
            {label:'经销商编号',value:this.accountInfo.agentnum},
            {label:'erp编号',value:this.accountInfo.erpagentnum},
            {label:'是否服务经销商',value:this.accountInfo.isservice?'是':'否'},
            {label:'状态',value:this.accountInfo.status},
            {label:'签约日期',value:this.accountInfo.signdate},
            {label:'经销商类型',value:this.accountInfo.type},
            {label:'上级经销商',value:this.accountInfo.parent_enterprisename},
            {label:'经销商等级',value:this.accountInfo.grade},
            {label:'省市县',value:`${this.accountInfo.province}-${this.accountInfo.city}-${this.accountInfo.county}`},
            {label:'详细地址',value:this.accountInfo.address},
            {label:'企业联系人',value:this.accountInfo.contact},
            {label:'联系电话',value:this.accountInfo.phonenumber},
            {label:'税号',value:this.accountInfo.taxno},
            {label:'免运费金额',value:this.accountInfo.freefreightamount},
            {label:'订单可退货天数',value:this.accountInfo.limitreturnday},
            {label:'是否订单审核可开票',value:this.accountInfo.invoicingpoint == '1' ? '订单审核' : '发货'},
            {label:'是否授权所有营销类别',value:this.accountInfo.saleclassauth ? '否' : '是'},
            {label:'创建人',value:this.accountInfo.createby},
            {label:'创建时间',value:this.accountInfo.createdate},
          ]
        }]
    },
    changeDataStructure() {
      this.mainAreaData = [
        {
          label:'账号类型',
          value:this.mainData.usertypename
        },
        {
          label:'状态',
          value:this.mainData.status === 'ACTIVE'?'启用':this.mainData.status === 'INACTIVE'?'停用':'其他'
        },
        {
          label:'联系方式',
          value:this.mainData.phonenumber
        }
      ]
      console.log(this.mainAreaData)
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex,tabIndex) {
      this.flag = false
      tabIndex = this.$route.query.tabIndex
      this.$router.replace({path:'/account_details',query:{id:id,rowindex:rowindex,tabIndex:tabIndex}})
      this.queryMainData(id)
    },
    onSuccess(){
      this.visible = false
      this.queryMainData(this.$route.query.id)
      this.$emit('onSuccess')
    }
  },
  mounted () {
    this.queryMainData(this.$route.query.id)
  },
  created() {
    // 获取用户信息表结构
    this.table_userinfo = this.tool.tabelCol(this.$route.name).detailUserinfo.tablecols
    // 获取人员表结构
    this.table_hrinfo = this.tool.tabelCol(this.$route.name).detailHrInfo.tablecols
    // 获取角色表结构
    this.table_roleinfo = this.tool.tabelCol(this.$route.name).detailRoleInfo.tablecols
  }
}
</script>

<style scoped>

</style>