<template>

  <div class="app-container sign-and-weigh-box">


    <div class="filter-container">


      <el-row style="float:right;">
        <el-button @click="handleWarehouse" type="primary">新增</el-button>
      </el-row>
    </div>

    <div class="filter-container" style="margin-top:20px">
      <el-input style="width: 300px;" class="filter-item" placeholder="单据日期/单据编码/制单人/供应商"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
    </div>

    <div class="filter-container">
      <span>入库时间:</span>
      <el-date-picker v-model="start_time" prefix-icon="el-icon-date" :editable="false" style="width: 196px;"
                      type="date" placeholder="选择日期时间" align="right" format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd" @change="startTimeChange"></el-date-picker>
      <span class="">-</span>
      <el-date-picker v-model="end_time" prefix-icon="el-icon-date" :editable="false" style="width: 196px;"
                      type="date" placeholder="选择日期时间" align="right" format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd" @change="endTimeChange"></el-date-picker>
    </div>

    <div class="filter-container" style="margin-top: 10px">
      <el-checkbox style="width: 30px" v-model="checked" @change="changeAllSelected">全选</el-checkbox>
      <el-button size="small" icon="el-icon-delete" @click="batchDelete">删除</el-button>

    </div>

    <el-row :gutter="12" style="margin-top: 10px">
      <el-table
        :data="Warehouse.warehouseDate"
        :class="signAndWeighBoxPatients"
        style="width: 100%" border
        highlight-current-row
        v-loading="Warehouse.loading"
        ref="multipleTable"
        @selection-change="select"

      >
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>

        <el-table-column label="单据日期" align="center">
          <template slot-scope="scope">
            {{ scope.row.warehousing_time | parseTime('{y}-{m}-{d}')}}
          </template>
        </el-table-column>

        <el-table-column label="单据编号" align="center">
          <template slot-scope="scope">
            {{scope.row.warehousing_order}}
          </template>
        </el-table-column>

        <el-table-column label="制单人" align="center">
          <template slot-scope="scope">
            {{getXuserName(scope.row.creater)}}
          </template>
        </el-table-column>


        <el-table-column label="厂家" align="center">
          <template slot-scope="scope">
            {{getManufactuerName(scope.row.manufacturer)}}
          </template>
        </el-table-column>

        <el-table-column label="经销商" align="center">
          <template slot-scope="scope">
            {{getDealerName(scope.row.dealer)}}
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)">
            </el-button>

            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)">
            </el-button>

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

      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[7]"
        :page-size="7"
        background
        style="margin-top:20px;float: right"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-row>
  </div>
</template>

