<template>
  <div>
    <div class="container normal-panel normal-margin">
      <el-button type="warning" size="small" icon="el-icon-s-claim" @click="onSubmit">保 存</el-button>
    </div>
    <div class="container normal-panel normal-margin">
      <p class="normal-title normal-margin">经销商信息</p>
      <el-row :gutter="20">
        <el-form :model="form" :rules="rule" ref="form" size="small" status-icon label-position="left" label-width="120px" class="demo-ruleForm">
          <el-col :span="8">
            <el-form-item label="经销商编号" prop="agentnum">
              <el-input class="width-240" type="text" v-model="form.agentnum" placeholder="请输入" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="经销商名称" prop="agentname">
              <el-input class="width-240" type="text" v-model="form.agentname" placeholder="请输入" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="省市县" prop="value">
              <el-cascader
                class="width-240"
                v-model="form.value"
                :options="arealist"
                @change="cascaderChange">
              </el-cascader>
            </el-form-item>
            <el-form-item label="地址" prop="address">
              <el-input type="textarea" class="width-240" v-model="form.address"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contact">
              <el-input class="width-240" type="text" v-model="form.contact" placeholder="请输入" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系人手机号" prop="phonenumber">
              <el-input class="width-240" type="number" v-model="form.phonenumber" placeholder="请输入" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="所属营销区域">
              <el-input class="width-240" readonly v-model.number="form.sa_saleareaname"></el-input>
            </el-form-item>
            <el-form-item label="合作开始时间" prop="signdate">
              <el-date-picker
                class="width-240"
                v-model="form.signdate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经销商类别" prop="agenttypemx">
              <el-select class="width-240" v-model="form.agenttypemx" placeholder="请选择">
                <el-option
                  v-for="item in agent_type"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="伙伴类型" prop="partnertype">
              <el-input class="width-240" type="text" readonly v-model="form.agenttype" placeholder="伙伴类型" autocomplete="off"></el-input>
            </el-form-item> -->
            <el-form-item label="上级经销商" prop="parentid">
              <selectAgent class="width-240" @selectAgent="selectAgent"></selectAgent>
            </el-form-item>
            <el-form-item label="经销商等级" prop="grade">
              <el-select class="width-240" v-model="form.grade" placeholder="请选择">
                <el-option
                  v-for="item in agent_level"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
  </div>
</template>

<script>
import selectAgent from '../../../../components/selectAgent/index.vue'
export default {
  components:{
    selectAgent
  },
  data () {
    return {
      
      form:{
        "value":[],
        "sa_agentsid": 0,
        "parentid":"",
        "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' }],
        agenttypemx:[
          { 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:[]
    }
  },
  methods:{
    initializeData () {
      this.form.sa_saleareaid = this.$route.query.id
      this.form.sa_saleareaname = this.$route.query.area
    },
    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','agenttypemx')
      this.agent_type = res1.data
    },
    selectAgent (data) {
      this.form.parentid = data.sa_agentsid
    },
    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)
        res.code === 1?this.createAgentMainUser(res.data.sa_agentsid):''
      });
    },
    async createAgentMainUser (sa_agentsid) {
      const res = await this.$api.requested({
        "classname": "webmanage.sale.agents.agents",
        "method": "createAgentMainUser",
        "content": {
            "sa_agentsid":sa_agentsid
        }
      })
      res.code === 1?this.$router.replace({path:'/agent_detail',query:{id:sa_agentsid}}):''
    }
  },
  mounted () {
    this.initializeData()
    this.query_arealist()
    this.query_sysoptions()
  }
}

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