<template>
  <div class="edit">
    <el-button 
              @click="dialogVisible=true" 
              size="small" 
              type="primary" 
              v-if="position == 'detail'"
              :disabled="!isHandle">编辑</el-button>
    <p  @click="dialogVisible=true" v-else>编辑</p>
    <el-dialog
      title="编辑线索"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form :model="param.content" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm" label-position="left">
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="param.content.name" size="small"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="param.content.phonenumber" size="small"></el-input>
        </el-form-item>
        <el-form-item label="省市县" prop="province">
          <el-cascader
            class="width-240"
            v-model="param.content.province"
            :options="arealist"
            @change="cascaderChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="param.content.address" size="small"></el-input>
        </el-form-item>
        <el-form-item label="来源" prop="cluesource">
          <el-input v-model="param.content.cluesource" size="small"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input type="textarea" v-model="param.content.notes" size="small"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="addOrdercule" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { log } from '@antv/g2plot/lib/utils';
export default {
  name: 'add',
  data() {
    return {
      param: {
        "classname": "saletool.orderclue.web.orderclue",
        "method": "edit",
        "content": {
            "sat_orderclueid": 0,
            "name": "",
            "phonenumber": "",
            "province": "",
            "city": "",
            "county": "",
            "address": "",
            "notes": "",
            "cluesource": ""
        }
      },
      dialogVisible:false,
      rules: {
        phonenumber:[
          {required:true,message:'请输入手机号',trigger:'blur'},
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
        province:[
          {required:true,message:'内容不能为空',trigger:'blur'},
        ],
        cluesource:[
          {required:true,message:'内容不能为空',trigger:'blur'},
          { min: 1, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur' }
        ],
      },
      areaSelectResult:[],
      isHandle:''
    };
  },
  props:{
    updataList: {
      type:Function
    },
    getDetail: {
      type:Function
    },
    position: {
      type:String 
    },
    detailInfo: {
      type: Object
    },
    arealist: {
      type: Array
    }
  },
  computed:{
  },
  watch:{
    detailInfo: {
      handler(val) {
        console.log(1111);
        
        this.param = {
          "classname": "saletool.orderclue.web.orderclue",
          "method": "edit",
          "content": {
            "sat_orderclueid": val.data.sat_orderclueid,
            "name": val.data.name,
            "phonenumber": val.data.phonenumber,
            "province": val.data.city ? [val.data.province,val.data.city,val.data.county] : [val.data.province],
            "city": val.data.province,
            "county": val.data.province,
            "address": val.data.address,
            "notes": val.data.notes,
            "cluesource": val.data.cluesource
          }
        }
        console.log(this.param);
        
        this.areaSelectResult = val.data.city ? [val.data.province,val.data.city,val.data.county] : [val.data.province]
        let is 
        if(JSON.parse(window.sessionStorage.getItem('userInfo')).name == this.detailInfo.data.leadername) {
          is = true
          if(this.detailInfo.data.status == '待跟进' || this.detailInfo.data.status == '跟进中') {
            is = true
          } else {
            is = false
          }
        } else {
          is = false
        }
        this.isHandle = is
      }
    }
  },
  created() {
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    addOrdercule() {
      if(this.areaSelectResult.length > 1) {
        this.param.content.province = this.areaSelectResult[0]
        this.param.content.city = this.areaSelectResult[1]
        this.param.content.county = this.areaSelectResult[2]
      } else {
        this.param.content.province = this.areaSelectResult[0]
      }
      this.$refs.ruleForm.validate( async val => {
        if( !val ) return this.$notify({
          title:'提示',
          message:'请按照要求输入信息',
          type:'warning'
        })
        let res = await this.$api.requested(this.param)
          this.tool.showMessage(res,() => {
            this.$refs.ruleForm.resetFields()
            this.dialogVisible = false  
            this.areaSelectResult = []
            this.$emit('updataList')
            this.$emit('getDetail')
          })
      })
      
    },
    cascaderChange(result) {
      this.areaSelectResult = result
      
    }
  },
};
</script>

<style scoped>
  /deep/.el-dialog__body {
    padding-bottom: 0;
  }
  /deep/.el-dialog__footer {
    display: flex;
    justify-content: space-around;
    padding-top: 0;
  }
  .edit {
    display: inline-block;
  }
  .width-240 {
    width: 100%;
  }
</style>
