<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加审核人
      </el-button>
    </div>
    <el-table
      size="mini"
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="deleteUser(row)" type="danger" size="mini">
            删除
          </el-button>
          <el-button type="green" size="mini" @click="addReviewDept(row)">增加审核部门</el-button>
        </template>
      </el-table-column>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80">
        <template slot-scope="scope">
          <span>{{ (listData.page - 1) * listData.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="200"
        label="审核人"
        prop="userName"
      >
      </el-table-column>
      <el-table-column
        width="200"
        prop="deptName"
        label="部门名称">
      </el-table-column>
      <el-table-column width="400" label="审核部门">
        <template slot-scope="{row}">
            <el-cascader
              style="width: 360px"
              :disabled="!row.isEdit"
              v-model="row.reviewDeptList"
              :show-all-levels="false"
              :options="deptOptions"
              :clearabl="true"
              :props="{value:'id',label:'name',multiple:true}"
              filterable
              placeholder="请选择">
            </el-cascader>
          <el-button v-if="row.isEdit" icon="el-icon-plus" type="success" size="small" @click="addDeptSure(row)"></el-button>
          <el-button v-if="row.isEdit" icon="el-icon-close" type="danger" size="small" @click="tableRefresh"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px">
        <user :is-choose="true" :type="1"  @chooseUser='chooseUser' :table-size="'mini'"></user>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import { getReviewerList, addReviewer, addSurveyReviewDept } from '@/api/complaint/reviewerList'
import { getDeptTreeData } from '@/api/system/user.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import User from '@/views/system/user/user'

const form = {
  userName: '',
  id: ''
}
export default {
  name: 'ReviewerList',
  components: {
    User,
    Cancledialog
  },
  data() {
    return {
      chooseData:[],
      detailsType: 0,
      complaintDialog: false,
      innerVisible: false,
      dialogVisible: false,
      form: Object.assign({}, form),

      search: {
        keyWord: ''
      },
      tableData: [],
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      confirmdialog: false,
      listLoading: false,
      dialogTitle: '',
      complaintTitleList: [],
      relationChecked: false,
      orderList: {
        listData: {
          total: 0,
          page: 1,
          pageSize: 10
        },
        search: {
          orderName: '',
          orderCode: '',
          clientName: '',
          beginTime: '',
          endTime: ''
        },
        orderData: [],
        listLoading: false
      },
      searchTime: [],
      deptOptions: []
    }
  },
  mounted() {
    this.getInfo()
    this.getDeptTree()
  },
  methods: {
    // 获取列表
    getInfo() {
      const _that = this
      _that.listLoading = true

      var params = _that.search
      var data = {
        'params': JSON.stringify(params)
      }

      getReviewerList(data).then(response => {
        console.log(response)
        const data = response.data
        data.forEach(item => {
          item.isEdit = false
          if (item.reviewDeptList.length > 0) {
            var result = []
            item.reviewDeptList.forEach(dept => {
              if (dept.parentId == 0) {
                var deptArr = []
                deptArr.push(dept.id)
                result.push(deptArr)
              } else {
                const deptArr = []
                deptArr.push(dept.parentId)
                deptArr.push(dept.id)
                result.push(deptArr)
              }
              item.reviewDeptList = result
            })
          }

        })
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    // 获取部门树
    getDeptTree() {
      var _that = this
      var data = {}
      getDeptTreeData(data).then(res => {
        console.log(res)

        const deptData = _that.getDeptsData(res.data)
        _that.deptOptions = deptData
      })
    },
    getDeptsData(data) {
      var deptData = data
      console.log(deptData)
      deptData.forEach((item, index) => {
        const parentId = item.pId
        if (parentId === 0) {
          // 是根元素的hua ,不做任何操作,如果是正常的for-i循环,可以直接continue.
        } else {
          // 如果ele是子元素的话 ,把ele扔到他的父亲的child数组中.
          deptData.forEach(d => {
            if (d.id === parentId) {
              let childArray = d.children
              if (!childArray) {
                childArray = []
              }
              childArray.push(item)
              d.children = childArray
              deptData.splice(index, 1)
            }
          })
        }
      })
      return deptData
    },
    // 增加审核人
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)
      _that.dialogTitle = '增加审核人'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
      _that.relationChecked = false
    },
    // 刷新表格
    tableRefresh() {
      this.getInfo()
    },
    chooseUser(row) {
      console.log(row)
      var _that = this
      var userList = []
      row.forEach(item => {
        var form = Object.assign({}, form)
        form.id = item.id
        form.userName = item.userName
        userList.push(form)
      })
      _that.addBtn(userList)
    },
    // 增加审核人
    addBtn(userList) {
      const _that = this
      var data = {
        'userList': JSON.stringify(userList)
      }
      // console.log(data);
      // return

      addReviewer(data).then(res => {
        console.log('addReviewer', res)
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    deleteUser(row) {
      var _that = this
      var printerTypeInfo = {
        id: row.id
      }
      var array = []
      array.push(printerTypeInfo)
      var params = {
        'complaintReviewerList': JSON.stringify(array)
      }

      var url = '/complaint/removeReviewer'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 增加审核部门
    addReviewDept(row) {
      row.isEdit = true
    },
    selectCase(val) {
      console.log(val);
    },
    addDeptSure(row) {
      const _that = this
      var reviewDeptList = row.reviewDeptList
      var result = []
      reviewDeptList.forEach(item => {
        if (item.length > 1) {
          result.push({ id: item[1] })
        } else {
          result.push({ id: item[0] })
        }
      })
      // console.log(result);
      var user = {
        id: row.id,
        userName: row.userName
      }
      var data = {
        'complaintReviewer': JSON.stringify(user),
        'deptList': JSON.stringify(result)
      }
      // console.log(data);
      // return

      addSurveyReviewDept(data).then(res => {
        console.log('addReviewer', res)
        if (res.httpCode == 200) {
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getInfo()
    }
  }
}
</script>
<style>

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
input[type='number'] {
  -moz-appearance: textfield;
}
</style>
<style lang="scss" scoped>

.textRed {
  color: red;
}
</style>
