<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="车辆类别名称" prop="carTypNm">
                        <el-input style="width: 192px" v-model.trim="filter.carTypNm" placeholder="车辆类别名称" clearable/>
                    </el-form-item>
                    <el-form-item label="停车卡类型" prop="paramCode">
                        <custom-select :value="filter.paramCode" v-model="filter.paramCode" typeName="停车卡类型" style="width: 100%"></custom-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="162px" labelPosition="right" :rules="rules">
                <el-row :gutter="15">
                    <el-col :span="22">
                        <el-form-item label="车场类别编号" prop="carTypId">
                            <el-input maxlength="20" disabled v-model.trim="form.carTypId" placeholder="系统自动生成"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="车场类别名称" prop="carTypNm">
                            <el-input maxlength="25" v-model.trim="form.carTypNm"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="放行方式" prop="ioSort">
                            <custom-select :value="form.ioSort" v-model="form.ioSort" typeName="车辆放行方式" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="规定时间组外通行方式" prop="outTimeIoSort">
                            <custom-select :value="form.outTimeIoSort" v-model="form.outTimeIoSort" typeName="车辆规定时间组外通行方式" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="通行时间组" prop="timeGroup">
                            <el-select v-model="form.timeGroup" placeholder="请选择" style="width: 100%">
                                <el-option
                                        v-for="item in options"
                                        :key="item.gtId"
                                        :label="item.gtNm"
                                        :value="item.gtId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                    <el-form-item label="允许停留时长" prop="remainTime">
                        <el-input maxlength="50" v-model.trim="form.remainTime" clearable/>
                    </el-form-item>
                </el-col>
                    <el-col :span="22">
                        <el-form-item label="超时提醒时长" prop="timeoutTime">
                            <el-input maxlength="50" v-model.trim="form.timeoutTime" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="车辆类别类型" prop="carTyp">
                            <custom-select :value="form.carTyp" v-model="form.carTyp" typeName="车辆类别类型" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="22">
                        <el-form-item label="计费规则" prop="ruleId">
                            <el-select v-model="form.ruleId" placeholder="请选择" style="width: 100%">
                                <el-option
                                        v-for="item in options3"
                                        :key="item.ruleId"
                                        :label="item.feeTypNm"
                                        :value="item.ruleId">
                                </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 {getCateUserList,getTimeGroupOptions,getCostRules,saveOrUpdateCateUser,getCateUserById,onDeleteCateUser} from '@/api/parking/mangeCar/cateUser'
  export default {
    name: 'cateUser',
    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,
        carTypIds:[],
        carTypIdss:"",
        equipmentOptions:[],
        workStationOptions:[],
        options:[],
        options3:[],
        time:"",
        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: {
          carTypId:"",
          carTypNm:"",
          ioSort:"",
          outTimeIoSort:"",
          timeGroup:"",
          remainTime:"",
          timeoutTime:"",
          carTyp:"",
          ruleId:""
        },
        filter: {
          pageSize: 10,
          pageNum: 1,
          carTypNm:"",
          paramCode:""
        },
        pagination: {
          total: 0
        },
        currentPage: 1,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '类别编号',
            key: 'carTypId',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '车辆类别',
            key: 'carTypNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '停车卡类型',
            key: 'paramData',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '放行方式',
            key: 'ioSort',
            minWidth: '90',
            formatter (row, column, cellValue, index) {
              if (cellValue === "0") {
                return '自由放行'
              }else if(cellValue === "1"){
                return '手动放行'
              }else if(cellValue === "2"){
                return '不允许通行'
              }
            }
          },
          {
            title: '规定时间外放行方式',
            key: 'outTimeIoSort',
            minWidth: '150',
            formatter (row, column, cellValue, index) {
              if (cellValue === "0") {
                return '自由放行'
              }else if(cellValue === "1"){
                return '手动放行'
              }else if(cellValue === "2"){
                return '不允许通行'
              }
            }
          },
          {
            title: '车辆类型',
            key: 'carTyp',
            minWidth: '120',
            formatter (row, column, cellValue, index) {
              if (cellValue === "0") {
                return '固定车型'
              }else if(cellValue === "1"){
                return '收费车型'
              }
            }
          },
          {
            title: '时间组',
            key: 'gtNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '允许停留时长(时)',
            key: 'remainTime',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '超时提醒时长(时)',
            key: 'timeoutTime',
            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: {
          carTypNm:[{required: true, message: '车辆类别名称不能为空', trigger: 'blur'}],
          ioSort:[{required: true, message: '请选择放行方式', trigger: 'blur'}],
          outTimeIoSort:[{required: true, message: '请选择超过规定时间进出方式', trigger: 'blur'}],
          timeGroup:[{required: true, message: '请选择通行时间组', trigger: 'blur'}],
          remainTime:[{validator: checkNumber,required: true, trigger: 'blur'}],
          timeoutTime:[{validator: checkNumber,required: true,  trigger: 'blur'}],
          carTyp:[{required: true, message: '请选择车辆类别类型', trigger: 'blur'}],
          ruleId:[{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
        getCateUserList(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
            saveOrUpdateCateUser(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
        // this.form.carTypId=row.carTypId
        getCateUserById({carTypId:row.carTypId}).then(res => {
          this.form = res.parkCarType
        })
        this.operVisible = true
      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确定删除当前车辆类别?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.carTypIds=[]
            this.multipleSelection.forEach((item,index)=>{
              this.carTypIds.push(item.carTypId)
            })
            this.carTypIdss=this.carTypIds.join(",")
            onDeleteCateUser({carTypIds:this.carTypIdss}).then(res=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.handleTreeNode()
            })
          })
        }
      },
      handleRowRemove ({index, row}) {
        // console.log(row)
        this.$confirm(`确定删除当前车辆类别?`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          onDeleteCateUser({carTypIds:row.carTypId}).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()
      getTimeGroupOptions().then((res)=>{
        this.options=res.list
      })
      getCostRules().then(res=>{
        this.options3=res.list
      })
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions, 51)
      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>