<script>
  import { uParseTime } from '@/utils/tools'
  import { fetchAllAdminUsers, fetchAllDoctorAndNurse } from '@/api/doctor'
  import {
    deleteWarehouseInfo,
    GetAllConfig,
    GetAllGoodInfo,
    GetAllGoodType,
    getWarehouseInfoList,
    getWarehouseList
  } from '@/api/stock'

  export default {
    name: 'otherStockInOrder',
    created() {
      var year = new Date().getFullYear()
      var month = new Date().getMonth() + 1
      var day = new Date().getDate()
      if (parseInt(month) < 10) {
        month = '0' + month
      }
      if (parseInt(day) < 10) {
        day = '0' + day
      }
      const endTime = year + '-' + month + '-' + day
      this.end_time = endTime

      var year = new Date().getFullYear()
      var month = new Date().getMonth()
      var day = new Date().getDate()
      if (parseInt(month) < 10) {
        month = '0' + month
      }
      if (parseInt(day) < 10) {
        day = '0' + day
      }
      const startTime = year + '-' + month + '-' + day
      this.start_time = startTime
      this.GetWarehouse()
      this.GetConfigInfo()
      this.fetchAllAdminUsers()
    },
    data() {
      return {
        page: 1,
        limit: 7,
        checked: false,
        total: 0,
        pageTotal: 0,
        pageSelect: 0,
        adminUserOptions: [],
        multipleSelection: [],
        signAndWeighBoxPatients: 'sign-and-weigh-box-patients',
        start_time: '',
        end_time: '',
        goodType: [],
        goodInfo: [],
        manufacturer: [],
        selectedTableData: [],
        dealer: [],
        Warehouse: {
          loading: false,
          warehouseDate: [],
          tableCurrentIndex: ''
        },

        WarehouseInfo: {
          loading: false,
          warehouseInfoDate: []
        }
      }
    },
    methods: {
      GetWarehouse: function() {
        const Params = {
          page: this.page,
          limit: this.limit,
          start_time: this.start_time,
          end_time: this.end_time
        }
        this.Warehouse.warehouseDate = []
        getWarehouseList(Params).then(response => {
          if (response.data.state == 0) {
            this.Warehouse.loading = false
            this.$message.error(response.data.msg)
            return false
          } else {
            this.Warehouse.loading = false
            this.total = response.data.data.total
            for (let i = 0; i < response.data.data.list.length; i++) {
              this.Warehouse.warehouseDate.push(response.data.data.list[i])
            }
          }
        })
      }, tableRowClassName({ row, rowIndex }) {
        // 把每一行的索引放进row
        row.index = rowIndex
      },
      onRowClick(row, event, column) {
        this.WarehouseInfo.warehouseInfoDate = []
        this.Warehouse.tableCurrentIndex = row.index
        const params = {
          'id': row.id
        }
        this.WarehouseInfo.loading = true
        getWarehouseInfoList(params).then(response => {
          if (response.data.state == 0) {
            this.WarehouseInfo.loading = false
            this.$message.error(response.data.msg)
            return false
          } else {
            this.WarehouseInfo.loading = false
            for (let i = 0; i < response.data.data.info.length; i++) {
              this.WarehouseInfo.warehouseInfoDate.push(response.data.data.info[i])
            }
          }
        })
      }, getXuserName(id) {
        if (id <= 0) {
          return ''
        }
        var name = ''
        if (this.adminUserOptions == null || typeof (this.adminUserOptions.length) === 'undefined') {
          return name
        }
        var leng = this.adminUserOptions.length
        if (leng == 0) {
          return name
        }
        for (let index = 0; index < leng; index++) {
          if (this.adminUserOptions[index].id == id) {
            name = this.adminUserOptions[index].name
            break
          }
        }
        return name
      }, fetchAllDoctorAndNurse() {
        fetchAllDoctorAndNurse().then(response => {
          if (response.data.state == 1) {
            this.doctorOptions = response.data.data.doctors
          }
        })
      },
      fetchAllAdminUsers() {
        fetchAllAdminUsers().then(response => {
          console.log(response)
          if (response.data.state == 1) {
            this.adminUserOptions = response.data.data.users
            var alen = this.adminUserOptions.length
            for (let index = 0; index < alen; index++) {
              if (this.adminUserOptions[index].user_type == 2) {
                // this.doctorOptions.push(this.adminUserOptions[index]);
              }
            }
          }
        })
      }, getSpecificationName: function(id) {
        let name = ''
        for (let i = 0; i < this.goodInfo.length; i++) {
          if (this.goodInfo[i].id == id) {
            name = this.goodInfo[i].specification_name
          }
        }

        return name
      }, getTypeName: function(id) {
        let name = ''
        for (let i = 0; i < this.goodType.length; i++) {
          if (this.goodType[i].id == id) {
            name = this.goodType[i].type_name
          }
        }
        return name
      }, GetAllGoodType: function() {
        GetAllGoodType().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            for (let i = 0; i < response.data.data.goodType.length; i++) {
              this.goodType.push(response.data.data.goodType[i])
            }
          }
        })
      }, GetAllGoodInfo: function() {
        GetAllGoodInfo().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            for (let i = 0; i < response.data.data.goodInfo.length; i++) {
              this.goodInfo.push(response.data.data.goodInfo[i])
            }
          }
        })
      }, clicks: function() {
        console.log(this.WarehouseInfo.warehouseInfoDate)
      }, handleWarehouse: function() {
        this.$router.push({ path: '/stock/in/add' })
      }, handleReturnSales: function() {
        // this.$router.push({path:'/patients/create'}
        if (this.multipleSelection.length <= 0) {
          this.$message.error('请选择退货物品')
          return
        }

        const ids = []
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id)
        }
        this.$router.push({ name: 'salesReturn', query: { id: ids.join('&') }})
      }, handleReturnSalesRecord: function() {
        this.$router.push({ name: 'salesReutrnRecord' })
      }, handleWarehouseRecord: function() {
        this.$router.push({ name: 'warehouseRecord' })
      }, handleSelectionChange: function(val) {
        this.multipleSelection = val
      }, handleSizeChange(val) {
        this.limit = val
        this.GetWarehouse()
      }, handleCurrentChange(val) {
        this.page = val
        this.GetWarehouse()
      }, startTimeChange(val) {
        this.GetWarehouse()
      }, endTimeChange(val) {
        this.GetWarehouse()
      }, calculate: function(val) {
        return Math.round(parseFloat(val) * 100) / 100
      }, GetConfigInfo: function() {
        GetAllConfig().then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
          } else {
            this.manufacturer = response.data.data.manufacturer
            this.dealer = response.data.data.dealer
          }
        })
      }, getManufactuerName: function(manufacturer_id) {
        for (let i = 0; i < this.manufacturer.length; i++) {
          if (this.manufacturer[i].id == manufacturer_id) {
            return this.manufacturer[i].manufacturer_name
          }
        }
      }, getDealerName: function(dealer_id) {
        for (let i = 0; i < this.dealer.length; i++) {
          if (this.dealer[i].id == dealer_id) {
            return this.dealer[i].dealer_name
          }
        }
      }, handleEdit: function(index, row) {
        this.$router.push({ name: 'stockInDetail', query: { id: row.id }})
      }, handleDelete: function(index, row) {
        const ids = []
        ids.push(row.id)
        const idStr = ids.join(',')

        const params = {
          ids: idStr
        }

        this.$confirm('确认删除入库单记录?', '删除入库单记录', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteWarehouseInfo(params).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg)
              return false
            } else {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              for (let i = 0; i < ids.length; i++) {
                for (let y = 0; y < this.Warehouse.warehouseDate.length; y++) {
                  if (ids[i] == this.Warehouse.warehouseDate[y].id) {
                    this.Warehouse.warehouseDate.splice(y, 1)
                  }
                }
              }
            }
          })
        }).catch(() => {
        })
      }, changeAllSelected: function(val) {
        if (val) {
          this.$refs.multipleTable.toggleAllSelection()
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      }, select(selection) {
        this.selectedTableData = selection
      }, batchDelete() {
        if (this.selectedTableData.length <= 0) {
          this.$message.error('请选择要删除的记录')
          return
        }
        const ids = []
        for (let i = 0; i < this.selectedTableData.length; i++) {
          ids.push(this.selectedTableData[i].id)
        }
        const idStr = ids.join(',')
        const params = {
          ids: idStr
        }
        this.$confirm('确认删除入库单记录?', '删除入库单记录', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteWarehouseInfo(params).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg)
              return false
            } else {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })

              for (let i = 0; i < ids.length; i++) {
                for (let y = 0; y < this.Warehouse.warehouseDate.length; y++) {
                  if (ids[i] == this.Warehouse.warehouseDate[y].id) {
                    this.Warehouse.warehouseDate.splice(y, 1)
                  }
                }
              }
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>

<style rel="stylesheet/css" lang="scss" scoped>
  .information {
    border: 1px #dcdfe6 solid;
    padding: 30px 20px 30px 20px;

  .border {
    border-bottom: 1px #dcdfe6 solid;
    margin: 0px 0 20px 0;
  }

  }

  .title {
    background: #409eff;
    height: 44px;
    line-height: 44px;
    padding: 0 0 0 10px;
    color: #fff;
    margin: 0 0 10px 0;

  }

  .edit_separater {
    border-top: 1px solid rgb(233, 233, 233);
    margin-top: 15px;
    margin-bottom: 15px;
  }

</style>

<style>
  .sign-and-weigh-box .sign-and-weigh-box-patients .cell {
    font-size: 12px;
  }

  .sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
    background: #6fb5fa;
  }

  .count {
    color: #bd2c00;

  }

</style>
