<template>
    <d2-container>
        <div class="d2-table-view-full" style="overflow: hidden">
            <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.trim="filter.areaNm" placeholder="区域名称" clearable/>
                    </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="150px" labelPosition="right" :rules="rules">
                <el-row :gutter="15">
                    <el-col :span="24">
                        <el-form-item label="车场区域编号" prop="areaId">
                            <el-input maxlength="20" disabled v-model.trim="form.areaId" placeholder="系统自动生成"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="车场区域名称" prop="areaNm">
                            <el-input maxlength="20" v-model.trim="form.areaNm"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="车位总数" prop="vehicleCount">
                            <el-input maxlength="8" v-model.trim="form.vehicleCount"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="固定车位" prop="fixCount">
                            <el-input maxlength="10" v-model.trim="form.fixCount"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="临时车位" prop="tempCount">
                            <el-input maxlength="10" v-model.trim="form.tempCount"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="剩余车位显示模式" prop="showCountMode">
                            <custom-select :value="form.showCountMode" v-model="form.showCountMode" typeName="剩余车位显示模式" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="满位入场模式" prop="fullInMode">
                            <custom-select :value="form.fullInMode" v-model="form.fullInMode" typeName="满位入场模式" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="未登记车辆执行动作" prop="forbidinAction">
                            <custom-select :value="form.forbidinAction" v-model="form.forbidinAction" typeName="未登记车辆执行动作" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="车辆入场时间段控制" prop="timeMode">
                            <custom-select :value="form.timeMode" v-model="form.timeMode" typeName="车辆入场时间段控制" style="width: 100%"></custom-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 {getAreaList,saveOrUpdateArea,getAreaById,onDeleteArea} from '@/api/parking/setArea'
  export default {
    name: 'system-staff',
    components: {

    },
    data () {
      let checkNumber = (rule, value, callback) => {
        if(!value&&value!==0){
          return callback(new Error('不能为空'))
        }
        let reg = /^[0-9]*$/
        if (!reg.test(value)) {
          return callback(new Error('请输入数字'))
        } else {
          callback ()
        }
      }
      return {
        isShow:true,
        options:[],
        areaIds:[],
        areaIdss:"",
        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: {
          areaId:"",
          areaNm:"",
          vehicleCount:"",
          fixCount:"",
          tempCount:"",
          showCountMode:"",
          fullInMode:"",
          forbidinAction:"",
          timeMode:""
        },
        filter: {
          pageSize: 10,
          pageNum: 1,
          areaNm:""
        },
        pagination: {
          total: 0
        },
        currentPage: 1,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '车辆区域编号',
            key: 'areaId',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '车辆区域名称',
            key: 'areaNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '车位总数',
            key: 'vehicleCount',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '固定车位',
            key: 'fixCount',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '临时车位',
            key: 'tempCount',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '剩余车位显示模式',
            key: 'showCountMode',
            minWidth: '160',
            formatter (row, column, cellValue, index) {
              if (cellValue === '0') {
                return '显示所有剩余车位'
              } else if (cellValue === '1') {
                return '显示临时车剩余车位'
              }else if (cellValue === '2') {
                return '显示固定车剩余车位'
              }
            }
          },
          {
            title: '满位入场模式',
            key: 'fullInMode',
            minWidth: '160',
            formatter (row, column, cellValue, index) {
              if (cellValue === '0') {
                return '禁止所有车入场'
              } else if (cellValue === '1') {
                return '允许所有车入场'
              }else if (cellValue === '2') {
                return '运行指定车入场'
              }
            }
          },
          {
            title: '未登记车辆执行动作',
            key: 'forbidinAction',
            minWidth: '120',
            formatter (row, column, cellValue, index) {
              if (cellValue === '0') {
                return '禁止入场'
              } else if (cellValue === '1') {
                return '人工控制'
              }else if (cellValue === '2') {
                return '自动控制'
              }
            }
          },
          {
            title: '入场时间模式',
            key: 'timeMode',
            minWidth: '160',
            formatter (row, column, cellValue, index) {
              if (cellValue === '0') {
                return '全部车辆放行'
              } else if (cellValue === '1') {
                return '固定车辆控制时间段'
              }else if (cellValue === '2') {
                return '临时车辆控制时间段'
              } else if (cellValue === '3') {
                return '全部车辆控制时间段'
              }
            }
          },
        ],
        rowHandle: {
          columnHeader: '操作',
          minWidth: '180',
          align: 'center',
          custom: [
            {
              text: '编辑',
              type: 'primary',
              size: 'small',
              emit: 'edit'
            },
            {
              text: '删除',
              type: 'danger',
              size: 'small',
              emit: 'delete'
            }
          ]
        },
        data: [],
        rules: {
          areaNm:[{required: true, message: '区域名称不能为空', trigger: 'blur'}],
          vehicleCount:[{validator: checkNumber,required: true, trigger: 'blur'}],
          fixCount:[{validator: checkNumber,required: true, trigger: 'blur'}],
          tempCount:[{validator: checkNumber,required: true,  trigger: 'blur'}],
          showCountMode:[{required: true, message: '请选择剩余车位显示模式', trigger: 'blur'}],
          fullInMode:[{required: true, message: '请选择满位入场模式', trigger: 'blur'}],
          forbidinAction:[{required: true, message: '请选择未登记车辆执行动作', trigger: 'blur'}],
          timeMode:[{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
        this.isShow=true
      },
      handleTreeNode () {
        this.handleList()
      },
      handleList (val) {
        if (val) {
          this.filter.pageNum = 1
          this.currentPage = 1
        }
        this.loading = true
        getAreaList(this.filter)
          .then(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) {
            if(parseInt(this.form.vehicleCount)<parseInt(this.form.fixCount)+parseInt(this.form.tempCount)){
              this.$message({
                message: '车位总数小于固定车位临时车位之和',
                type: 'warning'
              })
              return
            }
            this.staLoading = true
            saveOrUpdateArea(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.form.stationId=row.stationId
        getAreaById({areaId:row.areaId}).then(res => {
          this.form = res.parkArea
        })
        this.operVisible = true
        this.isShow=false
      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确定要删除当前区域吗?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.areaIds=[]
            this.multipleSelection.forEach((item,index)=>{
              this.areaIds.push(item.areaId)
            })
            this.areaIdss=this.areaIds.join(",")
            onDeleteArea({split:this.areaIdss}).then(res=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.handleTreeNode()
            })
          })
        }
      },
      handleRowRemove ({index, row}) {
        // console.log(row)
        this.$confirm(`确定要删除当前区域吗?`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          onDeleteArea({split:row.areaId}).then(res=>{
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleTreeNode()
          })
        })
      },
      clearForm () {
        if (this.$refs['form']!==undefined) {
          this.$refs['form'].resetFields()
        }
        this.form = {}
        this.diaLogName = '新增'
      },
    },
    mounted () {
      this.clearForm()
      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>