<template>
  <div class="form-wrap">
    <div class="searchBox">
      <!-- <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <el-form-item
          label="名称"
          prop="otaSourceName">
          <el-input v-model="form.otaSourceName"></el-input>
        </el-form-item>
        <el-form-item
          label="状态"
          prop="keyWords3">
          <el-select
            clearable
            v-model="form.keyWords3">
            <el-option
              value=""
              label="全部"></el-option>
            <el-option
              :value="1"
              label="激活"></el-option>
            <el-option
              :value="0"
              label="禁用"></el-option>
          </el-select>
        </el-form-item>
      </el-form> -->
      <div class="btn-wrap">
        <!-- <el-button @click="reset">重置</el-button> -->
        <el-button
          type="primary"
          @click="getOtaProjectList">搜索</el-button>
        <el-button
          type="primary"
          plain
          @click="showDialog('add')">创建分销商</el-button>
      </div>
    </div>

    <div class="tableBox">
      <div class="block-title">查询信息</div>
      <el-table
        :data="tableData"
        v-loading="loading">
        <el-table-column
          label="#"
          type="index"
          min-width="60">
        </el-table-column>

        <el-table-column
          label="分销商名称"
          prop="otaSourceName">
        </el-table-column>

        <el-table-column
          label="编码"
          prop="otaSourceCode">
        </el-table-column>

        <!-- <el-table-column
          label="退款人工审核开关"
          prop="is_refund_need_audit"
          v-if="canEdit">
          <template slot-scope="scope">
            <el-switch
              v-if="scope.row.otaSourceCode==='CLEARWX'||scope.row.otaSourceCode==='CLEARWXDEV'"
              v-model="scope.row.is_refund_need_audit"
              @change="handleRefundChange($event,scope.row)"
              :active-value="1"
              :inactive-value="0"></el-switch>
          </template>
        </el-table-column> -->

        <!-- <el-table-column
          label="预付开关"
          prop="prePayment"
          v-if="canEdit">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.prePayment"
              @change="handleChange($event,scope.row)"
              :active-value="1"
              :inactive-value="0"></el-switch>
          </template>
        </el-table-column> -->

        <el-table-column label="预付类型">
          <template slot-scope="scope">
            <el-tag :type="scope.row.paymentType===1?'success':'danger'">{{ scope.row.paymentType === 1 ? '分销商平台付款' : '本平台充值余额支付' }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="余额(￥)"
          prop="paymentAmount">
        </el-table-column>

        <!-- <el-table-column
          label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.is_open ? '已激活' : '已禁用' }}</span>
            <span v-if="canEdit">
              <el-button
                type="text"
                @click="offSale(scope.row)"
                class="offSale-btn"
                v-if="scope.row.is_open"><i class="iconfont icon-offSale"></i>禁用</el-button>
              <el-button
                type="text"
                @click="onSale(scope.row)"
                class="onSale-btn"
                v-else><i class="iconfont icon-onSale"></i>激活</el-button>
            </span>
          </template>
        </el-table-column> -->

        <el-table-column
          width="300"
          fixed="right"
          label="操作"
          v-if="canEdit">
          <template slot-scope="scope">
            <!-- <el-button
              type="text"
              @click="showDialog('edit',scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
            <el-button
              type="text"
              @click="deleteOtaProject(scope.row)"><i class="el-icon-delete"></i>删除</el-button> -->
            <el-button
              type="text"
              @click="$refs.editTypeDialog.show(scope.row)"><i class="el-icon-edit"></i>修改预付类型</el-button>
            <el-button
              type="text"
              @click="$refs.editDialog.show(scope.row, 'recharge')"><i class="el-icon-circle-plus-outline"></i>充值</el-button>
            <el-button
              type="text"
              @click="$refs.editDialog.show(scope.row, 'decrease')"><i class="el-icon-remove-outline"></i>扣款</el-button>
            <el-button
              type="text"
              @click="$refs.logDialog.show(scope.row)"><i class="el-icon-message"></i>消费记录</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        :current-page.sync="form.pageNum"
        @current-change="getOtaProjectList"
        layout="total, prev, pager, next"
        :total="total">
      </el-pagination>
    </div>

    <ElDialog
      :title="dialogType === 'edit' ? '编辑分销商' : '创建分销商'"
      width="700px"
      v-model="dialogVisible">
      <NewDialog
        :public-ota-list="publicOtaList"
        :exist-list="tableData"
        :role-list="roleList"
        :ota-item="currentItem"
        :dialog-type="dialogType"
        @updateList="getOtaProjectList"></NewDialog>
    </ElDialog>

    <EditDialog
      ref="editDialog"
      @update="getOtaProjectList"></EditDialog>

    <EditTypeDialog
      ref="editTypeDialog"
      @update="getOtaProjectList"></EditTypeDialog>
    <AmountLogDialog
      ref="logDialog"></AmountLogDialog>
  </div>
