<template>
  <div class="patent-inventor-merge-manage">
    <el-drawer class="custom-drawer-form" title="合并发明人" size="1000px" :visible.sync="drawer" direction="rtl" :before-close="close" destroy-on-close>
      <el-container>
        <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>
              <el-button type="" size="small" @click="getList">查询</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 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.inventorIds.indexOf(scope.row.id) !== -1"></el-checkbox>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="名称" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="address" label="地址" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="remark" 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>
        </el-main>
        <el-footer class="footer-common">
          <el-button @click="close">关 闭</el-button>
          <el-button type="primary" @click="dialogVisible = true">确 定</el-button>
        </el-footer>
      </el-container>
    </el-drawer>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" append-to-body destroy-on-close :before-close="cancel">
      <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="address">
          <el-input v-model="ruleForm.address" placeholder="请输入地址" type="textarea"></el-input>
        </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 slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectAll: false,
      drawer: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      queryParams: {
        name: '',
        size: 10,
        current: 1,
        projectId: 0,
        order: 'desc',
        merge: false,
        ids: ''
      },
      title: '',
      dialogVisible: false,
      ruleForm: {},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' },],
      },
    }
  },
  mounted() {
  },
  methods: {
    open(row, title) {
      this.ruleForm = { ...row }
      this.title = title
      this.drawer = true
      this.selectAll = false
      this.queryParams.projectId = row.projectId
      this.queryParams.current = 1
      this.queryParams.ids = row.inventorIds.toString()
      this.getList()
    },
    close() {
      this.drawer = false
      this.$emit('close')
    },
    onChange2() {
      this.ruleForm.provinceId = this.addressIds[0]
      this.ruleForm.cityId = this.addressIds[1]
      this.ruleForm.areaId = this.addressIds[2]
    },
    onChange({ id }) {
      const index = this.ruleForm.inventorIds.indexOf(id)
      if (index === -1) {
        this.ruleForm.inventorIds.push(id)
      } else {
        this.ruleForm.inventorIds.splice(index, 1)
      }
    },
    onChange3() {
      this.selectAll = !this.selectAll
      if (this.selectAll) {
        let params = { ...this.queryParams }
        params.size = 9999999
        this.loading = true
        this.$api.getPatentInventorList(params).then(response => {
          this.ruleForm.inventorIds = response.data.records.map(item => item.id)
          this.loading = false
        }).catch(error => {
          this.loading = false
        })
      } else {
        this.loading = true
        this.ruleForm.inventorIds = []
        this.$nextTick(() => {
          this.loading = false
        })
      }
    },
    getList() {
      this.loading = true
      this.$api.getPatentInventorList(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();
    },
    cancel() {
      this.dialogVisible = false
    },
    submit() {
      if (this.ruleForm.inventorIds.length === 0) {
        this.$message.error('请选择发明人')
        return false
      }
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          this.ruleForm.merge = true
          if (this.ruleForm.id) {
            this.$api.editPatentInventor(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.cancel()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            this.$api.addPatentInventor(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.cancel()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style lang="scss">
.patent-inventor-merge-manage {
  .pagination {
    text-align: center;
    margin: 20px 0;
  }
  .el-checkbox__label {
    display: none !important;
  }
}
</style>
