<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="级别名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入级别名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:customerLevel:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border height="500" v-loading="loading" :data="customerLevelList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="级别名称" align="center" prop="name" />
      <el-table-column  label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{getStatusLabel(scope.row.status)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="sort" />
      <el-table-column label="操作"   align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:customerLevel:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:customerLevel:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="pageSizes"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="customLevelDialog.title" :visible.sync="customLevelDialog.open" width="500px" append-to-body>
      <el-form ref="form" :model="customLevelDialog.form" :rules="customLevelDialog.rules" label-width="80px">
        <el-form-item label="级别名称" prop="name">
          <el-input v-model="customLevelDialog.form.name" placeholder="请输入级别名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="customLevelDialog.form.status" label="0">正常</el-radio>
          <el-radio v-model="customLevelDialog.form.status" label="1">停用</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="customLevelDialog.form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {add, del, get, edit, list} from '@/api/crm/customerLevel'

const initForm = {
  id: null,
  name: null,
  status: '0',
  sort: 0
}

const statusOptions = [
  {
    dictLabel: "正常",
    dictValue: 0,
    type: 'success'
  },
  {
    dictLabel: "停用",
    dictValue: 1,
    type: 'danger'
  }
]

export default {
  name: "index",
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },
      loading: false,
      customerLevelList: [],
      statusOptions: statusOptions,
      total: 0,
      pageSizes:[10, 20, 30, 50, 100],
      showSearch: true,
      customLevelDialog: {
        title: "新增",
        open: false,
        form: initForm,
        rules: {
          name: [
            { required: true, message: "级别名称不能为空", trigger: "blur" }
          ]
        }
      }
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true
      list(this.queryParams).then(response => {
        this.customerLevelList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    getStatusType(status) {
      return this.statusOptions.find(val => val.dictValue === status)?.type || ''
    },
    getStatusLabel(status) {
      return this.statusOptions.find(val => val.dictValue === status)?.dictLabel || ''
    },
    handleAdd() {
      this.reset()
      this.customLevelDialog.open = true
      this.customLevelDialog.title = "新增级别"
    },
    handleUpdate(row) {
      this.reset()
      get(row.id).then(response => {
        this.customLevelDialog.form = response.data
        this.customLevelDialog.form.status = response.data.status.toString()
        this.customLevelDialog.open = true
        this.customLevelDialog.title = "修改级别"
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除级别编号为"' + row.id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return del(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.customLevelDialog.form.id != null) {
            edit(this.customLevelDialog.form).then(response => {
              const {code} = response
              if (code === 200) {
                this.msgSuccess("修改成功");
                this.customLevelDialog.open = false;
                this.getList();
              }
            })
          } else {
            add(this.customLevelDialog.form).then(response => {
              const {code} = response
              if (code === 200) {
                this.msgSuccess("新增成功");
                this.customLevelDialog.open = false;
                this.getList();
              }
            })
          }
        }
      });
    },
    cancel() {
      this.customLevelDialog.open = false
      this.reset();
    },
    reset() {
      this.customLevelDialog.form = initForm
      this.resetForm("form")
    },
  }
}
</script>

<style scoped>

</style>
