<template>
  <div class="">
    <el-drawer class="custom-drawer-form" title="合并申请人/权利人" size="1000px" :visible.sync="drawer" direction="rtl" :before-close="close" destroy-on-close>
      <el-container class="patent-applicant-merge">
        <el-header>
          <el-form :inline="true" class="margin-left_20">
            <el-form-item label="名称">
              <el-input v-model="queryParams.name" size="small" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="国家">
              <el-select v-model="queryParams.country" size="small" placeholder="请选择" clearable filterable>
                <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>
              <el-button type="" size="small" @click="getList">查询</el-button>
              <el-button type="primary" size="small" @click="handleAdd()" :disabled="!$permission('/workspace/folder/merge/applicationMerge/add')">新增</el-button>
            </el-form-item>
          </el-form>
        </el-header>
        <el-main class="container-common-main">
          <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
            <el-table-column type="index" label="#" width="55" align="center"></el-table-column>
            <el-table-column prop="name" label="名称" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="countryName" label="国家" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="addressStr" label="地址" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="createTime" label="更新时间" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{ $d(scope.row.updateTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" align="center" width="150">
              <template slot-scope="scope">
                <el-dropdown split-button type="primary" size="small">
                  <p  @click="handleEdit(scope.row)" v-if="$permission('/workspace/folder/merge/applicationMerge/modify')">编辑</p>
                  <p v-else :disabled="true">编辑</p>
                  <el-dropdown-menu slot="dropdown" class="text-align_center">
                    <el-dropdown-item @click.native="handleSelect(scope.row)" :disabled="!$permission('/workspace/folder/merge/applicationMerge/addApplication')">添加申请人</el-dropdown-item>
                    <el-dropdown-item @click.native="handleExist(scope.row)" :disabled="!$permission('/workspace/folder/merge/applicationMerge/merged')">已合并</el-dropdown-item>
                    <el-dropdown-item divided class="color-red" @click.native="handleDelete(scope.row)" :disabled="!$permission('/workspace/folder/merge/applicationMerge/delete')">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination">
            <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
          </div>
        </el-main>
        <el-footer class="footer-common">
          <el-button @click="close">关 闭</el-button>
        </el-footer>
      </el-container>
    </el-drawer>

    <merge-applicant-exist-dialog ref="mergeApplicantExistDialog" @close="close2" />

    <merge-applicant-select-dialog ref="mergeApplicantSelectDialog" @close="close2" />

    <merge-applicant-form-dialog ref="mergeApplicantFormDialog" @close="close2" />
  </div>
</template>

<script>
import MergeApplicantExistDialog from "../dialog/MergeApplicantExist";
import MergeApplicantFormDialog from "../dialog/MergeApplicantForm";
import MergeApplicantSelectDialog from "../dialog/MergeApplicantSelect";
export default {
  components: {
    MergeApplicantSelectDialog,
    MergeApplicantFormDialog,
    MergeApplicantExistDialog
  },
  data() {
    return {
      drawer: false,
      loading: false,
      total: 0,
      tableData: [],
      projectId: 0,
      commonData: {},
      queryParams: {
        name: '',
        country: '',
        address: '',
        size: 10,
        current: 1,
        projectId: 0,
        order: 'desc',
        prop: 'id',
        from: 'merge'
      },
      addressIds: [],
      areaTree: []
    }
  },
  mounted() {
  },
  methods: {
    open(id) {
      this.projectId = id
      this.queryParams.projectId = id
      this.drawer = true
      this.getList()
      this.getCommonData()
      this.getAreaList()
    },
    close() {
      this.drawer = false
      this.$emit('close')
    },
    getList() {
      this.loading = true
      this.$api.getPatentApplicantList(this.queryParams).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    close2() {
      this.getList()
    },
    getCommonData() {
      this.$api.getCommonData({ keys: 'COUNTRIES' }).then(response => {
        this.commonData = response.data
      })
    },
    getAreaList() {
      this.$api.getArea().then(response => {
        this.areaTree = response.data
      })
    },
    handleAdd() {
      this.$refs.mergeApplicantFormDialog.open({
        projectId: this.queryParams.projectId,
        applicantIds: [],
        name: '',
        remark: '',
        shortName: '',
        country: '',
        address: '',
        addressIds: []
      }, '新增合并申请人', this.commonData, this.areaTree)
    },
    handleSelect(row) {
      this.$refs.mergeApplicantSelectDialog.open(row)
    },
    handleExist(row) {
      this.$refs.mergeApplicantExistDialog.open(row)
    },
    handleEdit(row) {
      this.$refs.mergeApplicantFormDialog.open(row, '编辑合并申请人', this.commonData, this.areaTree)
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.deletePatentApplicant({ id: row.id }).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    },
  }
}
</script>

<style lang="scss">
.patent-applicant-merge {
  .pagination {
    text-align: center;
    margin: 20px 0;
  }
}
</style>
