<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="small">
      <el-form-item label="编码" prop="supplierCode">
        <el-input v-model="queryParams.supplierCode" clearable placeholder="供应商编码" style="width:150px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="供应商名称" style="width:170px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="全部" style="width:140px">
          <el-option v-for="t in typeOptions" :key="t.value" :label="t.label" :value="t.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" 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" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:supplier:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete()" v-hasPermi="['erp:supplier:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" border size="small" @selection-change="onSelectionChange">
      <el-table-column type="selection" width="46" align="center" />
      <el-table-column label="供应商编码" prop="supplierCode" width="140" show-overflow-tooltip />
      <el-table-column label="供应商名称" prop="name" min-width="180" show-overflow-tooltip />
      <el-table-column label="类型" width="120" align="center">
        <template slot-scope="s">{{ typeText(s.row.type) }}</template>
      </el-table-column>
      <el-table-column label="信用等级" width="90" align="center">
        <template slot-scope="s"><span class="sp-credit" :class="'c' + (s.row.creditRating || '')">{{ s.row.creditRating || '—' }}</span></template>
      </el-table-column>
      <el-table-column label="联系人" prop="contactPerson" width="100" />
      <el-table-column label="联系电话" prop="contactPhone" width="130" />
      <el-table-column label="地址" prop="address" min-width="180" show-overflow-tooltip />
      <el-table-column label="备注" prop="remark" min-width="140" show-overflow-tooltip />
      <el-table-column label="操作" width="130" align="center" fixed="right">
        <template slot-scope="s">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="handleUpdate(s.row)" v-hasPermi="['erp:supplier:edit']">编辑</el-button>
          <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(s.row)" v-hasPermi="['erp:supplier:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <el-dialog :title="title" :visible.sync="open" width="640px" append-to-body>
      <el-form :model="form" :rules="rules" ref="form" label-width="90px" size="small">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="供应商编码" prop="supplierCode">
              <el-input v-model="form.supplierCode" placeholder="请输入编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="t in typeOptions" :key="t.value" :label="t.label" :value="t.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用等级" prop="creditRating">
              <el-select v-model="form.creditRating" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="c in creditOptions" :key="c" :label="c" :value="c" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" :rows="2" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="open = false">取消</el-button>
        <el-button type="primary" :loading="buttonLoading" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, addSupplier, updateSupplier, delSupplier } from '@/api/erp/purchase'

export default {
  name: 'ErpSupplier',
  data() {
    return {
      loading: true,
      buttonLoading: false,
      showSearch: true,
      total: 0,
      list: [],
      ids: [],
      multiple: true,
      title: '',
      open: false,
      form: {},
      queryParams: { pageNum: 1, pageSize: 20, supplierCode: undefined, name: undefined, type: undefined },
      typeOptions: [
        { value: 'RAW', label: '原料供应商' },
        { value: 'AUX', label: '辅料供应商' },
        { value: 'OTHER', label: '其他' }
      ],
      creditOptions: ['A', 'B', 'C', 'D'],
      rules: {
        supplierCode: [{ required: true, message: '请输入供应商编码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listSupplier(this.queryParams).then(res => {
        this.list = res.rows || []
        this.total = res.total || 0
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    onSelectionChange(sel) {
      this.ids = sel.map(i => i.supplierId)
      this.multiple = !sel.length
    },
    reset() {
      this.form = { supplierId: null, supplierCode: '', name: '', type: undefined, creditRating: undefined, contactPerson: '', contactPhone: '', address: '', remark: '' }
      this.resetForm('form')
    },
    handleAdd() {
      this.reset()
      this.title = '新增供应商'
      this.open = true
    },
    handleUpdate(row) {
      this.reset()
      this.form = { ...row }
      this.title = '编辑供应商'
      this.open = true
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        this.buttonLoading = true
        const api = this.form.supplierId ? updateSupplier : addSupplier
        api(this.form).then(() => {
          this.$modal.msgSuccess('保存成功')
          this.open = false
          this.getList()
        }).finally(() => { this.buttonLoading = false })
      })
    },
    handleDelete(row) {
      const ids = row && row.supplierId ? row.supplierId : this.ids
      const tip = row && row.name ? '「' + row.name + '」' : '选中的 ' + this.ids.length + ' 个供应商'
      this.$modal.confirm('确认删除' + tip + '？').then(() => {
        return delSupplier(ids)
      }).then(() => {
        this.$modal.msgSuccess('删除成功')
        this.getList()
      }).catch(() => {})
    },
    handleExport() {
      this.download('erp/supplier/export', { ...this.queryParams }, `supplier_${new Date().getTime()}.xlsx`)
    },
    typeText(t) {
      const o = this.typeOptions.find(x => x.value === t)
      return o ? o.label : (t || '—')
    }
  }
}
</script>

<style lang="scss" scoped>
$accent: #5b8db8;
.sp-credit {
  display: inline-block; min-width: 18px; font-size: 12px; font-weight: 600; color: #909399;
  &.cA { color: #3a8a4d; }
  &.cB { color: $accent; }
  &.cC { color: #d6a256; }
  &.cD { color: #c45656; }
}
</style>
