<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="800px" :before-close="cancel" :modal="false" :close-on-click-modal="false" :modal-append-to-body="false">
        <el-container style="height:calc(100vh - 250px)">
            <el-header style="display:flex;align-items:center;">
            <!-- 检索 -->
            <div>
                <!-- 后端没有根据检索式检索 -->
                <mySearch style="width: 500px" :SearchFields="searchFiled" @search="search" :searchValue="searchOption">
                </mySearch>
            </div>
            </el-header>
            <el-main>
            <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
                <el-table-column align="center" width="60">
                <template slot-scope="scope">
                    <el-checkbox @change="(val)=>onChange(val,scope.row)" v-if="refresh"
                        :disabled="disabled.findIndex(item=> item.mergeId == scope.row.mergeId) != -1"
                    :checked="mergedList.findIndex(item=> item.name == scope.row.name) !== -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>
            </el-main>
            <el-footer class="footer-common">
                <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>
            </el-footer>
        </el-container>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">关 闭</el-button>
            <el-button type="primary" @click="submit">确认</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    projectId:{

    },
    mergeType:{}
  },
  data() {
    return {
        visible:false,
        title:'',
        mergedList:[],
        disabled:[],
        tableData:[],
        loading:false,
        //检索字段
        searchFiled: [
            {
            label: '名称',
            value: 'name',
            type: 'String',
            }
        ],
        //检索条件
        searchOption: {},
        queryParams: {
            pageNum: 1,
            pageSize: 10,
        },
        // 总条数
        total: 0,
        refresh:true,
    };
  },
  watch: {},
  computed: {},
  created() {},
  mounted() {},
  methods: {
    open(title,checked,disabled){
        this.title=title
        this.mergedList = checked || []
        this.disabled=disabled||[]
        this.searchOption = []
        this.getList()
        this.visible = true
    },
    cancel(){
        this.visible = false
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    //生成字符串
    getString(val){
      var data = []
      if (val.constructor == Object) {
        if (Object.keys(val).length > 0) {
          Object.keys(val).forEach(item => {
            data.push(
              {
                key: item,
                value: val[item]
              }
            )
          })
        } else {
          return ''
        }
      } else if (val.constructor == Array) {
        if (val.length > 0) {
          data = val
        } else {
          return ''
        }
      } else {
        return val
      }
      var str = ''
      data.forEach(item => {
        var value = ''
        if (Array.isArray(item.value)) {
          value = '('
          item.value.forEach((i,index)=>{
            if(/\s/.test(i)){
              value += `"${i}"`
            }else{
              value += i
            }
            if(index!= item.value.length- 1){
              value += ' OR '
            }
          })
          value += ')'
        } else {
          if (item.value) {
            value = `(${item.value})`
          } else {
            return
          }
        }
        var str2 = ''
        if(item.key == 'name'){
          if(this.mergeType == 2){
            str2 = 'MINM=(' + value +')'
          }else{
            str2 = '(MATM=(' + value + ') OR MRHM=(' + value +'))'
          }
        }
        
        if (str) {
          str = str + ' AND ' + str2
        } else {
          str = str + str2
        }

      })
      return str
    },
    //获取检索条件检索
    search(val) {
      let params = {}
      val.forEach(item => {
        if (item.type == 3) {
          params[item.value] = item.searchValue.map(itemValue => {
            return itemValue.value
          })
        } else {
          params[item.value] = item.searchValue.label
        }
      })

      // 返回条件对象
      this.searchOption = this.$commonJS.ArrayToArray(val)
      // 调用查询接口
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 获取发明人或者申请人、权利人数据，包括已合并的数据
    getList() {
      let params = {
        ...this.queryParams,//分页信息
        searchQuery: this.getString(this.searchOption || []),//检索条件
        projectId: this.projectId,//项目id
        type: this.mergeType,
      }
      this.loading = true
      this.$api.getMergePerson(params).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.total = res.data.total
          this.loading  =false
          this.refresh = false
          this.$nextTick(() => {
            this.refresh = true
          })
        }
      }).catch(error => {
        this.tableData = []
        this.total = 0
        this.loading  =false
      })
    },
    onChange(val,row) {
      if (val) {
        this.mergedList.push(row)
      } else {
        var index = this.mergedList.findIndex(item => item.name == row.name)
        if(index!=-1){
         this.mergedList.splice(index, 1) 
        }
      }
    },
    submit(){
        this.$emit('checked',this.mergedList)
        this.cancel()
    },
  },
};
</script>
<style lang="scss" scoped>
</style>