<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span>商品名称:</span>
        <el-input v-model="listQuery.productName" style="width:130px;" placeholder="商品名称" clearable @keyup.enter.native="getList" @clear="getList" />
      </div>
      <div class="filter-control">
        <span>发起机构:</span>
        <el-input v-model="listQuery.linkMan" style="width:130px;" placeholder="发起机构" clearable @keyup.enter.native="getList" @clear="getList" />
      </div>
      <div class="filter-control">
        <span>需求状态:</span>
        <el-select v-model="listQuery.status" style="width:120px;" clearable @change="getList">
          <el-option value="" label="全部" />
          <el-option label="未完成" :value="0" />
          <el-option label="已完成" :value="1" />
          <el-option label="已删除" :value="3" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>发布日期:</span>
        <el-date-picker
          v-model="registTime"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <el-table-column label="采购商品" width="220" align="center" prop="productName" />
      <el-table-column label="图片" align="center" width="100">
        <template slot-scope="{ row }">
          <el-popover
            placement="top-start"
            title=""
            width="120"
            trigger="hover"
          >
            <img :src="row.productImage" alt="" style="width:100px;height:100px;">
            <img slot="reference" :src="row.productImage" alt="" style="width:50px;height:50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="发起机构" align="center" prop="clubName" />
      <el-table-column label="联系人" width="100" align="center" prop="linkMan" />
      <el-table-column label="手机号" width="140" align="center" prop="mobile" />
      <el-table-column label="参与机构数" width="100" align="center" prop="clubCount" />
      <el-table-column label="需求数量" width="100" align="center" prop="needNum" />
      <el-table-column label="状态" width="150" align="center" prop="status">
        <template slot-scope="{ row }">
          <span
            :class="{
              'el-span-warning': row.status == 1,
              'el-span-success': row.status == 2,
              'el-span-danger': row.status == 3,
            }"
          >
            {{ row.status | statusFilters }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="发布时间" prop="saveTime" width="100" />
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row)">查看详情</el-button>
          <el-button v-if="row.status === 1" type="warning" size="mini" @click="hanleVerify(row)">置为已完成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      v-show="total>10"
      :total="total"
      :page-sizes="[10]"
      :page-size="10"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getList, buyDone } from '@/api/centralized'
import pickerOptions from '@/utils/time-picker.js'
export default {
  filters: {
    statusFilters(value) {
      console.log('value', value)
      // 处理状态显示
      const map = {
        1: '未完成',
        2: '已完成',
        3: '已删除'
      }
      return map[value]
    }
  },
  data() {
    return {
      registTime: '',
      pickerOptions,
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        productName: '',
        status: '',
        clubName: '',
        startSaveTime: '',
        endSaveTime: ''
      }
    }
  },
  computed: {
    disabled() {
      return !this.addPeople.status
    },
    organizeId() {
      return this.$store.getters.organizeId
    }
  },
  created() {
    this.getList()
  },
  methods: {
    initTime() {
      // 初始化获取时间筛选值
      if (this.registTime && this.registTime.length > 0) {
        this.listQuery.startSaveTime = this.registTime[0]
        this.listQuery.endSaveTime = this.registTime[1]
      } else {
        this.listQuery.startSaveTime = ''
        this.listQuery.endSaveTime = ''
      }
    },
    async getList() {
      this.listLoading = true
      try {
        this.initTime()
        const res = await getList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.listLoading = false
      } catch (error) {
        console.log('error', error)
        this.listLoading = false
      }
    },
    // 置为已完成
    hanleVerify(row) {
      this.$confirm('确定将该需求置为已完成吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.buyDone(row.id)
      })
    },
    // 调用
    async buyDone(id) {
      try {
        await buyDone({ id: id })
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 集采详情
    handleDetail(row) {
      this.$router.push({ path: '/centralized/details', query: { id: row.id }})
    },
    handleFilter() {
      this.fetchData()
    }
  }
}

</script>
<style scoped>
  .el-dialog{
    width: 600px;
  }
</style>
