<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="form" v-model="form">
        <el-row>
          <el-col :span="5">
            <el-form-item label="车队名称:">
              <el-input v-model="form.MOTORCADE_NAME" placeholder="搜索" class="filter-item" style="width: 200px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">搜索</el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="MOTORCADE_NAME" label="车队名称"/>
      <el-table-column label="审核状态" align="center">
        <template slot-scope="{row}">
          {{ getAuditType(row.IS_AUDIT) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="300">
        <template slot-scope="{row}">
          <el-button type="primary" icon="el-icon-view" size="mini" @click="handleEdit(row)">编辑</el-button>
          <el-button type="primary" icon="el-icon-view" size="mini" @click="getAuditInfo(row)">备案申请</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
      <div/>
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <flow-step ref="flowStep" append-to-body @refresh="getList"/>
    <!--新增-->
    <el-dialog :visible.sync="addLoading" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" style="width: 500px;">
        <el-form-item label="车队名称:" prop="ADD_MOTORCADE_NAME">
          <el-input v-model="form.ADD_MOTORCADE_NAME" placeholder="请输入车队名称" class="filter-item" style="width: 200px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addLoading = false">取 消</el-button>
        <el-button type="primary" @click="saveMotorcade">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑-->
    <el-dialog :visible.sync="editLoading"  width="600px">
      <el-form ref="form" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="车队名称:">
          <el-input v-model="form.ADD_MOTORCADE_NAME" placeholder="请输入车队名称" class="filter-item" style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editLoading = false">取 消</el-button>
        <el-button type="primary" @click="editMotorcade()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
export default{
  components: { Pagination },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10
      },
      areaList: [], // 省市县列表
      placeList: [],
      listLoading: true,
      addLoading: false,
      editLoading: false,
      varList: [],
      total: 0,
      title: '',
      isShow: false,
      form: {
        MOTORCADE_ID: '',
        MOTORCADE_NAME: '',
        ADD_MOTORCADE_NAME: '',
        PHONE: '',
        ID_CARD: '',
        CORPINFO_NAME: '',
        DEPARTMENT_NAME: '',
        VISIT_START_TIME: '',
        VISIT_END_TIME: '',
        DOOR_NAME: '',
        TEMPORARY_TYPE: '2',
        CORPINFO_ID: JSON.parse(sessionStorage.getItem('user')).CORPINFO_ID
      },
      rules: {
        ADD_MOTORCADE_NAME: [
          { required: true, message: '车队名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getAuditType(IS_AUDIT){
      if (IS_AUDIT === '1'){
        return '审核通过'
      }else {
        return '未审核'
      }
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 重置
    reset() {
      this.form.MOTORCADE_NAME = ''
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/mkmj/management/getMotorcadeList?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page, this.form
      ).then((data) => {
        console.log(data)
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    handleAdd(){
      this.addLoading = true
    },
    saveMotorcade(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          requestFN(
            '/mkmj/management/motorcadeInfoManagement',
            {
              MOTORCADE_NAME: this.form.ADD_MOTORCADE_NAME,
              CORPINFO_ID: JSON.parse(sessionStorage.getItem('user')).CORPINFO_ID
            }
          ).then((data) => {
            this.addLoading = false
            this.form.MOTORCADE_NAME = ''
            this.getQuery()
          }).catch((e) => {
            this.addLoading = false
          })
        } else {
          this.$message.error('请填写完整信息');
        }
      })
    },
    handleEdit(row){
      this.form.MOTORCADE_ID = row.MOTORCADE_ID
      this.editLoading = true
    },
    editMotorcade(){
      requestFN(
        '/mkmj/management/motorcadeInfoManagement',
        {
          MOTORCADE_ID : this.form.MOTORCADE_ID,
          MOTORCADE_NAME: this.form.ADD_MOTORCADE_NAME,
          CORPINFO_ID: JSON.parse(sessionStorage.getItem('user')).CORPINFO_ID
        }
      ).then((data) => {
        this.editLoading = false
        this.form.MOTORCADE_NAME = ''
        this.getQuery()
      }).catch((e) => {
        this.editLoading = false
      })
    },
    handleDelete(row) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requestFN(
          '/mkmj/management/deleteMotorCade',
          {
            MOTORCADE_ID : row.MOTORCADE_ID
          }
        ).then((data) => {
            this.listLoading = false
            this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    getAuditInfo(row) {
      this.$parent.activeName = 'Apply'
      this.$parent.MOTORCADE_ID = row.MOTORCADE_ID
    }
  }

}
</script>

