<template>
  <div>
    <el-button size="mini" :type="btnType?btnType:'text'" @click="onShow">编 辑</el-button>
    <el-dialog title="编 辑" :visible.sync="dialogFormVisible">
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="90px" label-position="left" size="mini">
          <el-col :span="24">
            <el-form-item label="企业名称" prop="enterprisename">
              <el-input v-model="form.enterprisename" placeholder="输入企业名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="输入企业联系人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="联系电话" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="输入联系电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="税号" prop="taxno">
              <el-input v-model="form.taxno" placeholder="输入税号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="省市县" prop="value">
              <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="14">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" type="textarea" :rows="3" 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 {
  props:['data','btnType'],
  data () {
    return {
      dialogFormVisible:false,
      form:{
        enterprisename:'',
        value:[],
        sys_enterpriseid:0
      },
      rules:{
        enterprisename: [
          { required: true, message: '输入企业名称', trigger: 'blur' },
        ],
        contact: [
          { required: true, message: '输入企业联系人', trigger: 'blur' },
        ],
        phonenumber: [
          { required: true, message: '输入联系电话', trigger: 'blur' },
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
        taxno: [
          { required: true, message: '输入税号', trigger: 'blur' },
        ],
        value: [
          { required: true, message: '选择省市县', trigger: 'change' },
        ],
        address:[
          { required: true, message: '输入详细地址', trigger: 'blur' },
        ],
      }
    }
  },
  computed:{
    ...mapGetters ({
      arealist:'arealist'
    })
  },
  methods:{
    onShow () {
      this.$store.dispatch('queryArealist')
      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)
      this.dialogFormVisible = true
      
    },
    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]})
      
    },
    onSubmit () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
          const res = await this.$api.requested({
          "id": 20220920084101,
          "content":this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
     })
    }
  },
  mounted () {
  }
}

</script>
<style>
</style>