<template>
  <div>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" style="margin-top: 10px;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >{{ $t('common.add') }}</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <div style="margin-top: 16px">
      <el-table
        :data="groupList"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="index"
          width="70"
          :label="$t('supplier.index')"
          align="center"
          header-align="center"
        />
        <el-table-column
          :label="$t('supplier.groupTitle')"
          align="center"
          header-align="center"
          prop="title"
        />
        <el-table-column
          prop="beizhu"
          :label="$t('supplier.groupBeizhu')"
          align="center"
          header-align="center"
        />
        <el-table-column
          prop="createTime"
          :label="$t('supplier.createTime')"
          align="center"
          header-align="center"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template></el-table-column>
        <el-table-column :label="$t('common.action')" align="center" header-align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >{{ $t('common.update') }}</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >{{ $t('common.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="customeParams.page"
      :limit.sync="customeParams.rows"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="126px">
        <el-form-item :label="$t('supplier.groupAorder')" prop="aorder">
          <el-input v-model="form.aorder" :placeholder="$t('supplier.groupAorderNotice')" />
        </el-form-item>
        <el-form-item :label="$t('supplier.groupTitle')" prop="title">
          <el-input v-model="form.title" :placeholder="$t('supplier.groupTitleNotice')" />
        </el-form-item>
        <el-form-item :label="$t('supplier.beizhu')" prop="maitou">
          <el-input v-model="form.beizhu" :placeholder="$t('supplier.beizhuNotice')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('supplier.submit') }}</el-button>
        <el-button @click="cancel">{{ $t('supplier.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getGroupList, getGroup, createGroup, updateGroup, deleteGroup } from '@/api/supplier'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import RightToolbar from '@/components/RightToolbar'
export default {
  components: {
    Pagination, RightToolbar
  },
  data() {
    return {
      parseTime,
      groupList: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      customeParams: {
        page: 1,
        rows: 10,
        phone: null,
        name: null,
        company: null,
        pid: null
      },
      countryList: [],
      title: '',
      // 是否显示弹出层
      open: false,
      form: {},
      rules: {
        title: [{ required: true, message: this.$t('supplier.groupTitleNotice'), trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getGroupList(this.customeParams).then((r) => {
        this.groupList = r.data.list
        this.total = r.data.total
      })
    },
    getGroup() {
      getGroupList({ page: 1, rows: 100 }).then(r => {
        this.groupList = r.data.list
      })
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('supplier.addGroup')
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.customeParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.customeParams = {}
      this.handleQuery()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          updateGroup(this.form).then(response => {
            this.$message({
              message: this.$t('common.success'),
              type: 'success'
            })
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createGroup(this.form).then(response => {
          this.$message({
            message: this.$t('common.success'),
            type: 'success'
          })
          this.open = false
          this.getList()
        })
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getGroup({ id: id }).then(response => {
        this.form = response.data
        this.open = true
        this.title = this.$t('supplier.updateGroup')
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$confirm(this.$t('supplier.deleteGroup')).then(function() {
        return deleteGroup(id)
      }).then(() => {
        this.getList()
        this.$message({
          message: this.$t('common.success'),
          type: 'success'
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
::v-deep .el-table__header .el-table__cell {
  background-color: #27417c;
  font-size: 13px;
  height: 38px;
  text-align: center;
  font-size: 13px;
  font-weight: 500;
  color: #ffffff;
}
</style>
