<template>
  <el-card shadow="never" class="aui-card--fill">
    <el-form
      style="display: flex; align-items: center; justify-content: right;"
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          size="small"
          v-model="dataForm.paramKey"
          placeholder="请输入关键字查询"
        />
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="getDataList()">
            <svg class="icon-svg aui-sidebar__menu-icon"><use xlink:href="#icon-sousuo"></use></svg>
          查询
        </el-button>
        <el-button size="small" type="primary" @click="addOrUpdateHandle()">
            <svg class="icon-svg aui-sidebar__menu-icon"><use xlink:href="#icon-新建"></use></svg>
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      :stripe="true"
      :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        height: '56px'
      }"
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        label="序号"
        width="50"
      >
      </el-table-column>
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="code" label="编码"> </el-table-column>
      <el-table-column
        type="type"
        header-align="center"
        align="center"
        label="类型"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.type === 0 ? '' : 'success'">{{
            scope.row.type === 0 ? "工厂" : "供应商"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="supplier" label="供应商"> </el-table-column>
      <el-table-column prop="telephone" label="联系地址"> </el-table-column>
      <el-table-column prop="desc" label="描述"> </el-table-column>
      <el-table-column
        type="status"
        header-align="center"
        align="center"
        label="状态"
        width="80"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'danger' : 'success'">{{
            scope.row.status === 0 ? "不可用" : "可用"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
          >
            <el-tooltip
              class="item"
              effect="dark"
              content="修改"
              placement="top"
            >
            <svg class="icon-svg aui-sidebar__menu-icon"><use xlink:href="#icon-编辑"></use></svg>
            </el-tooltip>
          </el-button>
          <!-- <el-button type="text" style="color:red" size="small" @click="deleteHandle(scope.row.id)">
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <svg class="icon-svg aui-sidebar__menu-icon"><use xlink:href="#icon-删除"></use></svg>
            </el-tooltip>
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </el-card>
</template>

<script>
import AddOrUpdate from './components/factoryManage-add'
export default {
  data () {
    return {
      dataForm: {
        paramKey: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      // this.dataListLoading = true
      // this.$http({
      //   url: this.$http.adornUrl('/alarmBase/page'),
      //   method: 'post',
      //   data: this.$http.adornData({
      //     'current': this.pageIndex,
      //     'size': this.pageSize,
      //     'key': this.dataForm.paramKey
      //   })
      // }).then(({data}) => {
      //   if (data && data.code === 0) {
      //     this.dataList = data.data.list
      //     this.totalPage = data.data.total
      //   } else {
      //     this.dataList = []
      //     this.totalPage = 0
      //   }
      //   this.dataListLoading = false
      // })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定对[id=${id}]进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/alarmBase/delete'),
            method: 'post',
            data: this.$http.adornData({ id })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
