<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-button type="primary" icon="el-icon-sold-out" @click="renwuFp">
            批量分配
          </el-button>
        </div>
      </sticky>
      <el-form>
        <el-form-item>
          <el-input v-model="listQuery.serialNumber" placeholder="流水号" clearable style="width: 160px" />
          <el-input v-model="listQuery.shiyongdanwei" placeholder="使用单位名称" clearable style="width: 400px" @keyup.enter.native="handleFilter" />
          <el-select v-model="listQuery.shebeizhongleidaima" placeholder="请选择设备种类" clearable>
            <el-option v-for="item in sbzlList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="list"
      v-loading="showLoading"
      element-loading-text="正在加载"
      element-loading-spinner="el-icon-loading"
      :data="list"
      border
      fit
      highlight-current-row
      stripe
      width="100%"
      height="650px"
      @row-click="onRowClick"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column align="center" label="序号" width="60" fixed="left">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="流水号" prop="serialNumber" width="140" />
      <el-table-column align="center" label="设备种类" prop="shebeizhonglei" width="100" />
      <el-table-column align="center" label="检验类别" prop="jianyanleibie" width="100" />
      <el-table-column align="center" label="使用单位" prop="shiyongdanwei" width="240" />
      <el-table-column align="center" label="产品名称/设备名称" prop="shebeimingcheng" width="180" />
      <el-table-column align="center" label="产品编号/出厂编号" prop="chanpinbianhao" width="220" />
      <el-table-column align="center" label="联系人" prop="shiyongdanweiLianxiren" width="140" />
      <el-table-column align="center" label="联系电话" prop="shiyongdanweiDianhua" width="140" />
      <el-table-column align="center" label="区划名称" prop="quhuamingcheng" width="140" />
      <!--<el-table-column align="center" label="交费状态" prop="jfState" min-width="40">-->
      <!--<template slot-scope="scope">-->
      <!--<el-tag type="success" v-if="scope.row.jfstate===true">已交费</el-tag>-->
      <!--<el-tag type="primary" v-else>未交费</el-tag>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column fixed="right" align="center" label="报检日期" prop="shouliriqi" width="100" />
      <el-table-column fixed="right" align="center" label="备注" prop="beizhu" width="80" />
      <el-table-column fixed="right" align="center" label="操作（已/总）" width="140">
        <template slot-scope="scope">
          <el-button style="margin-left:1px" size="medium" round>
            <span style="color: #67C23A;font-weight: bold">{{ scope.row.allotNum }}</span> / <span style="color: #F56C6C;font-weight: bold">{{ scope.row.totalNum }}</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      :total="totalCount"
      :page-sizes="[10, 20, 40, 100]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog :visible.sync="dialogVisible" title="任务分配" width="1500px" @close="closeDialog">
      <el-container>
        <el-header>
          <el-select v-model="departmentId" placeholder="请选择科室" style="width: 240px;" @change="selectDepUser">
            <el-option v-for="dept in depts" :key="dept.id" :label="dept.name" :value="dept.id" />
          </el-select>
          <el-select v-model="renlingren" :collapse-tags="true" placeholder="请选择人员" multiple style="width: 240px;">
            <el-option v-for="user in users" :key="user.id" :label="user.nickname" :value="user.id" />
          </el-select>
          <el-button type="primary" @disabled="btnClick" @click="allotRenwu">
            执行分配
          </el-button>
        </el-header>
      </el-container>
      <el-table ref="allotList" :data="allotList" size="small" style="width: 150%" border fit highlight-current-row @row-click="onDialogRowClick" @selection-change="handleDialogSelectionChange">
        <el-table-column type="selection" width="45" />
        <el-table-column align="center" label="序号" width="50" fixed="left">
          <template slot-scope="scope">
            <span v-text="getIndex(scope.$index)" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="检验类别" prop="jianyanleibie" width="100px;" />
        <el-table-column align="center" label="使用单位" prop="shiyongdanwei" width="240px;" />
        <el-table-column align="center" label="产品名称/设备名称" prop="shebeimingcheng" width="180px;" />
        <el-table-column align="center" label="产品编号/出厂编号" prop="chanpinbianhao" width="220" />
        <el-table-column align="center" label="注册代码" prop="zhucedaima" width="220px;" />
        <el-table-column align="center" label="使用登记编号" prop="shiyongdengjibianhao" width="220px;" />
        <el-table-column align="center" label="报检日期" prop="shouliriqi" width="100px;" />
        <el-table-column align="center" label="下次检验日期" prop="xiacijianyanriqi" width="100px;" />
        <el-table-column v-if="allotSbzl === '8000'" align="center" label="管道长度(m)/数量(条)" prop="guandaochangdu" width="100px;" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import Sticky from '@/components/Sticky'
