<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body destroy-on-close :before-close="close" top="10vh">
      <div class="patent-applicant-merge-form">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="55px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="简称" prop="shortName">
            <el-input v-model="ruleForm.shortName" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="国家" prop="country">
            <el-select v-model="ruleForm.country" placeholder="请选择" filterable class="width_100">
              <el-option v-for="(item, index) in commonData.COUNTRIES" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="地址" prop="addressStr">
            <el-input v-model="ruleForm.addressStr" placeholder="请输入地址"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="addressIds">
            <el-cascader @change="onChange" v-model="addressIds" :options="areaOptions" :props="props" class="width_100"></el-cascader>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">关 闭</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      commonData: {},
      addressIds: [],
      areaOptions: [],
      visible: false,
      loading: false,
      title: '',
      btnLoading: false,
      props: {
        value: 'id',
        label: 'name'
      },
      ruleForm: {},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' },],
      },
    }
  },
  mounted() {
  },
  methods: {
    open(form, title, commonData, areaOptions) {
      this.ruleForm = { ...form }
      this.commonData = commonData
      this.areaOptions = areaOptions
      this.visible = true
      this.title = title
    },
    close() {
      this.visible = false
      this.$emit('close')
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          this.ruleForm.merge = true
          if (this.ruleForm.id) {
            this.$api.editPatentApplicant(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            this.$api.addPatentApplicant(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    onChange() {
      this.ruleForm.provinceId = this.addressIds[0]
      this.ruleForm.cityId = this.addressIds[1]
      this.ruleForm.areaId = this.addressIds[2]
    },
  }
}
</script>

<style lang="scss">
.patent-applicant-merge-form {

}
</style>
