<template>
  <div class="container">
    <button class="btn-close" :disabled="disabled" @click="toExit" />
    <div class="m-content">
      <p>{{ screenData.device_name }}</p>
    </div>
    <div class="button-wraper">
      <div v-for="(e, i) in screenData.region_points" :key="i" class="button-item">
        <p v-if="e.type === '1'" class="point_name">{{ e.name }}</p>
        <p v-if="e.type === '0'" class="point_name point_name_btn" @click="showPop('IN', e)">{{ e.name }}</p>
        <p class="point_value">{{ e.device_code }}</p>
        <div v-if="e.type === '1'" class="button" @click="showPop('CONTAINER', e)">CONTAINER</div>
        <div v-if="e.type === '1'" class="button" @click="showPop('SCANER', e)">SCANER</div>
        <div v-if="e.type === '0'" class="button" @click="showPop('BACK', e)">BACK</div>
      </div>
    </div>
    <div v-show="type === 'IN'" class="pop-wraper" :class="{'popshow': show, 'pophide': !show}">
      <div class="pop-grid">
        <el-row type="flex" justify="space-between">
          <el-col :span="13">
            <el-table
              :data="popList"
              style="width: 100%"
              @current-change="handleCurrentChange"
            >
              <el-table-column
                prop="vehicle_code"
                label="载具号"
              />
              <el-table-column
                prop="order_code"
                label="订单号"
              />
              <el-table-column
                prop="region_code"
                label="工序"
              />
              <el-table-column
                prop="material_qty"
                label="物料数量"
              />
              <el-table-column
                prop="due_date"
                label="交期时间"
              />
            </el-table>
          </el-col>
          <el-col :span="10">
            <el-table
              :data="popSecList"
              style="width: 100%"
            >
              <el-table-column
                prop="order_code"
                label="订单号"
              />
              <el-table-column
                prop="due_date"
                label="交期时间"
              />
              <el-table-column
                prop="material_qty"
                label="物料数量"
              />
              <el-table-column
                prop="region_code"
                label="工序"
              />
            </el-table>
          </el-col>
        </el-row>
      </div>
      <el-row type="flex" justify="space-around">
        <el-col :span="4">
          <button class="login_button login_button_dis" @click="show = false">取消</button>
        </el-col>
        <el-col :span="4">
          <button class="login_button" :disabled="disabled" @click="toSure">叫料</button>
        </el-col>
      </el-row>
    </div>
    <div v-show="type === 'CONTAINER'" class="pop-wraper pop-wraper-1" :class="{'popshow': show, 'pophide': !show}">
      <div class="pop-h1">{{ popData.title }}</div>
      <div class="pop-grid">
        <el-row class="filter-wraper" type="flex" justify="space-between">
          <el-col :span="5" class="p-label">托盘类型</el-col>
          <el-col :span="18" class="select-wraper">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                class="option-wraper"
              />
            </el-select>
          </el-col>
        </el-row>
      </div>
      <el-row type="flex" justify="space-around">
        <el-col :span="6">
          <button class="login_button login_button_dis" @click="show = false">取消</button>
        </el-col>
        <el-col :span="6">
          <button class="login_button" @click="toSure">确定</button>
        </el-col>
      </el-row>
    </div>
    <div v-show="type === 'SCANER'" class="pop-wraper" :class="{'popshow': show, 'pophide': !show}">
      <div class="pop-grid">
        <el-table
          :data="popList"
          style="width: 100%"
        >
          <el-table-column
            label="订单号"
          >
            <template slot-scope="scope">
              <input v-model="scope.row.order_code" type="text" class="set-input">
            </template>
          </el-table-column>
          <el-table-column
            label="数量"
          >
            <template slot-scope="scope">
              <input v-model="scope.row.material_qty" type="text" class="set-input">
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
          >
            <template>
              <el-button circle type="primary" icon="el-icon-plus" @click="addRow" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-row type="flex" justify="space-around">
        <el-col :span="4">
          <button class="login_button login_button_dis" @click="show = false">取消</button>
        </el-col>
        <el-col :span="4">
          <button class="login_button" :disabled="disabled" @click="toSure">确定</button>
        </el-col>
      </el-row>
    </div>
    <div v-show="type === 'BACK'" class="pop-wraper" :class="{'popshow': show, 'pophide': !show}">
      <div class="pop-grid">
        <el-table
          :data="popList"
          style="width: 100%"
        >
          <el-table-column
            prop="order_code"
            label="订单号"
          />
          <el-table-column
            label="数量"
          >
            <template slot-scope="scope">
              <input v-model="scope.row.material_qty" type="text" class="set-input">
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-row type="flex" justify="space-around">
        <el-col :span="4">
          <button class="login_button login_button_dis" @click="show = false">取消</button>
        </el-col>
        <el-col :span="4">
          <button class="login_button" :disabled="disabled" @click="toSure">确定</button>
        </el-col>
      </el-row>
    </div>
    <div v-show="show" class="modal" />
  </div>
