<template>
  <div>
    <el-button size="mini" :type="btnType?btnType:'text'"  type="text" @click="show">编 辑</el-button>
    <el-dialog title="新 增" :visible.sync="dialogFormVisible" width="900px">
      <div slot="title" style="font-size: 15px">
        新增客户
      </div>
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="120px" label-position="left" size="mini">
          <el-col :span="20">
            <el-form-item label="客户名称" prop="enterprisename">
              <el-input v-model="form.enterprisename" placeholder="请输入客户名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="客户类型" >
              <el-select v-model="form.type" placeholder="请选择客户类型">
                <el-option
                    v-for="item in options.type"
                    :key="item.value"
                    :label="item.value"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="客户来源" >
              <el-input v-model="form.source" placeholder="请输入客户来源"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="上级客户" >
                <el-select v-model="form.parentid" placeholder="请选择上级客户">
                  <el-option
                      v-for="item in options.parentCustomers"
                      :key="item.sa_customersid"
                      :label="item.enterprisename"
                      :value="item.sa_customersid">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="15">
            <el-form-item label="省市县" >
              <el-cascader
                  style="width:100%"
                  v-model="form.value"
                  :options="basicData.data().areaData"
                  @change="cascaderChange" clearable>
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="企业地址" >
              <el-input v-model="form.address" placeholder="请输入企业简称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="企业简称" >
              <el-input v-model="form.abbreviation" placeholder="请输入企业简称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="企业税号" >
              <el-input v-model="form.taxno" placeholder="请输入企业税号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="客户等级" >
              <el-select v-model="form.grade" placeholder="请选择客户等级">
                <el-option
                    v-for="item in options.gradeData"
                    :key="item.rowindex"
                    :label="item.value"
                    :value="item.rowindex">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="所属行业" >
              <el-select v-model="form.industry" placeholder="请选择所属行业">
                <el-option
                    v-for="item in options.industryData"
                    :key="item.value"
                    :label="item.value"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="联系方式" >
              <el-input v-model="form.phonenumber" placeholder="请输入联系方式"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
export default {
  name: "edit",
  props:['data','btnType'],
  data(){
    return {
      dialogFormVisible:false,
      form:{
        value:[],
        sa_customersid:0,
        sys_enterpriseid:0,
        enterprisename:'',
        type:'',
        source:'',
        parentid:'',
        abbreviation:'',
        taxno:'',
        grade:'',
        industry:'',
        phonenumber:'',
        province:'',
        city:'',
        county:'',
        address:'',
      },
      value:'',
      options:{
        type:[],
        parentCustomers:[],
        gradeData:[],
        industryData:[]
      },
      rules:{
        enterprisename: [
          { required: true, message: '输入客户', trigger: 'blur' },
        ],
      }
    }
  },
  computed:{
    ...mapGetters ({
      arealist:'arealist'
    })
  },
  methods:{
    show(){
      this.dialogFormVisible = true
      this.typeList()
      this.parentCustomer()
      this.gradeList()
      this.industryList()
      this.$store.dispatch('queryArealist')
      console.log("输出")
      console.log(this.data)
      let arr = ['province','city','county']
      arr.forEach(e=>{
        this.data[e] !== '' ?this.form.value.push(this.data[e]):''
      })
      this.form = Object.assign({},this.form,this.data)
      console.log("------------------")
      console.log(this.form)
      if (this.form.parentid === 0){
        this.form.parentid = '未选择'
      }
    },
    onSubmit(){
      if (this.form.parentid === '未选择'){
        this.form.parentid = 0
      }
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": 20221012163902,
          "content": this.form
        })
        this.tool.showMessage(res, ()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
      })

    },
    /*客户类型列表*/
    async typeList(){
      const res = await this.$api.requested({
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "typename": "customertypemx",
          "parameter": {
            "siteid": "YOSTEST2"
          }
        }
      })
      this.options.type = res.data
    },

    /*上级客户*/
    async parentCustomer(){
      const res = await this.$api.requested({
        "id": 20221014101002,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": ""
          }
        }
      })
      console.log(res)
      this.options.parentCustomers = 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 gradeList(){
      const res = await this.$api.requested({
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "typename": "customergrade",
          "parameter": {
            "siteid": "YOSTEST2"
          }
        }
      })
      this.options.gradeData = res.data
    },
    /*行业列表*/
    async industryList(){
      const res = await this.$api.requested({
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "typename": "industry",
          "parameter": {
            "siteid": "YOSTEST2"
          }
        }
      })
      this.options.industryData = res.data
    }
  }
}
</script>

<style scoped>

</style>