<template>
  <div class="driver-allocated">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="任务类型">
        <el-select style="width: 150px" clearable v-model="searchParam.type" size="small" placeholder="请选择任务类型">
          <el-option v-for="(value,key,index) in typeList" :key="key" :label="value" :value="key"/>
        </el-select>
      </el-form-item>
      <el-form-item label="任务单号">
        <el-input clearable v-model="searchParam.inspectNo" size="small" placeholder="请输入任务单号" style="width: 200px"/>
      </el-form-item>
      <el-form-item label="河道名称">
        <el-input clearable v-model="searchParam.driverName" size="small" placeholder="请输入河道名称" style="width: 200px"/>
      </el-form-item>
      <el-form-item label="巡检时间">
        <el-date-picker
          style="width: 150px"
          placeholder="期望巡检时间"
          clearable
          size="small"
          value-format="yyyy-MM-dd"
          v-model="searchParam.inspectTime">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="区划">
        <driverAreaSelect v-model="searchParam.driverArea"></driverAreaSelect>
      </el-form-item>
<!--      <el-form-item v-if="activeName=='assignedList'" label="状态">-->
<!--        <el-select style="width: 150px" filterable clearable v-model="searchParam.status" size="small"-->
<!--                   placeholder="请选择状态">-->
<!--          <el-option v-for="(value,key,index) in statusList" :key="index" :label="value" :value="key"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button :loading="tableLoading" icon="el-icon-search" @click="search" size="small" type="primary">搜索
        </el-button>
        <el-button icon="el-icon-refresh-right" @click="initSearchParam();search()"
                   type="primary" size="small"
                   style="background-color: #fff; border: 1px solid #ccc ; color: #666">清空
        </el-button>
        <el-button @click="distribution" v-if="activeName=='index'&&(isAdmin||btnRule.some(btn=>btn=='assign'))"
                   :disabled="selectList.length==0?true:false" icon="el-icon-s-order" type="success" size="small">
          分配
        </el-button>
      </el-form-item>
    </el-form>
    <el-table @selection-change="handleSelectionChange" stripe ref="table" size="medium"
              :height="tableHeight" v-loading="tableLoading" :data="dataList" border
              row-key="id">
      <el-table-column v-if="activeName=='index'"  :reserve-selection="true" type="selection" width="55"></el-table-column>
      <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center"/>
      <el-table-column min-width="130" show-overflow-tooltip prop="inspectNo" label="任务单号" align="center">
        <template slot-scope="scoped">
          <a v-if="isAdmin||btnRule.some(btn=>btn=='view')" href="javascript:void(0);" @click="detailNo(scoped.row)">{{scoped.row.inspectNo}}</a>
          <span v-else>{{scoped.row.inspectNo}}</span>
        </template>
      </el-table-column>
      <el-table-column width="70" show-overflow-tooltip prop="num" label="行号" align="center"/>
      <el-table-column width="100" show-overflow-tooltip prop="typeText" label="任务分类" align="center"/>
      <el-table-column min-width="130" show-overflow-tooltip prop="driverName" label="河湖名称" align="center"/>
      <el-table-column min-width="130" show-overflow-tooltip prop="driverAreaText" label="区划" align="center"/>
      <el-table-column v-if="activeName=='index'" show-overflow-tooltip prop="formatInspectTime" label="巡检时间" min-width="160" align="center">
        <template slot-scope="scoped">
          <span v-if="scoped.row.inspectTime">{{scoped.row.formatInspectTime}}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="activeName=='index'" width="80" show-overflow-tooltip prop="formatCreateUser" label="申请人" align="center"/>
      <el-table-column v-if="activeName=='index'" width="160" show-overflow-tooltip prop="formatCreateTime" label="申请时间" align="center"/>
      <el-table-column v-if="activeName=='assignedList'" min-width="120" show-overflow-tooltip prop="equipmentName"
                       label="飞行器设备" align="center"/>
      <el-table-column v-if="activeName=='assignedList'" min-width="100" show-overflow-tooltip prop="mountequipmentName"
                       label="挂载设备" align="center"/>
      <el-table-column v-if="activeName=='assignedList'" min-width="80" show-overflow-tooltip prop="inspectHandName"
                       label="飞手" align="center"/>
      <el-table-column v-if="activeName=='assignedList'&&(isAdmin||btnRule.some(btn=>btn=='cancel'))" label="操作"
                       align="left" width="60">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==2" @click="cancelAss(scope.row)" type="text">取消</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-current">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        prev-text="上一页"
        next-text="下一页"
        background
        layout="total, prev, pager, next, jumper"
        style="display: inline-block;"
        :total="count"
        @current-change="handleCurrentChange"
      />
    </div>
    <allotTasks :equipmentTypeList="equipmentTypeList" @okSubmit="okSubmit" :inspectDrivers="selectList" @close="allotTasksClose" v-if="distributionVisible" :visible="distributionVisible"></allotTasks>
    <el-drawer
      title="任务详情"
      :visible.sync="reportVisible"
      :direction="direction"
      :size="drawerWidth"
      :before-close="handleReportClose">
      <report @goBack="reportVisible = false" v-if="reportVisible" :reportData="reportData"></report>
    </el-drawer>
  </div>
