<template>
  <div class="base-repairArea main-wrap">
    <el-form class="search-bar">
      <div class="grid-content">
        <el-input v-model.trim="filters.name" placeholder="请输入车牌号码" clearable></el-input>
      </div>
      <el-button type="primary" icon="el-icon-search" @click="searchList">搜索</el-button>
      <el-button class="margin-css" type="primary" icon="el-icon-upload" @click="showEditDialog(0)">新增</el-button>
    </el-form>
    <div class="tabel-wrap">
      <el-table border v-loading="loading" :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="车牌号码" min-width="80">
        </el-table-column>
        <el-table-column prop="createDate" :formatter="(c,r,val) => $formatDate(new Date(val),'yyyy-MM-dd hh:mm:ss')" label="创建时间" min-width="80">
        </el-table-column>
        <el-table-column prop="purpose" label="用途" min-width="120">
        </el-table-column>
        <el-table-column prop="sortId" label="排序" min-width="50"></el-table-column>
        <el-table-column fixed="right" label="操作" min-width="80" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" class="operationBtnWidth" @click="showEditDialog(1, scope)">修改</el-button>
            <el-button type="primary" size="mini" class="operationBtnWidth" @click="deleteHandle(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" :current-page="pagination.pageNum" :page-size="pagination.pageSize" :total="pagination.total" @current-change="changePage">
      </el-pagination>
    </div>
    <drag-dialog class="rateDialog" :title="editType?'修改':'新增'" :visible.sync="dialogEditVisible" :before-close="resetEditDialog">
      <el-form :disabled="loading">
        <el-form-item label="车牌号码" label-width="200px">
          <el-input v-model.trim="selected.name" placeholder="请输入车牌号码" :maxlength="20" clearable></el-input>
        </el-form-item>
        <el-form-item label="用途" label-width="200px">
          <el-input v-model.trim="selected.purpose" placeholder="请输入用途" :maxlength="20" clearable></el-input>
        </el-form-item>
        <el-form-item label="排序" label-width="200px">
          <el-input-number v-model.trim="selected.sortId" :controls="false" :precision="0" :min="0" :max="1000" clearable></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetEditDialog">取 消</el-button>
        <el-button type="primary" @click="updateSetting">确 定</el-button>
      </span>
    </drag-dialog>
  </div>
</template>

<script>
import listMixin from '@/mixin/listPage.js';
import store from './store.js';
import { mapGetters, mapActions } from 'vuex';
import { settingAdd, updateSetting, settingDel } from '@/api/baseData/baseData';

export default {
  beforeRouteEnter(to, from, next) {
    store.install();
    next();
  },
  mixins: [listMixin],
  data() {
    let typeArr = this.$route.path.split('/');
    let type = typeArr[typeArr.length - 1];
    return {
      type: type,
      filters: {
        name: '',
      },
    };
  },
  computed: {
    ...mapGetters('baseData/publicCarNumber', ['list', 'pagination']),
  },
  methods: {
    ...mapActions('baseData/publicCarNumber', {
      fetchSettingList: 'fetchList',
    }),
    initSelected() {
      this.selected = {
        id: '',
        name: '',
        purpose: '',
        sortId: null,
      };
    },
    fetchList(entity) {
      this.fetchSettingList({
        ...entity,
        type: this.type,
      });
    },
    showEditDialog(type, data) {
      this.editType = type;
      if (type) {
        this.selected = {
          id: data.row.id,
          name: data.row.name,
          purpose: data.row.purpose,
          sortId: data.row.sortId,
        };
      } else {
        this.initSelected();
      }
      this.dialogEditVisible = true;
    },
    validateSelect() {
      if (!this.selected.name) {
        this.$message.error('请输入车牌号码！');
        return;
      }
      return true;
    },
    updateSetting() {
      if (!this.validateSelect()) {
        return;
      }
      const entity = {
        id: this.selected.id,
        name: this.selected.name,
        purpose: this.selected.purpose,
        sortId: this.selected.sortId,
      };
      if (this.editType) {
        updateSetting({ ...entity, type: this.type })
          .then(res => {
            this.fetchList();
            this.resetEditDialog();
            this.$message.success(res.msg);
          })
          .catch(err => {
            this.$message.error(err.msg || '更新失败！');
          });
      } else {
        settingAdd({ ...entity, type: this.type })
          .then(res => {
            this.fetchList();
            this.resetEditDialog();
            this.$message.success(res.msg);
          })
          .catch(err => {
            this.$message.error(err.msg || '新增失败！');
          });
      }
    },
    deleteHandle(data) {
      this.$confirm(`确认要删除 ${data.row.name} 吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const entity = {
            id: data.row.id,
            type: this.type,
          };
          settingDel(entity)
            .then(res => {
              this.fetchList();
              this.$message.success(res.msg);
            })
            .catch(err => {
              this.$message.error('删除失败！');
            });
        })
        .catch(action => {
          console.log(action);
        });
    },
  },
};
</script>