</template>

<script>
import { getOtaPublicList, getOtaProjectList, updateOtaProject, deleteOtaProject } from '@/api/ota'
import { getRoleList } from '@/api/systemSetting/role'
import ElDialog from '@/components/Dialog'
import NewDialog from './otaList/NewDialog'
import EditDialog from './otaList/EditDialog'
import EditTypeDialog from './otaList/EditTypeDialog'
import AmountLogDialog from './otaList/AmountLogDialog'

export default {
  components: {
    ElDialog,
    NewDialog,
    EditDialog,
    EditTypeDialog,
    AmountLogDialog
  },
  data () {
    return {
      publicOtaList: [],
      roleList: [],
      currentItem: {},
      form: {
        pageNum: 1,
        pageSize: 10,
        otaSourceName: ''
      },
      loading: false,
      total: 0,
      tableData: [],
      dialogType: '', // 弹框类型 'add' 或 'edit'
      dialogVisible: false,
      logDialogVisible: false
    }
  },
  created () {
    this.getOtaPublicList()
    this.getOtaProjectList()
    // this.getRoleList()
  },
  computed: {
    canEdit () {
      return true
      // return this.$store.state.user.permissionList.includes('product:ota_edit')
    }
  },
  methods: {
    showDialog (type, item) {
      this.dialogVisible = true
      this.dialogType = type
      this.currentItem = item
    },
    // 获取公共分销商列表
    getOtaPublicList () {
      getOtaPublicList({pageNum: 1, pageSize: -1}).then(res => {
        this.publicOtaList = res.data || []
      })
    },
    // 获得角色列表
    getRoleList () {
      getRoleList().then(res => {
        this.roleList = res.extraInfo
      })
    },
    // 获取已绑定的分销商列表
    getOtaProjectList () {
      this.loading = true
      getOtaProjectList(this.form).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.loading = false
      })
    },
    // 删除分销商
    deleteOtaProject (item) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOtaProject(item).then(res => {
          this.$message.success('删除成功')
          this.getOtaProjectList()
        })
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    offSale (data) {
      let form = Object.assign({}, data)
      form.is_open = 0
      updateOtaProject(form).then(res => {
        this.$message.success('操作成功')
        this.getOtaProjectList()
      })
    },
    handleChange (e, data) {
      let form = Object.assign({}, data)
      form.prePayment = e
      updateOtaProject(form).then(res => {
        this.$message.success('操作成功')
        this.getOtaProjectList()
      })
    },
    handleRefundChange (e, data) {
      let form = Object.assign({}, data)
      form.is_refund_need_audit = e
      updateOtaProject(form).then(res => {
        this.$message.success('操作成功')
        this.getOtaProjectList()
      })
    },
    onSale (data) {
      let form = Object.assign({}, data)
      form.is_open = 1
      updateOtaProject(form).then(res => {
        this.$message.success('操作成功')
        this.getOtaProjectList()
      })
    }
  }
}
</script>
