<template>
  <div class="importer">
    <div class="search">
      <div class="left" />
      <div class="right">
        <div>
          <el-input v-model="search.entityName" placeholder="进口商ID/进口商名称" clearable />
        </div>
        <div>
          <el-select v-model="search.region" placeholder="请选择所属区域" clearable>
            <el-option value="">全部</el-option>
            <el-option
              v-for="item in constant.district_code"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
        <div>
          <el-select v-model="search.country" placeholder="请选择境外国家" clearable>
            <el-option value="">全部</el-option>
            <el-option
              v-for="item in constant.countryList"
              :key="item.code"
              :label="item.code + ' ' + item.name"
              :value="item.code"
            />
          </el-select>
        </div>
        <!-- <div>
          <el-select v-model="search.status" placeholder="请选择状态" clearable>
            <el-option value="">全部</el-option>
            <el-option
              v-for="item in constant.district_code"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </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="addImporter">添加</el-button>
        </div>
      </div>
    </div>
    <cy-comm-table
      ref="commTable"
      v-loading="loading"
      :columns="columns"
      :data="tableData"
      :count="count"
      parent-component="ImporterManage"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getImporterList } from '@/api/businessManage/tradeRelations'
import { codeChangeName } from '@/utils'
export default {
  name: 'ImporterManage',
  data() {
    return {
      count: 0,
      page: 1,
      rows: 10,
      search: {
        region: '',
        country: '',
        status: '',
        entityName: ''
      },
      addDate: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '进口商ID',
          prop: 'entityNo',
          showTooltip: true
        },
        {
          label: '进口商名称',
          showTooltip: true,
          prop: 'entityName'
        },
        {
          label: '所属区域',
          showTooltip: true,
          prop: 'region',
          render: (h, row) => {
            return <div>{ codeChangeName(this.constant.district_code, row.region, 'dictValue', 'dictLabel') }</div>
          }
        },
        {
          label: '境外国家',
          prop: 'country',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.country ? (row.country + ' ' + codeChangeName(this.constant.countryList, row.country, 'code', 'name')) : '--' }</div>
          }
        },
        {
          label: '状态',
          prop: 'status',
          showTooltip: true,
          render: (h, row) => {
            return <div>{ row.status === 1 ? '生效中' : '' }</div>
          }
        },
        {
          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', ['district_code', 'countryList'])
  },
  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
      }
      getImporterList(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: 'ImporterView',
          params: {
            id: row.id
          },
          query: {
            type: 'view'
          }
        })
      }
    },
    addImporter() {
      this.$router.push({ name: 'ImporterAdd', params: { id: 'child' }, query: { type: 'add' }})
    }
  }
}
</script>

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

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