<template>
  <div class="distribution-user-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="6">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item v-if="false">
            <Area v-model="queryArea" :clearable="true" />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.user_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('昵称查找推广员')"
              remote
              :remote-method="remoteMethod"
              reserve-keyword
            >
              <el-option
                v-for="item in accountOptions"
                :key="item.user_id"
                :label="item.user_nickname"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-select
              v-model.trim="queryForm.user_parent_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('昵称查找推广员粉丝')"
              remote
              :remote-method="remoteMethod"
              reserve-keyword
            >
              <el-option
                v-for="item in accountOptions"
                :key="item.user_id"
                :label="item.user_nickname"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="false">
            <el-select
              v-model="queryForm.user_role_id"
              clearable
              :placeholder="__('请选择')"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" fixed="left" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button
            v-if="false"
            type="info"
            @click="showProxy(row, 'province', 'pa_name')"
          >
            查看用户粉丝
          </el-button>
          <el-button type="text" @click="showTable(row)">
            {{ __('查看订单') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('允许分销')"
        prop="user_active"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.user_active"

            :active-value="1"

            :inactive-value="0"
            @change="sChange($event, scope.row, 'user_active', 'user_active')"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('城市合伙人')"
        prop="user_is_pt"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.user_is_pt"

            :active-value="1"

            :inactive-value="0"
            @change="sChange($event, scope.row, 'user_is_pt', 'user_is_pt')"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        align="center"
        :label="__('省代理')"
        prop="pa_name"
        width="150"
      >
        <template #default="{ row }">
          <el-button type="info" @click="showProxy(row, 'province', 'pa_name')">
            设置省代理
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        align="center"
        :label="__('市代理')"
        prop="ca_name"
        width="150"
      >
        <template #default="{ row }">
          <el-button
            v-if="!row.ca_name"
            type="info"
            @click="showProxy(row, 'city', 'ca_name')"
          >
            {{ __('设置市代理') }}
          </el-button>
          <span v-if="row.ca_name">{{ row.ca_name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        align="center"
        :label="__('区代理')"
        prop="da_name"
        width="150"
      >
        <template #default="{ row }">
          <el-button
            v-if="!row.da_name"
            type="info"
            @click="showProxy(row, 'district', 'da_name')"
          >
            {{ __('设置区代理') }}
          </el-button>
          <span v-if="row.da_name">{{ row.da_name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        align="center"
        :label="__('合伙人充值业绩')"
        prop="commission_partner_deposit_trade"
        show-overflow-tooltip
        width="120"
      >
        <template #default="{ row }">
          <span v-if="row.commission_partner_deposit_trade !== 0">
            <el-button type="text" @click="showTable(row)">
              {{ row.commission_partner_deposit_trade }}
            </el-button>
          </span>
          <span v-if="row.commission_partner_deposit_trade === 0">
            {{ row.commission_partner_deposit_trade }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        align="center"
        :label="__('合伙人总业绩')"
        prop="commission_partner_buy_trade"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('佣金总额')"
        prop="commission_amount"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('店主佣金')"
        prop="commission_distributor_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="caseTypeNamesFormat"
        :label="__('一级消费奖')"
        prop="commission_buy_amount_0"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="caseTypeNamesFormat1"
        :label="__('二级消费奖')"
        prop="commission_buy_amount_1"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="caseTypeNamesFormat2"
        :label="__('三级消费奖')"
        prop="commission_buy_amount_2"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('消费奖市代理')"
        prop="commission_buy_ca"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('消费奖区代理')"
        prop="commission_buy_da"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('一级服务商佣金')"
        prop="commission_directseller_amount_0"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('二级服务商佣金')"
        prop="commission_directseller_amount_1"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('销售奖市代理')"
        prop="commission_directseller_ca"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('销售奖区代理')"
        prop="commission_directseller_da"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="caseTypeNamesFormat3"
        :label="__('已经结算佣金')"
        prop="commission_settled"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('注册时间')"
        prop="user_time"
        show-overflow-tooltip
        width="180"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <Edit ref="edit" @fetch-data="fetchData" />
    <PEdit ref="PEdit" @fetch-data="fetchData" />
    <Table ref="Table" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { enable, getList } from '@/api/distribution/user'
  import Edit from './components/DistributionUserEdit'
  import PEdit from './components/ProxyEdit'
  import Table from './components/DistributionUserTable'
  import Area from '@/components/VabArea/area'
  import { getAccounts } from '@/api/user/base'
  import { timestampToTime } from '@/utils/date'

  export default {
    name: 'DistributionUser',
    components: { Edit, Area, PEdit, Table },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        character: '',
        list: [],
        accountOptions: [],
        loading: true,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          user_id: '', //当前推广员编号
          user_parent_id: '', //推广员父编号
        },
        // 搜索框
        queryArea: {
          province: {},
          city: {},
          district: {},
        },
        options: [
          {
            value: 6,
            label: this.__('服务商'),
          },
          {
            value: 21,
            label: this.__('区代理'),
          },
          {
            value: 20,
            label: this.__('市代理'),
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      showTable(row) {
        this.$refs['Table'].showTable(row)
      },
      formatTime(row) {
        return timestampToTime(row.user_time)
      },
      caseTypeNamesFormat(row) {
        let showProp = null
        row.commission_buy_amount_0
          ? (showProp = row.commission_buy_amount_0)
          : (showProp = '0')
        return showProp
      },
      caseTypeNamesFormat1(row) {
        let showProp = null
        row.commission_buy_amount_1
          ? (showProp = row.commission_buy_amount_1)
          : (showProp = '0')
        return showProp
      },
      caseTypeNamesFormat2(row) {
        let showProp = null
        row.commission_buy_amount_2
          ? (showProp = row.commission_buy_amount_2)
          : (showProp = '0')
        return showProp
      },
      caseTypeNamesFormat3(row) {
        let showProp = null
        row.commission_settled
          ? (showProp = row.commission_settled)
          : (showProp = '0')
        return showProp
      },
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },
      showProxy(row, level, fieldName) {
        this.$refs['PEdit'].showEdit(row, level, fieldName)
      },
      sChange(newVal, row, fieldName, user_active) {
        this.$baseConfirm(
          this.__('请确认是否更改状态？'),
          null,
          async () => {
            row[fieldName] = newVal
            const obj = {}
            obj.user_id = row.user_id
            obj.enable_name = user_active
            obj[fieldName] = row[fieldName]
            await this.enable(obj)
          },
          async () => {
            row[fieldName] = !newVal
          }
        )
      },
      __,
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async enable(obj) {
        const { msg, status } = await enable(obj)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        return status === 200 ? true : false
      },
    },
  }
</script>
