<template>
  <div class="inline-16">
    <el-button size="small" type="text" @click="onshow" :disabled="!tool.checkAuth($route.name,'customerAddressManage') || !disable">编 辑</el-button>
    <el-drawer
        title="编辑客户地址"
        :visible.sync="dialogFormVisible"
        size="600px"
        direction="rtl"
        :show-close="false"
        append-to-body
        @close="dialogFormVisible = false">
      <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="省市县:" >
                <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="24">
              <el-form-item  label="详细地址:" >
                <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="地址类型:" >
                <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="默认地址:" >
                <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="备注:" >
                <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="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width">保 存</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "edit",
  props:["data","disable"],
  data(){
    return {
      dialogFormVisible:false,
      form:{
        value:[],
        contactsid: "",
        sys_enterpriseid: "",
        name: "",
        sex: "",
        depname: "",
        position: "",
        birthday: "",
        phonenumber: "",
        email: "",
        province: "",
        city: "",
        county: "",
        address: "",
        remarks: "",
        workaddress: 1,
        isdefault: '',
        isprimary: 1,
        ownerid: this.$route.query.id,
        ownertable: "sa_customers"
      },
      rules:{
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' },
        ],
      },
      value:'',
      addressList:[],
      typeData:[]
    }
  },
  methods:{
    onshow(){
      console.log(this.data,"编辑数据")
      this.dialogFormVisible = true
      // this.form.sys_enterpriseid = this.data.sys_enterpriseid
      // this.form.contactsid = this.data.contactsid
      // this.form.remarks = this.data.remarks
      // this.form.address = this.data.address
      // this.form.province = this.data.province
      // this.form.city = this.data.city
      // this.county = this.data.county
      // this.form.value[0] = this.data.province
      // this.form.value[1] = this.data.city
      // this.form.value[2] = this.data.county
      this.form = Object.assign({},this.form,this.data)
      if (this.form.isdefault === 0){
        this.form.isdefault = '0'
      }else {
        this.form.isdefault = '1'
      }
      this.form.value = [this.form.province,this.form.city,this.form.county]
      this.typeData = this.form.tag
      console.log(this.form)
      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.dialogFormVisible = false
          if (this.typeData !== ''){
            this.setTag(this.form.contactsid)
          }else {
            this.$emit("updateSuccess")
          }
        })
      })
    },
    /*省市县*/
    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 setTag(id){
      const res = await this.$api.requested({
        "id": 20220929090901,
        "content": {
          "ownertable":"sys_enterprise_contacts",
          "ownerid":id,
          "datatag":this.typeData
        }
      })
      this.$emit("updateSuccess")
      console.log(res)
    },
    /*地址类型*/
    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
      console.log(res,"地址类型")
    }
  }
}
</script>

<style scoped>

</style>