<template>
  <div class="driver-container">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="河道信息" name="1">
        <el-form :inline="true" :model="searchParam" class="demo-form-inline">
          <el-form-item label="区划">
            <driverAreaSelect v-model="searchParam.driverArea"></driverAreaSelect>
          </el-form-item>
          <el-form-item label="河湖名称">
            <el-input clearable v-model="searchParam.name" size="small" placeholder="请输入河道名称"
                      style="width: 200px"/>
          </el-form-item>
          <el-form-item label="河湖类型">
            <el-select clearable v-model="searchParam.type" size="small" placeholder="-请选择-"
                       style="width: 150px">
              <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-select clearable v-model="searchParam.level" size="small" placeholder="-请选择-"
                       style="width: 150px">
              <el-option v-for="(value,key,index) in levelList" :key="key" :label="value" :value="key"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :loading="tableLoading" @click="search" size="small" icon="el-icon-search" type="primary">搜索
            </el-button>
            <el-button @click="initSearchParam();search()" icon="el-icon-refresh-right"
                       type="primary" size="small"
                       style="background-color: #fff; border: 1px solid #ccc ; color: #666">清空
            </el-button>
          </el-form-item>
        </el-form>
        <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading"
                  :data="driverList"
                  size="medium"
                  border>
          <el-table-column :index="indexMethod" label="序号" type="index" width="80" align="center"/>
          <el-table-column show-overflow-tooltip prop="driverSn" label="河湖编码" align="center" width="100"/>
          <el-table-column show-overflow-tooltip prop="name" label="河湖名称" align="center" min-width="120">
            <template slot-scope="scoped">
              <a v-if="isAdmin||btnRule.some(btn=>btn=='view')" href="javascript:void(0);"
                 @click="handleEdit(scoped.row,1)">{{scoped.row.name}}</a>
              <span v-else>{{scoped.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="driverManagerName" label="河长" align="center" min-width="80"/>
          <el-table-column show-overflow-tooltip label="河湖类型" align="center" min-width="80">
            <template slot-scope="{row}">
              <el-tag :type="row.type === 2?'success':((row.type === 3||row.type === 4)?'warning':'')" size="small">{{typeList[row.type]}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="河湖等级" align="center" min-width="100">
            <template slot-scope="scoped">
              <span v-if="scoped.row.level">{{levelList[scoped.row.level]}}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip prop="driverAreaName" label="区划" min-width="200" align="center"/>
          <el-table-column show-overflow-tooltip prop="startPoint" label="起点" align="center"/>
          <el-table-column show-overflow-tooltip prop="endPoint" label="终点" align="center"/>
          <el-table-column show-overflow-tooltip prop="length" label="长度（公里）" min-width="110" align="center"/>
          <el-table-column v-if="isAdmin||btnRule.some(btn=>btn=='edit'||btn=='drop')" width="100" label="操作" align="center">
            <template slot-scope="scoped">
              <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                          placement="bottom">
                <el-button @click="handleEdit(scoped.row)" type="primary" size="mini" icon="el-icon-edit"/>
              </el-tooltip>
              <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='drop')" class="item" effect="dark" content="删除"
                          placement="bottom">
                <el-button @click="handleDel(scoped.row)" type="danger" size="mini" icon="el-icon-delete"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div  class="page-current">
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="total, prev, pager, next, jumper"
            style="display: inline-block;"
            @current-change="handleCurrentChange"
            :total="count"/>
        </div>
      </el-tab-pane>
      <el-tab-pane v-if="isAdmin||btnRule.some(btn=>btn=='add')" label="添加河湖" name="2">
        <driverForm :userList="userList" :levelList="levelList" :typeList="typeList"  :ruleForm="ruleForm"
                    style="width: 50%" v-if="activeName==2" @submit="submit"></driverForm>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible" :title="ruleForm.disabled?'河湖详情':'修改河湖'">
      <driverForm :userList="userList"  v-if="addDialogVisible" :levelList="levelList" :typeList="typeList"  ref="dialogForm" :ruleForm="ruleForm"
                  style="width: 100%" @submit="submit" @onBack="onBack"></driverForm>
    </el-dialog>
  </div>
</template>

<script>
  import driverApi from '@/api/basicData/driver'
  import lsadminApi from '@/api/system/lsadmin'
  import driverAreaSelect from '@/components/driverAreaSelect'
  import driverForm from './driverForm'
  import {mapGetters} from "vuex";

  export default {
    name: "driver",
    data() {
      return {
        typeList: {},
        levelList: {},
        activeName: '1',
        searchParam: {
          name: '',
          type: '',
          level: '',
          driverArea:''
        },
        addDialogVisible: false,
        ruleForm: {
          name: '',
          driverManager: '',
          type: '',
          level:'',
          length: '',
          startPoint: '',
          endPoint: '',
          status:1,
          driverArea: '',
          note:'',
        },
        userList:[],
        currentPage: 1,
        pageSize: parseInt((document.body.clientHeight - 290 - 43) / 44.5),
        count: 0,
        driverList: [],
        tableLoading: false,
        tableHeight: document.body.clientHeight - 290
      }
    },
    components: {driverForm,driverAreaSelect},
    computed: {
      ...mapGetters(["btnRule", "isAdmin"])
    },
    created() {
    },
    mounted() {
      this.tableLoading = true
      let me =this
      this.getBaseData(function () {
        me.getAllList()
      })
      this.getDriverManagerList()
    },
    watch: {
      activeName() {
        this.initRuleForm()
      }
    },
    methods: {
      handleDel(row){
        this.$confirm('此操作将删除河道, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableLoading=true
          driverApi.dropDriver({id:row.id}).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getAllList()
          }).catch(e => {
            this.tableLoading =false
          })
        }).catch(() => {
        });
      },
      getDriverManagerList(){
        lsadminApi.getDriverManagerList().then(res => {
          this.userList = res.data?res.data:[]
        }).catch(e => {
          this.userList ==[]
        })
      },
      indexMethod(index) {
        return (index + 1) + ((this.currentPage - 1) * this.pageSize);
      },
      handleEdit(data, type) {
        this.initRuleForm()
        if (type) {
          driverApi.getDetail({id: data.id}).then(res => {
            this.addDialogVisible = true
            this.$nextTick(function () {
              try {
                let child = JSON.parse(JSON.stringify(res.data))
                child.startPoint=child.start_point
                child.endPoint=child.end_point
                child.driverArea=child.driver_area
                child.driverManager=child.driver_manager
                child.disabled = true
                this.ruleForm = Object.assign({}, child)
              } catch (e) {
              }
            })
          }).catch(err => {
          })
        } else {
          this.addDialogVisible = true
          this.$nextTick(function () {
            try {
              let child = JSON.parse(JSON.stringify(data))
              if (type) {
                child.disabled = true
              }
              if(!child.driverManager){
                child.driverManager=''
              }
              this.ruleForm = Object.assign({}, child)
            } catch (e) {
            }
          })
        }
      },
      initRuleForm() {
        this.ruleForm = {
          name: '',
          driverManager: '',
          type: '',
          level:'',
          length: '',
          startPoint: '',
          endPoint: '',
          status:1,
          driverArea: '',
          note:'',
        }
      },
      getAllList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        searchParam.page = this.currentPage
        searchParam.pageSize = this.pageSize
        driverApi.getIndex(searchParam).then(res => {
          this.count = res.data.total
          this.driverList = res.data.records
          this.tableLoading = false
        }).catch(err => {
          this.driverList = []
          this.tableLoading = false
        })
      },
      search() {
        this.currentPage = 1
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      initSearchParam() {
        this.searchParam = {
          name: '',
          type: '',
          level: '',
          driverArea:''
        }
      },
      getBaseData(call) {
        driverApi.getBaseData().then(res => {
          const {area_list, level_list, type_list} = res.data
          this.levelList = level_list ? level_list : {}
          this.typeList = type_list ? type_list : {}
          call()
        }).catch(e => {
          this.levelList = {}
          this.typeList = {}
          call()
        })
      },
      submit(type, group) {
        if (type == 1) {
          this.activeName='1'
          this.search()
        } else {
          this.tableLoading = true
          this.getAllList()
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.addDialogVisible = false
        }
      },
      onBack() {
        this.initRuleForm()
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .driver-container {
    width: 100%;

    .el-input.is-disabled .el-input__inner {
      background-color: #fff !important;
      color: #333 !important;
    }
  }
</style>
