<template>
  <div>
    <div class="container normal-panel normal-margin">
      <el-button v-if="tool.checkAuth($route.name,'agent_update')" size="small" icon="el-icon-edit" @click="$router.push({path:'/edit_agent',query:{id:form.sa_agentsid}})">编 辑</el-button>
      <startAgent v-if="tool.checkAuth($route.name,'agent_update')" :data="form" type="default" @onSuccess="query_agentMain()"></startAgent>
      <delAgent v-if="tool.checkAuth($route.name,'agent_delete')" :data="form" :type="'default'" :icon="'el-icon-delete'" class="inline-16"></delAgent>
    </div>
    <div class="container normal-panel normal-margin">      
      <el-descriptions title="经销商信息" :column="4">
        <el-descriptions-item label="经销商编号">{{form.agentnum}}</el-descriptions-item>
        <el-descriptions-item label="省市县">{{form.province}}{{form.city}}{{form.county}}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{form.contact}}</el-descriptions-item>
        <el-descriptions-item label="所属营销区域">{{form.areaname}}</el-descriptions-item>
        <el-descriptions-item label="经销商名称">{{form.agentname}}</el-descriptions-item>
        <el-descriptions-item label="地址">{{form.address}}</el-descriptions-item>
        <el-descriptions-item label="联系人手机号">{{form.phonenumber}}</el-descriptions-item>
        <el-descriptions-item label="合作开始时间">{{form.signdate}}</el-descriptions-item>
        <el-descriptions-item label="经销商类别">{{form.agenttypemx}}</el-descriptions-item>
        <el-descriptions-item label="经销商等级">{{form.grade}}</el-descriptions-item>
        <el-descriptions-item label="上级经销商">{{form.parentagentname}}</el-descriptions-item>
        <!-- <el-descriptions-item label="伙伴类型">{{form.agenttype}}</el-descriptions-item> -->
      </el-descriptions>
    </div>
    <div  class="my-tabs" style="padding:0 16px">
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="经销商团队" name="first"></el-tab-pane>
        <el-tab-pane label="业绩目标" name="second"></el-tab-pane>
        <el-tab-pane label="代理区域" name="third"></el-tab-pane>
      </el-tabs>
    </div>
    <div class="container normal-panel" v-if="activeName === 'first'">
      <!-- <el-button class="normal-margin" type="primary" size="small">创建经销商主账号</el-button> -->
      <tableLayout :layout="tablecols" :data="form.hrs" :custom="false" height="300px"></tableLayout>
    </div>
    <div class="container normal-panel" v-if="activeName === 'third'">
      <salescope></salescope>
    </div>
  </div>
</template>

<script>
import selectAgent from '../../../../components/selectAgent/index.vue'
import arealist from '../../area/list.vue'
import delAgent from './del_agent.vue'
import startAgent from './start_agent.vue'
import salescope from './salescope/list.vue'

export default {
  components:{
    selectAgent,
    arealist,
    delAgent,
    startAgent,
    salescope
  },
  data () {
    return {
      activeName:'first',
      form:{
        "value":[],
        "sa_agentsid": 0,
        "parentid":"",
        "areaname":'',
        "agentnum":"",
        "agentname":"",
        "address":"",
        "agenttype":"",
        "signdate":"",
        "grade":'',
        "sa_saleareaid":'',
        "contact":"",
        "phonenumber":"",
        "province":"",
        "city":"",
        "county":""
      },
      rule:{
        agentnum:[{ required: true, message: '请输入经销商编号', trigger: 'blur' }],
        agentname:[{ required: true, message: '请输入经销商名称', trigger: 'blur' }],
        value:[
          { required: true, message: '请选择省市县', trigger: 'blur' },
          { required: true, message: '请选择省市县', trigger: 'change' }
        ],
        contact:[{ required: true, message: '请输入联系人', trigger: 'blur' }],
        phonenumber:[{ required: true, message: '请输入联系人手机号', trigger: 'blur' }, { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }],
        sa_saleareaname:[{ required: true, message: '请输入营销区域', trigger: 'blur' }],
        signdate:[{required: true, message: '请选择时间', trigger: 'change' }],
        agenttype:[{ required: true, message: '请选择经销商类别', trigger: 'change' }],
        grade:[{ required: true, message: '请选择经销商等级', trigger: 'change' }],
      },
      arealist:[],
      agent_type:[],
      agent_level:[],
      tablecols:[]
    }
  },
  methods:{
    async query_agentMain () {
      const res = await this.$api.requested({
        "classname": "webmanage.sale.agents.agents",
        "method": "query_agentMain",
        "content": {
            "sa_agentsid": this.$route.query.id
        }
      })
      let arr = ['province','city','county']
      res.data.value = []
      arr.forEach(e=>{
        res.data[e] !== '' ?res.data.value.push(res.data[e]):''
      })
      this.form = Object.assign({},this.form,res.data)
      // this.$refs.agent.value.agentname = res.data.parentagentname      
    },
    async query_arealist() {
      const res = await this.$api.requested({
        "classname": "system.tools",
        "method": "query_arealist",
        "content": {
        }
      })
      this.arealist = this.tool.createMenu(res.data)
    },
    cascaderChange (val) {
      if (val.length === 1) 
        return this.form = Object.assign({},this.form,{province:val[0],city:'',county:''})

      this.form = Object.assign({},this.form,{province:val[0],city:val[1],county:val[2]})
      
    },
    async query_sysoptions () {
      const res = await this.$store.dispatch('optiontypeselect','agentgrade')
      this.agent_level = res.data
      const res1 = await this.$store.dispatch('optiontypeselect','agenttype')
      this.agent_type = res1.data
    },
    selectAgent (data) {
      this.form.parentid = data.sa_agentsid
    },
    onClick (data) {
      this.form.sa_saleareaid = data.sa_saleareaid
      this.form.areaname = data.label
    },
    onSubmit () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "classname": "webmanage.sale.agents.agents",
          "method": "insertormodify_agent",
          "content": this.form
        })
        this.tool.showMessage(res)
      });
    }
  },
  created () {
    // 获取表结构
    this.tablecols = this.tool.tabelCol(this.$route.name)['agentGrouptable'].tablecols
  },
  mounted () {
    this.query_arealist()
    this.query_sysoptions()
    this.query_agentMain()
  }
}

</script>
<style>
</style>
<style scoped>
.width-240{
  width: 240px;
}

</style>