</template>

<script>
import crudProduceScreen from './produceScreen'
export default {
  data() {
    return {
      show: false,
      type: null,
      popData: {},
      popList: [],
      popSecList: [],
      currentRow: null,
      value: '',
      options: [{ value: 'G01', label: '钢托盘' }, { value: 'R01', label: '990mm笼框' }, { value: 'R02', label: '450mm 笼框' }, { value: 'S04', label: '1670mm料架' }, { value: 'S06', label: '1700mm料架' }],
      disabled: false
    }
  },
  computed: {
    screenData() {
      let res = {}
      if (this.$store.state.produceScreen.screenData !== '') {
        res = JSON.parse(this.$store.state.produceScreen.screenData)
      }
      return res
    }
  },
  methods: {
    toExit() {
      this.disabled = true
      crudProduceScreen.loginOut({ username: this.screenData.username, device_code: this.screenData.device_code }).then(res => {
        this.disabled = false
        localStorage.removeItem('screenData')
        this.$router.push('/produceScreen/setup')
      }).catch(() => {
        this.disabled = false
      })
    },
    showPop(type, e) {
      this.popData = {}
      this.popList = []
      this.popSecList = []
      this.currentRow = null
      this.value = ''
      this.type = type
      this.popData = e
      if (type === 'IN') {
        crudProduceScreen.regionOrder(this.screenData.device_code, this.popData.device_code).then(res => {
          this.popList = [...res.content]
          this.show = true
        })
      } else if (type === 'CONTAINER') {
        this.popData.title = '选择托盘类型'
        this.emptyObj = {}
        this.val1 = ''
        this.show = true
        this.emptyObj.device_code = e.name
      } else if (type === 'SCANER') {
        this.popList.push({ order_code: '', material_qty: '' })
        this.show = true
      } else if (type === 'BACK') {
        crudProduceScreen.fabOrders({ device_code: this.popData.device_code }).then(res => {
          this.popList = [...res.content]
          this.show = true
        })
      }
    },
    handleCurrentChange(val) {
      this.currentRow = val
      crudProduceScreen.fabMaterial({ vehicle_code: this.currentRow.vehicle_code }).then(res => {
        this.popSecList = [...res.content]
      })
    },
    toSure() {
      this.disabled = true
      if (this.type === 'IN') {
        if (this.popSecList.length === 0) {
          this.disabled = false
          return
        }
        crudProduceScreen.callMater({ mater: this.popSecList, device_code: this.popData.device_code, vehicle_code: this.currentRow.vehicle_code }).then(res => {
          this.disabled = false
          this.$message(res.msg)
        }).catch(() => {
          this.disabled = false
        })
      } else if (this.type === 'CONTAINER') {
        if (!this.value) {
          this.disabled = false
          this.$message.error('请选择托盘类型!')
          return
        }
        crudProduceScreen.callEmp({ vehicle_type: this.value, device_code: this.popData.device_code }).then(res => {
          this.disabled = false
          this.$message(res.msg)
        }).catch(() => {
          this.disabled = false
        })
      } else if (this.type === 'SCANER') {
        crudProduceScreen.sendMater({ device_code: this.popData.device_code, mater: this.popList }).then(res => {
          this.disabled = false
          this.$message(res.msg)
        }).catch(() => {
          this.disabled = false
        })
      } else if (this.type === 'BACK') {
        crudProduceScreen.sendVehicle({ mater: this.popList, device_code: this.popData.device_code }).then(res => {
          this.$message(res.msg)
        }).catch(() => {
          this.disabled = false
        })
      }
      this.show = false
    },
    addRow() {
      this.popList.push({ order_code: '', material_qty: '' })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import './style.scss';
</style>