<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" size="mini">
        <el-form-item label="分厂:">
          <el-select v-model="search.idCompany">
            <el-option
              v-for="(item,index) in companyList"
              :key="index"
              :value="item.id"
              :label="item.companyName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库房:">
          <el-select v-model="search.idStockRoom">
            <el-option
              v-for="(item,index) in stockRoomList"
              :key="index"
              :value="item.id"
              :label="item.roomName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="materialTypeIdList" filterable multiple placeholder="请选择" style="width: 260px;">
            <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.materialTypeName"
                :value="item.id.toString()">
            </el-option>
          </el-select>
        </el-form-item>
      <el-input  size="mini" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" @keyup.enter.native="searchData" class="filter-item" />
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getPaperInfo" />
        </el-tooltip>
      </div>
      </el-form>
    </div>

    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      size="mini"
      border
      default-expand-all>
      <el-table-column
        prop="materialCode"
        width="72"
        label="辅料编码">
      </el-table-column>
      <el-table-column
        prop="materialTypeName"
        width="81"
        label="辅料分类名称">
      </el-table-column>
      <el-table-column
        prop="materialName"
        label="辅料名称">
      </el-table-column>
      <el-table-column
        width="70"
        prop="mtRule"
        label="规格">
      </el-table-column>
      <el-table-column
        width="70"
      label="库存"
      prop="stockQty"></el-table-column>
      <el-table-column label="操作" fixed="left" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="webType">
              <el-button @click="sourceType == 1? chooseThisTo(row):chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
          </template>
          <template v-else>
            <el-button v-if="ERPChooseVisible" @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
            <el-button v-else @click="checkAccessories(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </template>
      </el-table-column>

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

<script>
import {
  getStockMaterialInfoList
} from '@/api/baseInfo/accessories/accessoriesInfo.js'
import { getMaterialTypeList } from '@/api/baseInfo/accessories/accessoriesType.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import {getCompanyList} from "@/api/stock/checkStockPaper";
import {getStockRoomList} from "@/api/baseInfo/stockRoomList/stockRoomList";

const form = {
  mtRule: '',
  paperCode: '',
  mtModel: '',
  byUnit: '',
  mtUnit: '',
  seUnit: '',
  barCode: '',
  remark: '',
  materialName: '',
  materialCode: '',
  idMaterialType: '',
  materialTypeName: '',
  convertUnitByToMt: 1,
  convertUnitMtToSe: 1,
  stockPrecision: 3,
}
export default {
  name:'selectAccessories',
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    },
    materialTypeIds: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ERPChooseVisible:true,
      webType: true,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      updateVisible: false,
      search: {
        idCompany: '',
        idStockRoom: 0,
        keyWord: '',
        voidFlag: 0,
        idMaterialTypes: ''
      },
      materialTypeIdList: [],
      form: Object.assign({}, form),
      options: [],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      updateInfo: {
        id: '',
        stockPrecision: null
      },
      confirmdialog: false,
      listLoading: false,
      tableData: [],

      companyList: [], // 分厂列表
      stockRoomList: [], // 库房列表
    }
  },
  mounted() {
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
    this.getCompanyInfo()
    this.getStockRoomInfo()
    this.getMaterialTypeInfo()
    if(this.materialTypeIds == ''){
      this.materialTypeIdList = []
    }else {
      this.materialTypeIdList = this.materialTypeIds.split(',')
    }
    // console.log(this.webType)
  },
  methods: {
    // 获取列表
    getPaperInfo() {
      const _that = this
      _that.listLoading = true
      _that.search.idMaterialTypes = _that.materialTypeIdList.join(',')
      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);
      getStockMaterialInfoList(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.$message.error(err)
        _that.listLoading = false
      })
    },

    // 获取分厂列表
    getCompanyInfo() {
      const _that = this
      var data = {
      }
      // console.log(data);
      getCompanyList(data).then(response => {
        console.log(response)
        _that.companyList = response.data
        _that.search.idCompany = _that.companyList[0].id

      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 获取库房列表
    getStockRoomInfo() {
      const _that = this
      // var data = {}
      var page = {
        pageIndex: 1,
        pageSize: 100
      }
      var data = {
        'page': JSON.stringify(page)
      }
      getStockRoomList(data).then(response => {
        console.log(response)
        var data = response.data
        var all = {
          roomName: '全部',
          id: 0
        }
        data.unshift(all)
        _that.stockRoomList = data
        _that.search.idStockRoom = data[0].id
        _that.getPaperInfo()
      }).catch(err => {
        _that.$message.error(err)
      })
    },

    // 获取分类列表
    getMaterialTypeInfo() {
      const _that = this
      var data = {
        'params': JSON.stringify({
          'voidFlag': 0
        })
      }
      // console.log(data);
      getMaterialTypeList(data).then(response => {
        // console.log(response)
        const data = response.data
        _that.options = data
      }).catch(err => {

      })
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },
    chooseThis(row) {
      this.$emit('chooseAccessories', row)
    },

    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.materialName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    },
    checkAccessories(row) {
      // this.$message.error(row)
      this.$emit('getAccessories', row)
    }
  }
}
</script>

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