<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="filterStatus" @tab-click="handleFilterStatusClick">
        <el-tab-pane label="全部" name="-1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="待审核" name="0" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核通过" name="1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核拒绝" name="3" :lazy="true"></el-tab-pane>
      </el-tabs>
      <div class="filter-box">
        <filter-user ref="filterUser" :filter-types.sync="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange"
                :height="tool.getTableHeight(true)">
        <el-table-column type="selection" width="55" v-if="tableHeaders"></el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" >{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else-if="scope.row[tableHeader.index].other.progress" >
              <el-progress type="circle" :percentage="Number.parseInt(percentList['MOVE_PERCENT'][scope.row.ID])"
                           :width="50"
                           :stroke-width="3"></el-progress>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180" v-if="tableHeaders">
          <template slot-scope="scope"
                    v-if="scope.row.AUDIT_STATUS==='0' && (permission.hasPermission(`user/move-pass`)||permission.hasPermission(`user/move-audit`)||permission.hasPermission(`user/move-delete`))">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                操作该数据<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="pass" @click.native="handleAuditShow(scope.row)"
                                  v-show="permission.hasPermission(`user/move-pass`)">审核通过
                </el-dropdown-item>
                <el-dropdown-item command="refuse" @click.native="handleAudit(scope.row, 'reject')"
                                  v-show="permission.hasPermission(`user/move-audit`)">审核拒绝
                </el-dropdown-item>
                <el-dropdown-item command="del" @click.native="handleDel(scope.row.ID)"
                                  v-show="permission.hasPermission(`user/move-delete`)">删除数据
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-dropdown size="small" trigger="click" @command="handleMuli" v-if="(filterStatus==='0') && permission.hasPermission(`user/move-delete`)">
          <el-button type="primary" size="small">
            所选数据<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="del" @click.native="handleDel()">批量删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="primary" size="small" @click="handleAdd" icon="el-icon-plus"
                   v-show="permission.hasPermission(`user/move-add`)">新建移网申请
        </el-button>
        <el-button type="success" size="small" @click="handleExport"
                   v-show="permission.hasPermission(`user/move-export`)">导出Excel
        </el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
    <el-dialog title="修改移网信息" :visible.sync="dialogEditFormVisible">
      <el-form :model="form" label-width="250px" class="form-dialog" v-loading="dialogEditLoading">
        <el-form-item label="移网类型">
          <el-select v-model="form.type" placeholder="请选择移网类型">
            <el-option v-for="(item,key) in netType" :label="item.name" :value="item.id" :key="key"></el-option>
          </el-select>
        </el-form-item>
        <div v-if="form.type==='network'">
          <el-form-item label="移动会员编号">
            <el-input v-model="form.baseInfo.USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动会员姓名">
            <el-input v-model="form.baseInfo.REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原上级编号">
            <el-input v-model="form.baseInfo.CON_USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原上级姓名">
            <el-input v-model="form.baseInfo.CON_REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动到节点会员编号">
            <el-input v-model="form.toConUserName"></el-input>
          </el-form-item>
          <el-form-item label="移动到节点会员姓名">
            <el-input v-model="form.toConRealName"></el-input>
          </el-form-item>
        </div>
        <div v-if="form.type==='relation'">
          <el-form-item label="移动会员编号">
            <el-input v-model="form.baseInfo.USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动会员姓名">
            <el-input v-model="form.baseInfo.REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原开拓人编号">
            <el-input v-model="form.baseInfo.REC_USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原开拓人姓名">
            <el-input v-model="form.baseInfo.REC_REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动到开拓会员编号">
            <el-input v-model="form.toConUserName"></el-input>
          </el-form-item>
          <el-form-item label="移动到开拓会员姓名">
            <el-input v-model="form.toConRealName"></el-input>
          </el-form-item>
        </div>
        <el-form-item v-if="form.type==='network'" label="移动到区位">
          <el-select v-model="form.location" placeholder="移动到区位">
            <el-option label="一市场" value="1" key="location_1"></el-option>
            <el-option label="二市场" value="2" key="location_2"></el-option>
            <el-option label="三市场" value="3" key="location_3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="handleEdit">修 改</el-button>
      </div>
    </el-dialog>
    <el-dialog title="审核移网信息" :visible.sync="dialogAuditFormVisible">
      <el-form :model="form" label-width="250px" class="form-dialog" v-loading="dialogAuditLoading">
        <el-form-item label="移网类型">
          <el-select v-model="form.type" placeholder="请选择移网类型">
            <el-option v-for="(item,key) in netType" :label="item.name" :value="item.id" :key="key"></el-option>
          </el-select>
        </el-form-item>
        <div v-if="form.type==='network'">
          <el-form-item label="移动会员编号">
            <el-input v-model="form.baseInfo.USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动会员姓名">
            <el-input v-model="form.baseInfo.REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原上级编号">
            <el-input v-model="form.baseInfo.CON_USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原上级姓名">
            <el-input v-model="form.baseInfo.CON_REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动到节点会员编号">
            <el-input v-model="form.toConUserName"></el-input>
          </el-form-item>
          <el-form-item label="移动到节点会员姓名">
            <el-input v-model="form.toConRealName"></el-input>
          </el-form-item>
        </div>
        <div v-if="form.type==='relation'">
          <el-form-item label="移动会员编号">
            <el-input v-model="form.baseInfo.USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动会员姓名">
            <el-input v-model="form.baseInfo.REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原开拓人编号">
            <el-input v-model="form.baseInfo.REC_USER_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="原开拓人姓名">
            <el-input v-model="form.baseInfo.REC_REAL_NAME" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="移动到开拓会员编号">
            <el-input v-model="form.toConUserName"></el-input>
          </el-form-item>
          <el-form-item label="移动到开拓会员姓名">
            <el-input v-model="form.toConRealName"></el-input>
          </el-form-item>
        </div>
        <el-form-item v-if="form.type==='network'" label="移动到区位">
          <el-select v-model="form.location" placeholder="移动到区位">
            <el-option label="一市场" value="1" key="location_1"></el-option>
            <el-option label="二市场" value="2" key="location_2"></el-option>
            <el-option label="三市场" value="3" key="location_3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAuditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="handleAuditPass">通 过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Vue from 'vue'
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import store from '@/utils/vuexStore'
  import FilterUser from '@/components/FilterUser'
  import permission from '@/utils/permission'
  import Pagination from '@/components/Pagination'
  import filterHelper from '../../utils/filterHelper'

  export default {
    name: 'user_move',
    components: {FilterUser,Pagination},
    mounted() {
      store.state.socket.onMessageCallback = this.onMessageCallback
      network.getData('user/move-net-type').then(response => {
        this.netType = response.netType
        this.getData()
      })
    },
    data() {
      return {
        tableHeaders: null,
        allData: null,
        tableData: null,
        netType: [],
        loading: true,
        multipleSelection: [],
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: {},
        filterModel: {},
        filterStatus: '0',
        dialogEditLoading: false,
        dialogAuditLoading: false,
        dialogEditFormVisible: false,
        dialogAuditFormVisible: false,
        auditId: null,
        form: {
          baseInfo: {USER_NAME: null},
          moveUserName: null,
          toConUserName: null,
          toConRealName: null,
          type: '',
          location: '1',
          remark: null,
          createRemark: '',
          auditStatus: null
        },
        percentList: {
          'MOVE_PERCENT': {},
        },
      }
    },
    methods: {
      handleMuli(command) {
        if (this.multipleSelection.length < 1) {
          this.$message({
            message: '请选择要操作的记录',
            type: 'warning'
          })
          return;
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleAdd() {
        this.$router.push({path: `/user/move-add`})
      },
      handleEditShow(row) {
        this.dialogEditLoading = true
        this.auditId = row.ID
        this.dialogEditFormVisible = true
        let vueObj = this
        network.getData('user/move-get', {id: this.auditId}).then(response => {
          vueObj.dialogEditLoading = false
          vueObj.form = response
          vueObj.form.type = String(response.type)
          vueObj.form.location = String(response.location)
          vueObj.form.createRemark = vueObj.form.remark
        })
      },
      handleEdit() {
        this.dialogEditFormVisible = false
        this.$message({
          message: '正在修改数据',
          type: 'info'
        })
        let path = 'user/move-edit'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {
        })
      },
      handleAuditShow(row) {
        this.dialogAuditLoading = true
        this.auditId = row.ID
        this.dialogAuditFormVisible = true
        let vueObj = this
        network.getData('user/move-get', {id: this.auditId}).then(response => {
          vueObj.dialogAuditLoading = false
          vueObj.form = response
          vueObj.form.type = String(response.type)
          vueObj.form.location = String(response.location) !== '0' ? String(response.location) : '1'
          vueObj.form.createRemark = vueObj.form.remark
        })
      },
      handleAuditPass() {
        this.dialogAuditFormVisible = false
        this.$message({
          message: '正在通过审核',
          type: 'info'
        })
        this.form.auditStatus = 'true'
        let path = 'user/move-pass'
        network.postData(path, this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {
        })
      },
      handleAudit(row = null, status) {
        let obj = this
        let title = '确定要通过审核？备注：'
        if (status === 'reject') {
          title = '确定要拒绝审核？备注：'
        }

        this.$prompt(title, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputValue: '',
          //type: 'warning',
        }).then(({value}) => {
          this.$message({
            message: '正在操作，请稍后',
            type: 'info'
          })
          let selectedIds = []
          if (row === null) {
            for (let val of obj.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(row.ID)
          }
          return network.postData(`user/move-audit`, {
            selected: selectedIds,
            remark: value,
            auditStatus: status
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {

        })
      },
      handleDel(id = null) {
        let obj = this
        this.$confirm('确定删除选定的数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let selectedIds = []
          if (id === null) {
            for (let val of obj.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(id)
          }
          return network.postData(`user/move-delete`, {
            selected: selectedIds
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          obj.getData(obj.currentPage, obj.pageSize)
        }).catch(response => {

        })
      },
      handleFilterStatusClick(tab, event) {
        filterHelper.clearFilterOption(this)
        this.getData()
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize, isLoading = true) {
          let vueObj = this
          let filterData = this.filterModel
          filterData.filterStatus = this.filterStatus != '-1' ? `=,${this.filterStatus}` : ''
          network.getPageData(this, 'user/move', page, pageSize, filterData, response => {
              this.filterTypes = response.filterTypes
              vueObj.allData = response
          }, isLoading, ['MOVE_PERCENT'])
      },
      // getData(page, pageSize) {
      //   let filterData = this.filterModel
      //   filterData.filterStatus = this.filterStatus != '-1' ? `=,${this.filterStatus}` : ''
      //   let vueObj = this
      //   network.getPageData(this, 'user/move', page, pageSize, filterData, function (response) {
      //     vueObj.allData = response
      //     this.filterTypes = response.filterTypes
      //   }, null, ['MOVE_PERCENT'])
      // },
      handleExport() {
        this.$confirm(`确定要导出当前数据吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return network.getData(`user/move-export`, this.filterModel)
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
        }).catch(response => {

        })
      },
      onMessageCallback(data) {
        if (data) {
          if (data.other && data.other.MODEL === 'USER_MOVE' && data.other.ID) {
            this.$set(this.percentList[data.other.FIELD], data.other.ID, data.percent)
          }
          if (data.percent && data.percent === 100) {
            //this.getData(this.currentPage, this.pageSize)
          }
        }
      },
    }
  }

</script>

<style scoped>

</style>
