<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <breadcrumb :breadcrumbTitle="this.$route.meta.title"></breadcrumb>
        <el-col :span="24" class="container">
          <el-col :span="24" class="btn">
            <el-button type="primary" size="mini" @click="dialog = true">添加</el-button>
          </el-col>
          <dataTable :fields="fields" :data="list" :opera="opera" :total="total" @query="search" @edit="toEdit" @delete="toDelete" @repair="toRepair">
          </dataTable>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="dialog" title="客户信息" @close="toClose">
      <data-form :data="form" :fields="formFields" :rules="{}" @save="turnSave"> </data-form>
    </el-dialog>
  </div>
</template>

<script>
import breadcrumb from '@c/common/breadcrumb.vue';
import dataTable from '@/components/frame/filter-page-table.vue';
import dataForm from '@/components/frame/form.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: car } = createNamespacedHelpers('car');
import bus from '@/components/common/bus';
export default {
  name: 'index',
  props: {},
  components: {
    breadcrumb,
    dataTable,
    dataForm,
  },
  data: function() {
    return {
      fields: [
        { label: '顾客姓名', prop: 'name', filter: 'input' },
        { label: '车牌号', prop: 'license', filter: 'input' },
        { label: '电话', prop: 'mobile', filter: 'input' },
        { label: '车型', prop: 'model', filter: 'input' },
        { label: '车辆颜色', prop: 'carcolor' },
        { label: 'VIN号', prop: 'vin' },
        { label: '保险日期', prop: 'insurance' },
        { label: '驾驶证日期', prop: 'drive' },
        { label: '行驶证日期', prop: 'travel' },
      ],
      opera: [
        { label: '修改', method: 'edit' },
        { label: '删除', method: 'delete' },
        { label: '添加维修', method: 'repair' },
      ],
      list: [],
      total: 0,
      dialog: false,
      form: {},
      formFields: [
        { label: '顾客姓名', model: 'name' },
        { label: '车牌号', model: 'license' },
        { label: '电话', model: 'mobile', type: 'number', options: { maxLength: 11, minLength: 11 } },
        { label: '车型', model: 'model' },
        { label: '车辆颜色', model: 'carcolor' },
        { label: 'vin号', model: 'vin' },
        { label: '保险日期', model: 'insurance', type: 'date' },
        { label: '驾驶证日期', model: 'drive', type: 'date' },
        { label: '行驶证日期', model: 'travel', type: 'date' },
      ],
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...car(['query', 'delete', 'create', 'update']),
    // 查询列表
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      } else {
        this.$message({
          message: res.errmsg,
          type: 'error',
        });
      }
    },
    // 修改
    toEdit({ data }) {
      this.$set(this, `form`, data);
      this.dialog = true;
    },
    // 删除
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$message({
          message: '删除成功',
          type: 'success',
        });
        this.toClose();
      } else {
        this.$message({
          message: res.errmsg,
          type: 'error',
        });
      }
    },
    // 查看维修单
    toRepair({ data }) {
      this.$router.push({ path: '/repairorder', query: { id: data.id } });
    },
    // 保存修改
    async turnSave({ data }) {
      if (data.id) {
        const res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '修改成功',
            type: 'success',
          });
          this.toClose();
        } else {
          this.$message({
            message: res.errmsg,
            type: 'error',
          });
        }
      } else {
        const res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '创建成功',
            type: 'success',
          });
          this.toClose();
        } else {
          this.$message({
            message: res.errmsg,
            type: 'error',
          });
        }
      }
    },
    // 取消
    toClose() {
      this.form = {};
      this.dialog = false;
      this.search();
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.btn {
  text-align: right;
  padding: 10px 0;
}
</style>