</template>

<script>
  import inspectApi from '@/api/business/inspect'
  import allotTasks from './allotTasks'
  import report from '../report'
  import driverAreaSelect from '@/components/driverAreaSelect'

  export default {
    name: "allocated",
    data() {
      return {
        drawerWidth: (document.body.clientWidth - 190) + 'px',
        direction: 'rtl',
        currentPage: 1,
        count: 0,
        pageSize: parseInt((document.body.clientHeight - 340 - 43) / 43),
        distributionForm: {
          adminId: ''
        },
        searchParam: {
          type: '',
          inspectNo: '',
          driverName: '',
          driverArea: '',
          inspectTime: '',
          status: ''
        },
        dataList: [],
        selectList: [],
        tableLoading: false,
        distributionVisible: false,
        addBtnLoading: false,
        tableHeight: document.body.clientHeight - 340,
        btnRule: [],
        isAdmin: false,
        reportData: {},
        reportVisible: false
      }
    },
    props: {
      typeList: {
        type: Object,
        default() {
          return {};
        }
      },
      activeName: {
        type: String,
        default() {
          return 'index';
        }
      },
      statusList: {
        type: Object,
        default() {
          return {};
        }
      },
      equipmentTypeList:{
        type: Object,
        default() {
          return {};
        }
      },
      adminList: {
        type: Array,
        default() {
          return [];
        }
      }
    },
    created() {
      const {btnRule, isAdmin} = this.$store.state.user
      this.btnRule = btnRule
      this.isAdmin = isAdmin
    },
    components: {report,allotTasks,driverAreaSelect},
    mounted() {
      this.tableLoading = true
      this.getAllList()
    },
    methods: {
      handleReportClose(hide){
        if(hide){
          hide()
        }
        this.reportVisible=false
      },
      allotTasksClose(){
        this.distributionVisible=false
      },
      okSubmit(){
        this.distributionVisible=false
        this.tableLoading = true
        this.selectList = []
        this.$refs.table.clearSelection()
        this.getAllList()
      },
      indexMethod(index) {
        return (index + 1) + ((this.currentPage - 1) * this.pageSize);
      },
      cancelAss(item) {
        this.$confirm('是否需要取消该任务分配！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableLoading = true
          inspectApi.assignCancel({id: item.id}).then(res => {
            this.$message({
              type: 'success',
              message: '取消该任务成功！'
            })
            this.getAllList()
          }).catch(e => {
            this.tableLoading = false
          })
        }).catch(() => {
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.addBtnLoading = true
            let ids = []
            this.selectList.map(item => {
              ids.push(item.id)
            })
            inspectApi.assignTask({ids: ids.join(','), adminId: this.distributionForm.adminId}).then(res => {
              this.addBtnLoading = false
              this.distributionForm.adminId = ''
              this.$refs[formName].resetFields()
              this.distributionVisible = false
              this.tableLoading = true
              this.$message({
                type: 'success',
                message: '分配任务成功！'
              })
              this.getAllList()
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      resetForm(formName) {
        this.distributionForm = {
          adminId: ''
        }
        this.$refs[formName].resetFields()
      },
      handleSelectionChange(val) {
        this.selectList = val
      },
      detailNo(item) {
        this.tableLoading = true
        inspectApi.getDetail({id: item.id}).then(res => {
          this.reportData = res.data
          this.reportVisible = true
          this.tableLoading = false
        }).catch(e => {
          this.reportData = {}
          this.tableLoading = false
        })
      },
      distribution() {
        let driverArea = this.selectList[0].driverArea
        let filter = this.selectList.filter(item => item.driverArea != driverArea)
        if (filter.length > 0) {
          this.$confirm('选中河湖区划不一致，是否继续分配', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.distributionVisible = true
          })
        }else{
          this.distributionVisible = true
        }
      },
      initSearchParam() {
        this.searchParam = {
          type: '',
          inspectNo: '',
          driverName: '',
          driverArea: '',
          inspectTime: '',
          status: ''
        }
      },
      search() {
        this.currentPage = 1
        this.tableLoading = true
        this.selectList = []
        this.$refs.table.clearSelection()
        this.getAllList()
      },
      handleCurrentChange(val) {
        this.tableLoading = true
        this.currentPage = val
        this.getAllList()
      },
      getAllList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        if (searchParam.inspectTime) {
          searchParam.inspectTime = new Date(searchParam.inspectTime).getTime()/1000
        }
        searchParam.page = this.currentPage
        searchParam.pageSize = this.pageSize
        let method = 'getAssignedList'
        if (this.activeName == 'index') {
          method = 'getList'
        }
        this.$refs.table.bodyWrapper.scrollTop = 0;
        inspectApi[method](searchParam).then(res => {
          this.dataList = res.data.records ? res.data.records : []
          this.count = res.data.total
          this.tableLoading = false
        }).catch(e => {
          this.dataList = []
          this.tableLoading = false
        })
      }
    }
  }
</script>

<style lang="less">
  .driver-allocated {
    .report-dialog {
      .el-dialog {
        width: 80% !important;
      }
    }

  }
</style>
