<template>
  <div class="">
    <el-dialog title="选择专题库" :visible.sync="visible" width="900px" :close-on-click-modal="false" append-to-body destroy-on-close :before-close="close" top="3vh">
      <div class="project-field project-field-copy">
        <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>
            <el-button type="" size="small" @click="handleSearch">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
          <el-table-column label="" align="center" width="55">
            <template slot-scope="scope">
              <el-checkbox :label="scope.row.id" @change="onChange(scope.row)" v-if="!loading" :checked="ids.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="clientName" label="委托方" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="应用场景" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-for="item in scope.row.scenarioName">{{ item }} <br> </span>
            </template>
          </el-table-column>
          <el-table-column label="调查类型" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-for="item in scope.row.typeName">{{ item }} <br> </span>
            </template>
          </el-table-column>
          <el-table-column prop="technicalTheme" label="技术主题" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createName" 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.createTime) }}</span>
            </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>
      </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 {
      queryParams: {
        name: '',
        size: 10,
        current: 1,
        sort: {
          order: 'desc',
          prop: 'create_time'
        },
        myself: false
      },
      btnLoading: false,
      visible: false,
      title: '',
      total: 0,
      loading: false,
      tableData: [],
      ids: [],
      field: {}
    }
  },
  mounted() {
  },
  methods: {
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    cancel() {
    },
    open(row) {
      this.ids = []
      this.visible = true
      this.field = { ...row }
      this.getList()
    },
    close() {
      this.visible = false
      this.$emit('close')
    },
    onChange({ id }) {
      const index = this.ids.indexOf(id)
      if (index === -1) {
        this.ids.push(id)
      } else {
        this.ids.splice(index, 1)
      }
    },
    handleSearch() {
      this.queryParams.current = 1
      this.getList()
    },
    getList() {
      this.loading = true
      this.$api.getProjectListV2(this.queryParams).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    submit() {
      if (this.ids.length === 0) {
        this.$message.error('请选择专题库')
        return false
      }
      let params = {
        id: this.field.id,
        projectId: this.ids.toString()
      }
      this.btnLoading = true
      this.$api.copyProjectField(params).then(response => {
        this.$message.success('操作成功')
        this.close()
        this.btnLoading = false
      }).catch(error => {
        this.btnLoading = false
      })
    },
  }
}
</script>

<style lang="scss">
.project-field-copy {
  .el-checkbox__label {
    display: none !important;
  }
}
</style>
