<template>
  <div class="app-container">

    <div style="margin-bottom: 20px">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        添加黑名单
      </el-button>
      <el-button
        type="primary"
        icon="el-icon-download"
        @click="download"
      >下载模板</el-button>
      <el-input
        v-model="form.taskName"
        placeholder="任务名称"
        class="margin-left input"
      />
      <el-select
        v-model="form.purchaserId"
        class="margin-left input"
        placeholder="请选择购买方"
        clearable
        filterable
      >
        <el-option
          v-for="(item, index) in buyList"
          :key="index"
          :label="item.companyName"
          :value="item.purchaserId"
        />
      </el-select>
      <el-date-picker
        v-model="form.startTimeToEndTime"
        type="datetimerange"
        range-separator="至"
        class="margin-left"
        start-placeholder="选择开始时间"
        end-placeholder="选择结束时间"
      />
      <el-button
        class="margin-left"
        type="primary"
        icon="el-icon-search"
        @click="query(1)"
      >
        搜索
      </el-button>
    </div>
    <!-- 主体表格 -->
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" width="80" label="序号">
        <template slot-scope="scope">
          {{
            (pagination.page - 1) * pagination.pageSize + (scope.$index + 1)
          }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="taskName" label="任务名称" />
      <el-table-column align="center" prop="documentType" label="人员名单">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            size="small"
            @click="getDetail(row)"
          >查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="purchaserName" label="购买方">
        <template v-slot="{ row }">
          {{ row.purchaserName | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="	创建时间">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作区" width="180">
        <template v-slot="{ row }">
          <el-button
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
      <!--分页栏-->
    </el-table>
    <pagination
      :total="pagination.total"
      :page.sync="pagination.page"
      :limit.sync="pagination.pageSize"
      :page-sizes="pagination.pageSizeOptions"
      :layout="pagination.layout"
      @pagination="handleSizeChange"
    />
    <!-- 人员名单 -->
    <PersonnelModal ref="personnelInfoModel" :roster="rosterList" />
    <UploadModal
      ref="uploadModal"
      :buy-list="buyList"
      @finish="modalSuccess(false)"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { deleteById, getList } from '@/api/blacklist'
import PersonnelModal from './components/PersonnelModal.vue'
import UploadModal from './components/UploadModal.vue'
import { purchaserList } from '@/api/configuration'
import tableMixins from '@/mixins/tableMixins'
import { parseTime } from '@/utils'
export default {
  components: { Pagination, PersonnelModal, UploadModal },
  filters: {
    matchNull(str) {
      if (!str) {
        return '-'
      } else {
        return str
      }
    }
  },
  mixins: [tableMixins],
  data() {
    return {
      rosterList: [],
      buyList: [], // 购买方公司
      form: {
        taskName: null, // 任务名称
        purchaserId: null,
        startTime: null,
        endTime: null,
        startTimeToEndTime: null
      },
      pagination: {
        page: 1,
        current: 1
      }

    }
  },
  created() {
    this.purchaserList()
  },
  methods: {
    initData() {
      this.getTableList()
    },
    // 购买方列表
    purchaserList() {
      purchaserList().then((res) => {
        this.buyList = res.data
      })
    },
    // 获取表格数据
    async getTableList() {
      this.pagination.current = this.pagination.page
      this.tableLoading = true
      try {
        const result = await getList(this.getFilterParams())
        this.setDataList(result.data)
      } catch (e) {
        console.log(e)
      } finally {
        this.tableLoading = false
      }
    },
    // 获取筛选信息
    query(type) {
      if (type) {
        this.pagination.page = 1
        this.pagination.current = 1
      }
      this.form.startTime = null
      this.form.endTime = null
      if (this.form.startTimeToEndTime && this.form.startTimeToEndTime.length > 0) {
        this.form.startTime = parseTime(this.form.startTimeToEndTime[0], '{y}-{m}-{d} {h}:{i}:{s}')
        this.form.endTime = parseTime(this.form.startTimeToEndTime[1], '{y}-{m}-{d} {h}:{i}:{s}')
      }
      this.clearPageParams()
      this.getTableList()
    },
    // 增加筛选条件
    transformFilterForm() {
      let startTime = null
      let endTime = null
      if (this.form.startTimeToEndTime && this.form.startTimeToEndTime.length > 0) {
        startTime = parseTime(this.form.startTimeToEndTime[0], '{y}-{m}-{d} {h}:{i}:{s}')
        endTime = parseTime(this.form.startTimeToEndTime[1], '{y}-{m}-{d} {h}:{i}:{s}')
      }
      return {
        purchaserId: this.form.purchaserId || null,
        taskName: this.form.taskName || null,
        startTime: startTime,
        endTime: endTime
      }
    },
    handleDelete(row) {
      const that = this
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showClose: false,
        type: 'warning',
        beforeClose: async(action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.cancelButtonLoading = true
            instance.confirmButtonText = '提交中...'
            try {
              await deleteById(row.id)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              that.initData()
            } catch (e) {
              // this.$message({
              //   type: 'error',
              //   message: '删除失败!'
              // })
            } finally {
              done()
              setTimeout(() => {
                instance.confirmButtonLoading = false
                instance.cancelButtonLoading = false
              }, 300)
            }
          } else {
            done()
          }
        }
      })
        .then(() => {})
        .catch(() => {})
    },
    getDetail(row) {
      console.log(row)
      this.rosterList = row?.userBlacklist || []
      this.$refs.personnelInfoModel.open()
    },
    // 下载模板
    download() {
      window.location.href =
        process.env.VUE_APP_BASE_API + '/static/blacklistExcel.xlsx'
    },
    modalSuccess(isEdit) {
      if (!isEdit) {
        this.clearPageParams()
      }
      this.getTableList()
    },
    handleCreate() {
      this.$refs.uploadModal.open()
    }
  }
}
</script>

<style scoped>
.table{
  margin-top: 15px;
}
.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.search-layout {
  margin-bottom: 20px;
}
.search-layout .filter-item {
  margin-right: 20px;
  margin-top: 10px;
}
.search-layout .filter-item:last-of-type {
  margin-right: 0;
}
.head {
  margin-left: 16px;
  font-weight: 600;
  font-size: 18px;
  color: #333333;
}
.input {
  width: 200px;
  /*margin-right: 15px;*/
}
.m-r {
  margin-right: 15px;
}
</style>
