<template>
    <d2-container>
        <div class="d2-table-view-full">
            <div class="d2-table-view-header">
                <el-form label-width="100px" labelPosition="right" size="small" :inline="true" :model="filter" @keyup.enter.native="handleList" class="demo-form-inline">
                    <el-form-item label="车道名称">
                        <el-input style="width: 192px" v-model="filter.channelNm" placeholder="车道名称" clearable/>
                    </el-form-item>
                    <!--<el-form-item label="车场区域">-->
                        <!--<el-input style="width: 192px" v-model="filter.areaId" placeholder="车场区域" clearable/>-->
                    <!--</el-form-item>-->
                    <el-form-item label="车场区域" prop="areaId">
                    <el-select v-model="filter.areaId" placeholder="请选择">
                    <el-option
                    v-for="item in options"
                    :key="item.areaId"
                    :label="item.areaNm"
                    :value="item.areaId">
                    </el-option>
                    </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button   type="primary" @click="handleList('1')">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="d2-table-view-content">
                <div class="d2-table-view-handle">
                    <el-button @click="onCreateClick" size="small" icon="el-icon-plus">新增</el-button>
                    <el-button size="small" @click="onMutilDelClick">批量删除</el-button>
                </div>
                <d2-crud
                        title="车道管理"
                        ref="d2Crud"
                        :options="tableOptions"
                        :loading="loading"
                        :columns="columns"
                        :data="data"
                        :rowHandle="rowHandle"
                        @edit="handleRowEdit"
                        @delete="handleRowRemove"
                        selection-row
                        @selection-change="handleSelectionChange"
                >
                </d2-crud>
            </div>
            <div class="d2-table-view-footer">
                <el-pagination
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        :page-size="tableOptions.pageList[0]"
                        :page-sizes="tableOptions.pageList"
                        :current-page.sync="currentPage"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="pagination.total"
                ></el-pagination>
            </div>
        </div>
        <el-dialog
                :title="diaLogName"
                :visible.sync="operVisible"
                @close="clearForm"
                width="40%"
                :close-on-click-modal="false"
        >
            <el-form ref="form" :model="form" label-width="110px" labelPosition="right" :rules="rules">
                <el-row :gutter="15">
                    <el-col :span="22">
                        <el-form-item label="车场车道编号" prop="channelId">
                            <el-input maxlength="20" disabled v-model.trim="form.channelId" placeholder="系统自动生成"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="车场车道名称" prop="channelNm">
                            <el-input maxlength="25" v-model.trim="form.channelNm"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="开闸方式" prop="openMode">
                            <custom-select :value="form.openMode" v-model="form.openMode" typeName="开闸方式" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="出入方式" prop="ioTyp">
                            <el-select v-model="form.ioTyp" placeholder="请选择">
                                <el-option
                                        label="出口"
                                        value="出口">
                                </el-option>
                                <el-option
                                        label="入口"
                                        value="入口">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="车场区域" prop="areaId">
                            <el-select v-model="form.areaId" placeholder="请选择">
                                <el-option
                                        v-for="item in options"
                                        :key="item.areaId"
                                        :label="item.areaNm"
                                        :value="item.areaId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="工作站名" prop="stationId">
                            <!--<el-input maxlength="50" v-model.trim="form.stationId"/>-->
                            <el-select v-model="form.stationId" placeholder="请选择">
                                <el-option
                                        v-for="item in workStationOptions"
                                        :key="item.stationId"
                                        :label="item.staticonNm"
                                        :value="item.stationId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="对应设备" prop="deviceId">
                            <el-select v-model="form.deviceId" placeholder="请选择">
                                <el-option
                                        v-for="item in equipmentOptions"
                                        :key="item.deviceId"
                                        :label="item.deviceNm"
                                        :value="item.deviceId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                 <el-button :loading="staLoading" @click="operInstance(1)" v-if="isShow">继续添加</el-button>
        <zl-button :loading="staLoading" @click="operInstance(2)">完成</zl-button>
      </span>
        </el-dialog>
    </d2-container>