import Utils from '@/utils/contact'
export default {
  name: 'AllotOldList',
  components: { Sticky },
  data() {
    return {
      dialogVisible: false,
      totalCount: 0,
      list: [],
      sbzlList: [],
      listQuery: {
        pageNum: 1, // 页码
        pageRow: 20, // 每页条数
        shiyongdanwei: undefined,
        shebeizhongleidaima: '',
        order: 'modifyDate desc'
      },
      multipleSelection: [],
      allotList: [],
      depts: [],
      users: [],
      departmentId: this.$store.getters.departmentId,
      renlingren: [],
      selection: [],
      btnClick: false,
      showLoading: true,
      allotSbzl: ''
    }
  },
  created() {
    this.getList()
    this.getDepartment()
    this.getSbzlList()
    this.getUserList(this.departmentId)
  },
  mounted() {
    const that = this
    this.common.$on('allot-old-list', function() {
      that.handleFilter()
    })
  },
  methods: {
    getList() {
      this.showLoading = true
      this.apibjd({
        url: '/renwu/listold',
        method: 'get',
        params: this.listQuery
      }).then(data => {
        this.list = data.list
        this.totalCount = data.totalCount
        this.showLoading = false
      })
    },
    getSbzlList() {
      this.api({
        url: '/sedirectory/getList',
        method: 'get',
        params: {
          sbzl: undefined,
          level: '1'
        }
      }).then(data => {
        this.sbzlList = data
      })
    },
    handleSizeChange(val) {
      // 改变每页数量
      this.listQuery.pageRow = val
      this.handleFilter()
    },
    handleCurrentChange(val) {
      // 改变页码
      this.listQuery.pageNum = val
      this.getList()
    },
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    getIndex($index) {
      // 表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
    },
    // 批量分配
    renwuFp() {
      if (this.multipleSelection.length === 0) {
        this.$message({ message: '请在列表选择要操作的数据。', type: 'warning' })
        return false
      }
      let bjdIds = ''
      for (let i = 0; i < this.multipleSelection.length; i++) {
        if (i === 0) {
          bjdIds = this.multipleSelection[i].bjdId
        } else {
          bjdIds = bjdIds + ',' + this.multipleSelection[i].bjdId
        }
      }
      this.apibjd({
        url: '/renwu/getAllotBaseListOld',
        method: 'post',
        data: {
          bjdIds: bjdIds
        }
      }).then(data => {
        this.allotList = data.list
        this.allotSbzl = this.multipleSelection[0].shebeizhongleidaima
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.allotList.toggleAllSelection()
          this.selection = data.list
        })
      })
    },
    // 表格单击选中行
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    // 获取选中行的bjdId
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    onDialogRowClick(row) {
      this.$refs.allotList.toggleRowSelection(row)
    },
    handleDialogSelectionChange(val) {
      this.selection = val
    },
    // 查询检验科室
    getDepartment() {
      this.api({
        url: '/department/getAllDepartment',
        method: 'get'
      }).then(data => {
        this.depts = data
      })
    },
    selectDepUser(val) {
      this.renlingren = []
      this.getUserList(val)
    },
    getUserList(val) {
      this.api({
        url: '/user/getUserListByDepartmentId',
        method: 'get',
        params: {
          departmentId: val
        }
      }).then(data => {
        this.users = data
      })
    },
    closeDialog() {
      this.renlingren = []
      this.departmentId = this.$store.getters.departmentId
      this.users = this.getUserList(this.departmentId)
    },
    allotRenwu() {
      this.btnClick = true
      if (this.selection.length === 0) {
        this.$message({ message: '请选择要分配的数据。', type: 'warning' })
        return false
      }
      if (this.renlingren.length > 0) {
        this.apibjd({
          url: '/renwu/completeAllotOld',
          method: 'post',
          data: {
            renwuList: this.selection,
            renlingren: this.renlingren
          }
        }).then(() => {
          this.multipleSelection = []
          // 刷新分配任务列表
          this.getList()
          // 刷新我的任务列表
          Utils.$emit('task-list')
          this.$message({ message: '分配成功。', type: 'success' })
          this.dialogVisible = false
          this.showLoading = true
        })
      } else {
        this.$message({ message: '请选择要分配的人员。', type: 'warning' })
        return false
      }
    }
  }
}
</script>
