<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-import">
        <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="projectName" label="所属专题库" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="类型" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ typeObj[scope.row.type] }}</span>
            </template>
          </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-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>
      </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 {
      projectId: 0,
      queryParams: {
        name: '',
        type: null,
        status: null,
        size: 10,
        current: 1,
        order: 'desc'
      },
      typeObj: {
        0: '数字',
        1: '日期',
        2: '文本',
        // 3: '下拉框',
        4: '单选',
        5: '多选',
        6: '树',
      },
      pTypeObj: {
        1: '标引',
        2: '分类'
      },
      statusObj: {
        0: '关闭',
        1: '正常',
      },
      btnLoading: false,
      visible: false,
      title: '',
      total: 0,
      loading: false,
      tableData: [],
      ids: []
    }
  },
  mounted() {
  },
  methods: {
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    cancel() {
    },
    open(id) {
      this.ids = []
      this.visible = true
      this.projectId = id
      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.getProjectFieldList(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.ids.toString(),
        projectId: this.projectId
      }
      this.btnLoading = true
      this.$api.importProjectField(params).then(response => {
        this.$message.success('操作成功')
        this.close()
        this.btnLoading = false
      }).catch(error => {
        this.btnLoading = false
      })
    },
  }
}
</script>

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