<template>
  <div>
    <el-dialog title="选择申请人/权利人" :visible.sync="visible" width="900px" append-to-body destroy-on-close
      :before-close="close" top="3vh">
      <div class="patent-applicant-merge-select">
        <el-form :inline="true">
          <el-form-item label="当前名称">
            <el-input v-model="queryParams.name" size="small" placeholder="请输入当前名称"></el-input>
          </el-form-item>
          <!-- <el-form-item label="标准名称">
            <el-input v-model="queryParams.shortName" size="small" placeholder="请输入标准名称"></el-input>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" size="small" @click="getList">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header"
          @sort-change="sortChange">
          <el-table-column align="center" width="55">
            <!-- <template slot="header">
              <el-checkbox :checked="selectAll" @change="onChange3"></el-checkbox>
            </template> -->
            <template slot-scope="scope">
              <el-checkbox :label="scope.row.name" @change="onChange(scope.row)" v-if="!loading"
                :checked="ruleForm.mergedName.indexOf(scope.row.name) !== -1"></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column sortable="custom" prop="name" label="当前名称" align="center"
            show-overflow-tooltip></el-table-column>
          <!-- <el-table-column sortable="custom" prop="shortName" label="标准名称" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column sortable="custom" prop="merge" label="是否合并" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.merge">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column> -->
          <el-table-column sortable="custom" prop="countryName" label="国家" align="center" width="150"
            show-overflow-tooltip></el-table-column>
          <el-table-column prop="addressStr" label="地址" align="center" show-overflow-tooltip></el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination :current-page.sync="queryParams.pageNum" :page-size="queryParams.pageSize" :total="total"
            @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
        </div>
      </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 {
      selectAll: false,
      visible: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      queryParams: {
        name: '',
        // shortName: '',
        pageSize: 10,
        pageNum: 1,
        projectId: 0,
        // prop: 'id',
        // order: 'desc',
        // from: 'select',
        // mergeId: 0
      },
    }
  },
  mounted() {
  },
  methods: {
    open(row) {
      
      this.ruleForm = { ...row }
      if (row.id) {
        
      } else {
        this.$set(this.ruleForm, 'mergedName', [])
      }
      this.visible = true
      this.selectAll = false
      this.queryParams.projectId = row.projectId
      this.queryParams.pageNum = 1
      // this.queryParams.mergeId = row.id
      this.getList()
    },
    close() {
      this.visible = false
    },
    sortChange({ column, prop, order }) {
      if (!order) {
        this.queryParams.prop = 'id'
        this.queryParams.order = 'desc'
        this.getList()
        return false
      }
      const o = {
        'descending': 'desc',
        'ascending': 'asc',
      }
      switch (prop) {
        case 'shortName':
          this.queryParams.prop = 'bname'
          break
        case 'countryName':
          this.queryParams.prop = 'country'
          break
        default:
          this.queryParams.prop = prop
      }
      this.queryParams.order = o[order]
      this.getList()
    },
    onChange(row) {
      const index = this.ruleForm.mergedName.indexOf(row.name)
      if (index === -1) {
        this.ruleForm.mergedName.push(row.name)
      } else {
        this.ruleForm.mergedName.splice(index, 1)
      }
    },
    onChange3() {
      this.selectAll = !this.selectAll
      if (this.selectAll) {
        let params = {
          ...this.queryParams,
          type: 0,
        }
        params.pageSize = 9999999
        this.loading = true
        this.$api.getMergePerson(params).then(response => {
          this.ruleForm.mergedName = response.data.data.map(item => item.name)
          this.loading = false
        }).catch(error => {
          this.loading = false
        })
      } else {
        this.loading = true
        this.ruleForm.mergedName = []
        this.$nextTick(() => {
          this.loading = false
        })
      }
    },
    // 获取人员信息
    getList() {
      this.loading = true
      let params = {
        type: 0,
        ...this.queryParams
      }
      this.$api.getMergePerson(params).then(response => {
        this.tableData = response.data.data
        this.total = response.data.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    // 确认按钮
    submit() {
      if (this.ruleForm.mergedName.length === 0) {
        this.$message.error('请选择申请人')
        return false
      }
      this.btnLoading = true
      let params = {
        type: 0,
        ...this.ruleForm,
      }
      if (this.ruleForm.id) {
        this.$api.updateMergePerson(params).then(response => {
          if (response.code == 200) {
            this.$message.success('编辑成功')
            this.btnLoading = false
            this.$emit('selectClose')
            this.close()
          }
        }).catch(error => {
          this.btnLoading = false
        })
      } else {
        this.$api.mergePerson(params).then(response => {
          if (response.code == 200) {
            this.$message.success('新增成功')
            this.btnLoading = false
            this.$emit('selectClose')
            this.close()
          }
        }).catch(error => {
          this.btnLoading = false
        })
      }


      // this.$api.editPatentApplicant(this.ruleForm).then(response => {
      //   this.$message.success('编辑成功')
      //   this.btnLoading = false
      //   this.close()
      // }).catch(error => {
      //   this.btnLoading = false
      // })
    },
  }
}
</script>

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

  .el-checkbox__label {
    display: none !important;
  }
}
</style>
