<template>
  <div class="supplier">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.entityNo" placeholder="供应商ID" clearable />
        </div>
        <div>
          <el-input v-model="search.entityName" placeholder="供应商名称" clearable />
        </div>
        <div>
          <el-date-picker
            v-model="addDate"
            type="daterange"
            range-separator="至"
            start-placeholder="添加日期范围"
            end-placeholder="添加日期范围"
          />
        </div>
        <div class="right-btn">
          <el-button type="primary" @click="fetchData('search')">查询</el-button>
          <el-button type="primary" @click="addSupplier">添加</el-button>
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="SupplierManage"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getSupplierList } from '@/api/businessManage/tradeRelations'
export default {
  name: 'SupplierManage',
  data() {
    return {
      page: 1,
      rows: 10,
      count: 0,
      search: {
        entityNo: '',
        entityName: '',
        fastSearch: ''
      },
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '供应商ID',
          prop: 'entityNo',
          showTooltip: true
        },
        {
          label: '供应商名称',
          showTooltip: true,
          prop: 'entityName'
        },
        {
          label: '联系人',
          prop: 'contactName'
        },
        {
          label: '联系邮箱',
          prop: 'contactEmail',
          showTooltip: true
        },
        {
          label: '添加时间',
          prop: 'gmtCreate',
          showTooltip: true
        },
        {
          label: '更新时间',
          prop: 'gmtModified'
        },
        {
          label: '操作',
          render: (h, row) => {
            const btnList = [
              // {
              //   msg: '编辑',
              //   icon: 'iconfont icon-a-Group8151',
              //   rowPower: ['', null],
              //   power: 'projectNumber:delete',
              //   category: 'edit'
              // },
              {
                msg: '详情',
                icon: 'iconfont icon-a-Group957',
                power: '',
                // rowPower: ['', null],
                category: 'see'
              }
            ]
            const options = btnList.map(item => {
              return (
                this.tablePower({ item, row }) && <el-tooltip class='item' effect='dark' content={item.msg} placement='top'>
                  <span class='table-icon-box'>
                    <i class={item.icon} onClick={() => { this.handlerOperate(item.category, row) }}></i>
                  </span>
                </el-tooltip>
              )
            })
            return <div>{ options }</div>
          },
          width: 120
        }
      ],
      loading: false
    }
  },
  computed: {
    ...mapGetters(['constant']),
    tablePower() {
      return ({ item, row }) => {
        const { rowPower } = item
        if (rowPower) {
          if (rowPower.includes(row.stage)) {
            return true
          }
        } else {
          return true
        }
      }
    }
  },
  created() {
    // this.$store.dispatch('getConstant', ['projectInitiationStatus'])
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      this.loading = true
      if (type === 'search') {
        this.page = 1
        this.rows = 10
      }
      const params = {
        ...this.search,
        createdTimeStart: this.addDate ? this.addDate[0] : '',
        createdTimeEnd: this.addDate ? this.addDate[1] : '',
        page: this.page || 1,
        rows: this.rows || 10
      }
      getSupplierList(params).then(({ data }) => {
        this.loading = false
        this.tableData = data.list
        this.count = Number(data.total)
      })
    },
    handlerOperate(type, row) {
      if (type === 'see') {
        this.$router.push({
          name: 'SupplierView',
          params: {
            id: row.id
          },
          query: {
            type: 'view'
          }
        })
      }
    },
    addSupplier() {
      this.$router.push({ name: 'SupplierAdd', params: { id: 'child' }, query: { type: 'add' }})
    }
  }
}
</script>

  <style lang="scss" scoped>
      .supplier {

      }
  </style>
  <style lang="scss">
      .supplier {
          .icon-a-Group957, .icon-a-Group1087 {
              font-size: 28px;
          }
      }
  </style>
