<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input clearable v-model="queryInfo.query" placeholder="请输入供应商名" prefix-icon="el-icon-search">
            <el-button slot="append" icon="el-icon-search" @click="getSupplierList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addSupplierDialog">新增</el-button>
        </el-col>
      </el-row>
      <div class="main-content">
      <el-table :data="supplierList.slice((queryInfo.pageNum-1)*queryInfo.pageSize,queryInfo.pageNum*queryInfo.pageSize)"
      style="width: 100%"
      stripe border fixed height="500">
          <el-table-column
            fixed="left"
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="name"
            label="供应商名称">
          </el-table-column>
          <el-table-column
            prop="contacts"
            label="供应商简称">
          </el-table-column>
          <el-table-column
            prop="number"
            label="供应商地址">
          </el-table-column>
          <el-table-column
            prop="remarks"
            label="备    注">
          </el-table-column>
          <el-table-column
            fixed="right"
            prop="operate"
            label="操作"
            width="180">
            <template slot-scope="scope" >
              <el-button type="primary" icon="el-icon-edit" size="small" @click="updataSupplierdialog(scope.row)">修改</el-button>
              <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteSupplier(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pages">
       <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.pageNum"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="queryInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
        </el-pagination>
      </div>
     </div>
      </el-card>

      <el-dialog :title="title" :visible.sync="supplierDialogVisible" width="65%" @close="closeSupplierDialog">

        <el-form :model="supplierForm" :rules="rules" ref="supplierRuleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="供应商名称" prop="name">
            <el-input v-model="supplierForm.name" ></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="contacts">
            <el-input v-model="supplierForm.contacts" ></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="number">
            <el-input v-model="supplierForm.number" ></el-input>
          </el-form-item>
          <el-form-item label="备    注" prop="remarks">
            <el-input v-model="supplierForm.remarks" ></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="supplierDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="operationSupplier">确 定</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
export default {
  data () {
    const checkPhone = (rule, value, callback) => {
      // 定义校验手机号的正则语法
      const phoneRege = /^1[345789][0-9]{9}$/
      if (phoneRege.test(value)) {
        return callback()
      }
      callback(new Error('请填写正确的手机号'))
    }
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      supplierList: [],
      supplierDialogVisible: false,
      title: '新增供应商',
      supplierForm: {
        name: '',
        contacts: '',
        number: '',
        remarks: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11各数字', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.queryInfo.pageSize = val
      this.getSupplierList()
    },
    handleCurrentChange (val) {
      this.queryInfo.pageNum = val
      this.getSupplierList()
    },
    async getSupplierList () {
      const { data: result } = await this.$http.get('/supplier/list', { params: this.queryInfo })
      if (result.status !== 200) return this.$message.error('获取列表失败')

      this.total = result.data.total
      this.supplierList = result.data.rows
    },
    closeSupplierDialog () {
      this.$refs.supplierRuleForm.resetFields()
    },
    addSupplierDialog () {
      this.title = '新增供应商'
      this.supplierDialogVisible = true
    },
    updataSupplierdialog (supplier) {
      this.supplierForm.name = supplier.name
      this.supplierForm.contacts = supplier.contacts
      this.supplierForm.number = supplier.number
      this.supplierForm.remarks = supplier.remarks
      this.title = '修改供应商'
      this.supplierDialogVisible = true
    },
    addSupplier () {
      this.$refs.supplierRuleForm.validate(async valid => {
        const { data: result } = await this.$http.post('/supplier/addSupplier', this.supplierForm)
        if (result.status !== 200) return this.$message.error('添加供应商失败')
        this.$message.success('成功添加供应商')
        this.getSupplierList()
        this.supplierDialogVisible = false
      })
    },
    async updataSupplier () {
      const { data: result } = await this.$http.put('/supplier/updataSupplier', this.supplierForm)
      if (result.status !== 200) return this.$message.error('修改供应商失败')
      this.$message.success('更新成功')
      this.getSupplierList()
      this.supplierDialogVisible = false
    },
    operationSupplier () {
      if (this.title === '新增供应商') return this.addSupplier()
      if (this.title === '修改供应商') {
        this.updataSupplier()
      }
    },
    async deleteSupplier (supplier) {
      const result = await this.$confirm('此操作将永久删除' + supplier.name + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (result !== 'confirm') return this.$message.info('用户取消操作')
      const { data: resultDB } = await this.$http.delete(`/supplier/delete/${supplier.name}`)
      if (resultDB.status !== 200) return this.$message.error('删除商品失败')
      this.getSupplierList()
      this.$message.success('商品删除成功')
    }
  },
  mounted () {
    this.getSupplierList()
  }
}
</script>

<style>
</style>
