<template>
  <div>
    <el-button size="small" type="primary" @click="onshow" >新增企业地址</el-button>
    <el-drawer
        title=" 新建企业地址"
        :visible.sync="dialogFormVisible"
        size="600px"
        direction="rtl"
        :show-close="false"
        append-to-body
        @close="onClose">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules"  ref="form"  size="mini" label-position="right" label-width="75px">
            <el-col :span="24">
              <el-form-item label="省市县:" prop="value">
                <el-cascader
                    style="width:100%"
                    v-model="value"
                    :options="basicData.data().areaData"
                    @change="cascaderChange" clearable>
                </el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="详细地址:" prop="address">
                <el-input type="textarea" rows="2" v-model="form.address" placeholder="请输入家庭住址"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="地址类型:" prop="typeData">
                <el-select
                    ref="sle"
                    style="width:100%;"
                    v-model="typeData"
                    multiple
                    placeholder="请选择标签"
                    size="mini"
                >
                  <el-option
                      v-for="item in addressList"
                      :key="item.index"
                      :label="item.tag"
                      :value="item.tag"
                  >
                    <span style="float: left">{{ item.tag }}</span>
                    <span style="float: right; color: #8492a6; font-size: 12px">{{ item.helptips?item.helptips:'暂无描述' }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="默认地址:" prop="isdefault">
                <el-radio v-model="form.isdefault" label="1">是</el-radio>
                <el-radio v-model="form.isdefault" label="0">否</el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="备注:" prop="remarks">
                <el-input type="textarea"  rows="5" v-model="form.remarks" placeholder="请输入备注"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="onClose" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width" :disabled="form.province === '' && form.address === ''">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "add",
  props:["data"],
  data() {
    return {
      dialogFormVisible:false,
      form:{
        contactsid: 0,
        sys_enterpriseid: "",
        name: "",
        sex: "",
        depname: "",
        position: "",
        birthday: "",
        phonenumber: "",
        email: "",
        province: "",
        city: "",
        county: "",
        address: "",
        remarks: "",
        workaddress: 1,
        isdefault: '1',
        isprimary: 1
      },
      rules:{
        address: [
          { required: false, message: '地址不能为空', trigger: 'blur' },
        ],
      },
      value:[],
      addressList:[],
      typeData:[]
    }
  },
  methods:{
    onshow(){
      if (this.data.enterprisename === '' || this.data.abbreviation === '' || this.data.contact === ''){
        this.$message.error('请先将企业基础信息补充完整，再进行此操作');
      }else {
        this.form.sys_enterpriseid = this.data.sys_enterpriseid
        this.dialogFormVisible = true
        this.addressType()
      }
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221018141802",
          "content": this.form
        })
        this.tool.showMessage(res,() => {
          this.$refs['form'].resetFields()
          this.value = ''
          this.dialogFormVisible = false
          if (this.typeData !== ''){
            this.setTag(res.data.contactsid)
          }else {
            this.$emit("insertSuccess")
          }

        })
      })
    },
    async setTag(id){
      const res = await this.$api.requested({
        "id": 20220929090901,
        "content": {
          "ownertable":"sys_enterprise_contacts",
          "ownerid":id,
          "datatag":this.typeData
        }
      })
      this.$emit("insertSuccess")
      this.typeData = []
    },
    onClose(){
      this.$refs['form'].resetFields()
      this.value = ''
      this.dialogFormVisible = false
    },
    /*省市县*/
    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 addressType(){
      let param = {
        content: {
          isExport: 0,
          pageNumber: 1,
          pageSize: 20,
          sys_systemtagid: 13
        },
        id: 20221013104401
      };
      const res = await this.$api.requested(param)
      this.addressList = res.data
    }
  }
}
</script>

<style scoped>

</style>