</template>
<script>
  import { tableOptions } from '@/assets/options'
  import {getAreaOptions} from '@/api/parking/costRules'
  import {getMangeTrafficList,getWorkStationOptions,getEquipmentOptions,saveOrUpdateTraffic,getTrafficById,onDeleteTraffic} from '@/api/parking/mangeTraffic'
  export default {
    name: 'system-staff',
    components: {

    },
    data () {
      return {
        isShow:true,
        schannelIds:[],
        schannelIdss:"",
        equipmentOptions:[],
        workStationOptions:[],
        options:[],
        time:"",
        options:[],
        stationId:[],
        stationIds:"",
        isCreate:false,
        userId:"",
        checkList:[],
        // classVisible:false,
        staLoading: false,
        multipleSelection:[],
        func: {
          add: false,
          delete: false,
          update: false,
          select: false
        },
        falg: true,
        isExpend: true,
        tableOptions,
        loading: false,
        operVisible: false,
        diaLogName: '新增',
        lanuchs: [],
        form: {
          channelId:"",
          channelNm:"",
          ioTyp:"",
          openMode:"",
          areaId:"",
          stationId:"",
          deviceId:""
        },
        filter: {
          pageSize: 10,
          pageNum: 1,
          channelNm:"",
          areaId:""
        },
        pagination: {
          total: 0
        },
        currentPage: 1,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '车道编号',
            key: 'channelId',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '车道名称',
            key: 'channelNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '出入类型',
            key: 'ioTyp',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '车场区域',
            key: 'areaNm',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '所属工作站',
            key: 'staticonNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '开闸方式',
            key: 'openMode',
            minWidth: '90',
            formatter (row, column, cellValue, index) {
              if (cellValue === "0") {
                return '否'
              }else if(cellValue === "1"){
                return '是'
              }
            }
          },
          {
            title: '对应设备',
            key: 'deviceNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
        ],
        rowHandle: {
          columnHeader: '操作',
          minWidth: '180',
          align: 'center',
          custom: [
            {
              text: '编辑',
              type: 'primary',
              size: 'small',
              emit: 'edit'
            },
            {
              text: '删除',
              type: 'danger',
              size: 'small',
              emit: 'delete'
            }
          ]
        },
        data: [],
        rules: {
          channelNm:[{required: true, message: '车道名称不能为空', trigger: 'blur'}],
          deviceId:[{required: true, message: '请选择对应设备', trigger: 'blur'}],
          openMode:[{required: true, message: '请选择开闸方式', trigger: 'blur'}],
          ioTyp:[{required: true, message: '请选择出入方式', trigger: 'blur'}],
          areaId:[{required: true, message: '请选择车场区域', trigger: 'blur'}],
          stationId:[{required: true, message: '请选择工作站名', trigger: 'blur'}],
        }
      }
    },

    methods: {
      dateChange (val) {
        if(val == null){
          this.filter.bgTm = ''
          this.filter.endTm = ''
        }else{
          this.filter.bgTm = val[0]
          this.filter.endTm = val[1]
        }
      },
      onCreateClick(){
        this.operVisible=true
      },
      handleTreeNode () {
        this.handleList()
      },
      handleList (val) {
        if (val) {
          this.filter.pageNum = 1
          this.currentPage = 1
        }
        this.loading = true
        getMangeTrafficList(this.filter)
          .then(res => {
            // console.log(res)
            this.loading = false
            this.data = res.page.list
            this.pagination.total = res.page.total
          })
          .catch(err => {
            this.loading = false
          })
      },
      handleSelectionChange(val) {
        this.isCreate=false
        this.multipleSelection = val;
      },
      handleCurrentChange (val) {
        this.filter.pageNum = val
        this.handleList()
      },
      handleSizeChange (val) {
        this.filter.pageNum = 1
        this.filter.pageSize = val
        this.handleList()
      },
      operInstance (val) {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.staLoading = true
            saveOrUpdateTraffic(this.form).then(res => {
              this.staLoading = false
              this.clearForm()
              if (val == 2) {
                this.operVisible = false
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.handleTreeNode()
              } else {
                this.clearForm()
              }

            }).catch(err => {
              this.staLoading = false
            })
          }
        })
      },
      handleRowEdit ({index, row}) {
        this.diaLogName = '修改'
        this.isShow=false
        getTrafficById({channelId:row.channelId}).then(res => {
          this.form = res.parkChannel
        })
        this.operVisible = true
      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确定删除当前车道?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.schannelIds=[]
            this.multipleSelection.forEach((item,index)=>{
              this.schannelIds.push(item.channelId)
            })
            this.schannelIdss=this.schannelIds.join(",")
            onDeleteTraffic({channelIds:this.schannelIdss}).then(res=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.handleTreeNode()
            })
          })
        }
      },
      handleRowRemove ({index, row}) {
        // console.log(row)
        this.$confirm(`确定删除当前车道?`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          onDeleteTraffic({channelIds:row.channelId}).then(res=>{
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleTreeNode()
          })
        })
      },
      clearForm () {
        if (this.$refs['form']!==undefined) {
          this.$refs['form'].resetFields()
        }
        this.form = {}
        this.diaLogName = '新增'
        this.isShow=true
      },
    },
    mounted () {
      this.clearForm()
      getAreaOptions().then((res)=>{
        this.options=res.list
      })
      getWorkStationOptions().then(res=>{
        this.workStationOptions=res.list
      })
      getEquipmentOptions().then(res=>{
        this.equipmentOptions=res.list
      })
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions, 102)
      this.handleTreeNode()
    }
  }
</script>
<style>
    .d2-table-view-full{
        height: 100%;
    }
    .timeBox{
        margin-top: 10px;
    }
    .timePay{
        margin-left: 8px;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .timeLabel label{
        width: 110px !important;
    }
    .bankLabel label{
        font-size: 13px;
    }
    .el-form-item__label {
        text-align: left !important;
    }
    .d2-table-view{
        overflow: hidden;
    }
    .d2-table-view-content{
        height: 100%;
    }
    .el-table{
        height: 450px !important;
    }
</style>