<template>
  <base-list>
    <template slot="n_header">
      <el-form v-model="pagerDto" inline>
        <el-form-item label="库房名字">
          <el-input v-model="pagerDto._Eq_Name" placeholder="库房名字 " clearable />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select v-model="pagerDto._Eq_IsEnable" placeholder="请选择" clearable>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            /> </el-select></el-form-item>
        <el-form-item>
          <el-button type="primary" @click="pagerSerach()">搜索</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template slot="n_option">
      <el-button type="primary" size="mini" @click="showAddRoom"> 添加库房 </el-button>
    </template>
    <template>
      <el-table
        v-loading="dataLoading"
        :data="data"
        :highlight-current-row="true"
        :select-on-indeterminate="true"
        width="100%"
      >
        <el-table-column prop="Name" label="名称" />
        <el-table-column prop="Description" label="描述" />
        <el-table-column prop="Sort" label="排序" />
        <el-table-column prop="CreateTime" :formatter="colFormatDate" label="创建日期" />
        <el-table-column prop="IsEnable" label="是否启用">
          <template slot-scope="r">
            <el-switch
              v-model="r.row.IsEnable"
              active-color="#1890ff"
              inactive-color="#ccc"
              @change="changeEnable($event, r.row.Id)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="r">
            <el-link
              icon="el-icon-edit"
              type="primary"
              @click="showEditRoom(r.row)"
            >编辑</el-link>

            <el-link
              icon="el-icon-delete"
              type="danger"
              @click="showDeleteRoom(r.row)"
            >删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 40, 100, 400]"
        :current-page="pagerDto.PageIndex"
        :page-size.sync="pagerDto.PageSize"
        background
        layout="prev, pager, next ,total ,jumper,sizes,slot "
        :total="pagerDto.total"
        @size-change="changePager"
        @current-change="pagerSerach"
      />
    </template>
    <el-dialog
      v-loading="modelLoading"
      title="添加/编辑库房"
      :visible.sync="modelDialogShow"
    >
      <edit-room :model="editModel" @save="saveRoom" />
    </el-dialog>
  </base-list>
</template>
<script>
import BaseList from '@/components/Xyy/baselist'
import {
  queryRoom,
  CreateRoom,
  GetRoom,
  UpdateRoom,
  DisableRoom,
  EnableRoom,
  DeleteRoom
} from '@/api/stock/room'
import colFormatDate from '@/utils/columnFormat'
import editRoom from './components/editRoom'
export default {
  name: 'StockRoomList',
  components: { 'base-list': BaseList, editRoom },
  data() {
    return {
      options: [
        { value: true, label: '是' },
        { value: false, label: '否' }
      ],

      data: [],
      editModel: {},

      dataLoading: false,
      pagerDto: { PageIndex: 0, PageSize: 10, total: 0 },
      modelDialogShow: false,
      modelLoading: false
    }
  },
  mounted() {
    console.log('aaaa')
    this.pagerSerach(1)
  },
  methods: {
    colFormatDate,
    pagerSerach(index) {
      this.dataLoading = true
      if (index) {
        this.pagerDto.PageIndex = index
      }
      queryRoom(this.pagerDto)
        .then((res) => {
          this.data = res.Result
          this.pagerDto.total = res.TotalCount
        })
        .finally((res) => {
          this.dataLoading = false
        })
    },
    changePager(v) {
      this.PageSize = v
      this.pagerSerach()
    },
    changeEnable($event, rowId) {
      var that = this
      if ($event) {
        EnableRoom({ Id: rowId }).then((r) => {
          that.$message({ type: 'success', message: '启用成功' })
        })
      } else {
        DisableRoom({ Id: rowId }).then((r) => {
          that.$message({ type: 'success', message: '停用成功' })
        })
      }
    },

    showEditRoom(row) {
      console.log(row)
      this.editModel = row
      this.modelDialogShow = true
    },
    showAddRoom() {
      this.editModel = { Id: 0 }
      this.modelDialogShow = true
    },
    showDeleteRoom(m) {
      this.$confirm('是否删除该库房?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        DeleteRoom({ id: m.Id }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.pagerSerach()
        })
      })
        .catch(() => {

        })
    },
    saveRoom(m) {
      if (m.Id > 0) {
        this.modelLoading = true
        UpdateRoom(m)
          .then((x) => {
            this.$message({ type: 'success', message: '修改成功' })
            this.pagerSerach()
            this.modelDialogShow = false
          })
          .finally((r) => {
            this.modelLoading = false
          })
      } else {
        this.modelDialogShow = true
        this.modelLoading = true
        CreateRoom(m)
          .then((x) => {
            this.$message({ type: 'success', message: '添加成功' })
            this.pagerSerach()
            this.modelDialogShow = false
          })
          .finally((r) => {
            this.modelLoading = false
          })
      }
    }
  }
}
</script>
