<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="" 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.id" @change="onChange(scope.row)" v-if="!loading" :checked="ruleForm.applicantIds.indexOf(scope.row.id) !== -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.current" :page-size="queryParams.size" :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: '',
        size: 10,
        current: 1,
        projectId: 0,
        prop: 'id',
        order: 'desc',
        from: 'select',
        mergeId: 0
      },
    }
  },
  mounted() {
  },
  methods: {
    open(row) {
      this.ruleForm = { ...row }
      this.$set(this.ruleForm, 'applicantIds', [])
      this.visible = true
      this.selectAll = false
      this.queryParams.projectId = row.projectId
      this.queryParams.current = 1
      this.queryParams.mergeId = row.id
      this.getList()
    },
    close() {
      this.visible = false
      this.$emit('close')
    },
    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({ id }) {
      const index = this.ruleForm.applicantIds.indexOf(id)
      if (index === -1) {
        this.ruleForm.applicantIds.push(id)
      } else {
        this.ruleForm.applicantIds.splice(index, 1)
      }
    },
    onChange3() {
      this.selectAll = !this.selectAll
      if (this.selectAll) {
        let params = { ...this.queryParams }
        params.size = 9999999
        this.loading = true
        this.$api.getPatentApplicantList(params).then(response => {
          this.ruleForm.applicantIds = response.data.records.map(item => item.id)
          this.loading = false
        }).catch(error => {
          this.loading = false
        })
      } else {
        this.loading = true
        this.ruleForm.applicantIds = []
        this.$nextTick(() => {
          this.loading = false
        })
      }
    },
    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();
    },
    submit() {
      if (this.ruleForm.applicantIds.length === 0) {
        this.$message.error('请选择申请人')
        return false
      }
      this.btnLoading = true
      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>
