<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input size="mini" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" />
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreateAdd">
        增加库房信息
      </el-button>
      <el-checkbox size="mini" @change="checkDisable" v-model="disableLock" label="已禁用" border></el-checkbox>
      <el-button
        v-if="!webType"
        class="filter-item"
        style="margin-left: 10px;"
        size="mini"
        type="warning"
        @click="search.keyWord='';getTableInfo()">
        重置
      </el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="webType">
            <template v-if="!isChoose">
          <el-button @click="handleUpdate(row.id,row.payMode)" type="primary" size="mini">
            编辑
          </el-button>
          <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>
            </template>
            <template v-else>
              <el-button @click="chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
          <template v-else>
            <el-button @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>

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

      <el-table-column
        prop="companyName"
        label="分厂名称">
      </el-table-column>
      <el-table-column
        prop="roomName"
        label="库房名称">
      </el-table-column>
      <el-table-column
        prop="adminPerson"
        label="负责人">
      </el-table-column>
      <el-table-column
        label="创建时间"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>
        </template>
      </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>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getTableInfo" />

    <el-dialog  :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="ruleForm" label-width="130px" :rules="rules" ref="ruleForm" label-position="right">
        <el-form-item prop="companyName" label="选择分厂">
          <el-select v-model="ruleForm.companyName" placeholder="请选择">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.companyName"
              :value="item.companyName+','+item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="roomName" label="库房名称">
          <el-input v-model="ruleForm.roomName"></el-input>
        </el-form-item>
        <el-form-item prop="adminPerson" label="负责人">
          <el-input v-model="ruleForm.adminPerson"></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('ruleForm'):editBtn('ruleForm')">确 定</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 { getStockRoomList, addInfo, updateInfo, getInfoById, getAllCompanyInfoList } from '@/api/baseInfo/stockRoomList/stockRoomList.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import { getIndustryInfo } from '@/api/doc/client/client'

export default {
  components: {
    Cancledialog, Pagination
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      disableLock: false,
      webType: true,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      companyList: [],
      search: {
        keyWord: ''
      },
      ruleForm: {
      },
      rules: {
        companyName: [
          { required: true, message: '请选择分厂', trigger: 'blur' }
        ],
        roomName: [
          { required: true, message: '请填写库房名称', trigger: 'blur' }
        ],
        adminPerson: [
          { required: true, message: '请填写负责人', trigger: 'blur' }
        ]

      },
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      confirmdialog: false,
      listLoading: false,
      tableData: [],
      menuId: ''
    }
  },
  mounted() {
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
    if (this.$route.query.sname != undefined) {
      this.search.keyWord = this.$route.query.sname
    }
    this.getTableInfo()
  },
  methods: {
    checkDisable(val) {
      // console.log(val);
      this.listData.page = 1;
      this.getTableInfo()
    },
    // 获取列表
    getTableInfo() {
      const _that = this
      _that.listLoading = true;
      if(_that.disableLock){
        _that.search.voidFlag = 1;
      }else {
        _that.search.voidFlag = 0;
      }
      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);
      getStockRoomList(data).then(response => {
        console.log('tableData', 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
      })

      // 获取分厂信息列表
      if (_that.companyList.length <= 0) {
        getAllCompanyInfoList(data).then(response => {
          console.log('companyList', response.data)
          const data = response.data
          // //  组合表单数据
          _that.companyList = response.data
        }).catch(err => {
          console.log(err)
        })
      }
    },
    // 增加库房信息
    handleCreateAdd() {
      const _that = this
      _that.ruleForm = {
        payMode: ''
      }
      _that.dialogTitle = '增加库房信息'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 增加
    addBtn(formName) {
      const _that = this
      _that.$refs[formName].validate((valid) => {
        console.log(valid)
        if (valid) {
          var dataForm = {
            idCompany: _that.ruleForm.companyName.split(',')[1],
            companyName: _that.ruleForm.companyName.split(',')[0],
            roomName: _that.ruleForm.roomName,
            adminPerson: _that.ruleForm.adminPerson
          }
          var info = JSON.stringify(dataForm)
          var data = {
            'stockRoom': info
          }

          // console.log("clientLevel",data);
          addInfo(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
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // 唤起修改
    handleUpdate(id) {
      const _that = this
      console.log(id)
      _that.dialogTitle = '修改物流档案'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      _that.byId = id
      var data = {
        stockRoomId: id
      }

      getInfoById(data).then(res => {
        console.log('getInfoById', res)
        var datas = res.data
        // console.log(datas);
        _that.ruleForm = {
          id: id,
          idCompany: datas.idCompany,
          companyName: datas.companyName,
          roomName: datas.roomName,
          adminPerson: datas.adminPerson
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改档案
    editBtn(formName) {
      const _that = this
      _that.$refs[formName].validate((valid) => {
        if (_that.ruleForm.companyName.split(',').length > 1) {
          var companyName = _that.ruleForm.companyName.split(',')[0]
          var idCompany = _that.ruleForm.companyName.split(',')[1]
        } else {
          var companyName = _that.ruleForm.companyName
          var idCompany = _that.ruleForm.idCompany
        }
        var dataForm = {
          id: _that.byId,
          idCompany: idCompany,
          companyName: companyName,
          roomName: _that.ruleForm.roomName,
          adminPerson: _that.ruleForm.adminPerson
        }
        // console.log("dataFormred",dataForm);
        var info = JSON.stringify(dataForm)
        var data = {
          'stockRoom': info
        }
        // console.log(data);

        updateInfo(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 data = {
        id: id,
        voidFlag: status
      }
      var params = {
        'stockRoom': JSON.stringify(data)
      }

      var url = '/inventory/voidStockRoom'
      _that.$refs.confirmDialog.updataClick(params, url)
    },
    // 刷新表格
    tableRefresh() {
      // 刷新变革
      this.getTableInfo()
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getTableInfo()
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('chooseStockRoom', row)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-checkbox.is-bordered.is-checked {
  border-color: #F56C6C;
}
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner,
::v-deep .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  border-color: #F56C6C !important;
  background-color: #F56C6C !important;
}

::v-deep .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #F56C6C !important;
}

::v-deep .el-checkbox__inner:hover {
  border-color: #F56C6C !important;
}

::v-deep .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: #F56C6C !important;
}

.filter-container {
  margin-bottom: 15px;
}
</style>
