<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加库房
      </el-button>
    </div>
    <div class="top-right-btn">
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button size="mini" circle icon="el-icon-refresh" @click="getDepotInfo" />
      </el-tooltip>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column
        prop="depotName"
        label="库房名称">
      </el-table-column>
     <el-table-column
        label="负责人名称"
        prop="principalUserName"
      >
      </el-table-column>
      <el-table-column
        label="联系电话"
        prop="phone"
      >
      </el-table-column>
      <el-table-column
        label="库房地址"
        prop="depotAddress"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        prop="remark"
      >
      </el-table-column>
      <el-table-column
        label="库房类型"
        prop="depotType"
      >
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            启用
          </el-tag>
          <el-tag v-else="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <div>
          <el-button @click="getDepotLocationInfoList(row.id)" type="success" size="mini">
            查看库位信息
          </el-button>
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          </div>
          <div style="margin-top: 10px">
          <el-button @click="editStatus(row.id,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row.id,'0')" v-else="row.voidFlag==1" size="mini">
            启用
          </el-button>
          </div>


        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getDepotInfo" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="70%">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="库房名称">
          <el-input v-model="form.depotName"></el-input>
        </el-form-item>
        <el-form-item label="负责人">
          <el-select v-model="form.principalUserId" filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.userName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="库房地址">
          <el-input v-model="form.depotAddress"></el-input>
        </el-form-item>
        <el-form-item label="库房类型">
          <el-select v-model="form.depotType" filterable placeholder="请选择">
            <el-option
              v-for="item in depotTypeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" :rows="2" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
<!--    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :menuId="menuId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>-->
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
  import {getDepotInfoList,addDepotInfo,getDepotInfoById,updateDepotInfo,getAllOperatorInfo} from '@/api/baseInfo/depot/depotInfo.js'
  import Cancledialog from '@/components/Dialog/Cancledialog.vue'

const form = {
  depotName:'',
  phone:'',
  depotAddress:'',
  remark:'',
  depotType:'',
  principalUserId:'',
}
  export default {
    components: {
      Cancledialog
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          0: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        dialogStatus: 1,//1增加，2修改
        dialogTitle: '',
        dialogVisible: false,
        search: {
          keyWord:''
        },
        form:Object.assign({}, form),
        listData:{
          total:0,
          page: 1,
          pageSize:10
        },
        options:[],
        depotTypeList:[
          {id:'1',typeName:'辅料'},
          {id:'2',typeName:'纸张'},
        ],
        confirmdialog:false,
        listLoading: false,
        tableData: [],
        menuId:''
      }
    },
    mounted() {
      this.getOperatorInfo();
      this.getDepotInfo();
    },
    methods: {
      //获取列表
      getDepotInfo() {
        const _that = this
        _that.listLoading = true
        var params = _that.search;
        var page = {
          pageIndex:_that.listData.page,
          pageSize: _that.listData.pageSize,
        }
        var data = {
          "params": JSON.stringify(params),
          "page": JSON.stringify(page),
        }
        // console.log(data);
        getDepotInfoList(data).then(response => {
          console.log(response)
          const data = response.data
          // //  组合表单数据
          _that.tableData = data

          _that.listData.total = response.total
          // console.log(_that.tableData)
          _that.listLoading = false
        }).catch(err => {
          _that.listLoading = false
        })
      },
      //增加机型档案
      handleCreateAdd(){
        //增加系统目录
        const _that = this
        _that.form={
          depotName:'',
          phone:'',
          depotAddress:'',
          remark:'',
          depotType:'',
          principalUserId:'',
        }
        _that.dialogTitle = '增加库房'
        _that.dialogStatus = '1';
        _that.dialogVisible = true;

      },
      //刷新变革
      tableRefresh(){
        //刷新变革
        this.getDepotInfo()
      },
      //增加机型档案
      addBtn(){
        const _that = this

        _that.options.forEach(function (item,index) {
          if(item.id == _that.form.principalUserId){
            _that.form.principalUserName=item.userName
          }
        })
        var depotInfo =JSON.stringify(_that.form)

        var data = {
          "depotInfo": depotInfo ,
        }
        // console.log(data);
        // return

        addDepotInfo(data).then(res => {
          // console.log(res);
          if(res.httpCode == 200){
            _that.dialogVisible = false
            _that.$message({
              message: '增加成功',
              type: 'success'
            });
            _that.tableRefresh();
          }else{
            _that.$message.error(res.msg)
          }
        }).catch(err => {
          _that.$message.error(err);
          _that.dialogVisible = true
        })
      },

      //唤起修改
      handleUpdate(row){
        const _that = this;
        _that.form=Object.assign({}, form),

        _that.dialogTitle = '修改库房'
        _that.dialogVisible = true
        _that.dialogStatus = '2';

        var idPrint = row.id
        var data= {
          id : idPrint
        }

        getDepotInfoById(data).then(res => {
          // console.log(res);
          const data = res.data

          _that.form={
            id:data.id,
            depotName:data.depotName,
            phone:data.phone,
            depotAddress:data.depotAddress,
            remark:data.remark,
            depotType:data.depotType,
            principalUserId:data.principalUserId,
          }
        }).catch(err => {
          _that.$message.error(err)
        })
      },
      //修改库房
      editBtn(){
        const _that = this
        _that.options.forEach(function (item,index) {
          if(item.id == _that.form.principalUserId){
            _that.form.principalUserName=item.userName
          }
        })
        var depotInfo =JSON.stringify(_that.form)
        var data = {
          "depotInfo": depotInfo ,
        }

        // console.log(data);
        // return
        updateDepotInfo(data).then(res => {
          // console.log(res);
          if(res.httpCode == 200){
            _that.dialogVisible = false
            _that.$message({
              message: '修改成功',
              type: 'success'
            });
            _that.tableRefresh();
          }else{
            _that.$message.error(res.msg)
          }
        }).catch(err => {
          _that.$message.error(err)
          _that.dialogVisible = true
        })

      },

      //启用禁用
      editStatus(id,status){
        var _that = this;
        var printerTypeInfo = {
          id:id,
          voidFlag:status
        }
        var params = {
          "printerTypeInfo": JSON.stringify(printerTypeInfo),
        }

        var url = '/baseinfo/voidDepotInfo';
        _that.$refs.confirmDialog.updataClick(params, url);
      },


      //搜索
      searchData(){
        this.listData.page = 1;
        this.getDepotInfo()
      },

      //查看库位档案
      getDepotLocationInfoList(id){
        this.$router.push({path:'/baseInfo/depotLocationInfo',query:{'id':id}})
      },

      //获取所有负责人
      getOperatorInfo(){
        getAllOperatorInfo().then(res => {
          console.log(res);
          var _that = this
          const data = res.data
          _that.options = data

        }).catch(err => {
          _that.$message.error(err)
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .filter-container {
    margin-bottom: 15px;
  }
</style>
