<template>
  <main class="m-16">
    <div class="wrap bg_white">
      <div class="p-16">
        <el-row class="top-box">
          <el-col :span="4">
            <el-button type="primary" class="m-r-16" size="small" @click="addHoppercar">
              <i class="el-icon-circle-plus" style="margin-right:5px; font-size:14px;"></i>新增</el-button>
            <el-button type="primary" size="small" plain @click="exportHandle">
              <i class="Files"></i>导出</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form ref="form" :model="filter" label-width="80px" label-position="left" class="m-t-20">
              <el-row :gutter="10">
                <el-col :span="4">
                  <el-form-item label="位置类型" prop="">
                    <el-select v-model="filter.type" placeholder="请选择位置类型" @change="changeType">
                      <el-option label="全部" value="-1">全部</el-option>
                      <el-option v-for="(item, index) in types" :key="index" :label="item.name"
                        :value="item.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="位置状态" prop="">
                    <el-select v-model="filter.status" placeholder="请选择位置状态" @change="changeSiteStatus">
                      <el-option v-for="(item, index) in statusData" :key="index" :label="item.name"
                        :value="item.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="位置编码" prop="">
                    <el-input v-model="filter.address_code" v-on:keyup.enter="changeCode" clearable
                      @clear="clearInput('address_code')" placeholder="请输入位置编码"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="位置名称" prop="">
                    <el-input v-model="filter.address_title" v-on:keyup.enter="changeSiteName" clearable
                      @clear="clearInput('address_title')" placeholder="请输入位置名称"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-checkbox v-model="is_status" label="仅显示启用的位置" @change="changeCheckbox"></el-checkbox>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" size="small" class="m-r-16" plain @click="resetData">重置</el-button>
                  <el-button type="primary" size="small" @click="searchClick">搜索</el-button>
                </el-col>
              </el-row>
            </el-form>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table :data="tableData" style="width: auto;" border :header-cell-style="header_style"
              :row-class-name="setClassName">
              <el-table-column width="50" type="expand">
                <template #default="scope">
                  <el-table :data="scope.row.cart_data_list" style="width: 350px; margin-left: 50px;" border
                    :header-cell-style="header_style">
                    <el-table-column prop="cart_title" label="当前停放料车" width="150" align="center"></el-table-column>
                    <el-table-column prop="cart_status_name" label="料车状态" width="80" align="center"></el-table-column>
                    <el-table-column prop="" label="物料明细" width="" align="center">
                      <template #default="scope1">
                        <span class="isBlue" style="cursor: pointer;"
                          v-if="scope.row.type == 2 && scope1.row.cart_status == 2"
                          @click="details(scope1.row, scope.row.address_title)">查看明细</span>
                      </template>
                    </el-table-column>
                  </el-table>
                </template>
              </el-table-column>
              <el-table-column prop="address_title" label="位置名称" width="240" align="center" />
              <el-table-column prop="address_code" label="位置编码" width="120" align="center" />
              <el-table-column prop="type_name" label="位置类型" width="240" align="center" />
              <el-table-column prop="order_by" label="优先级" width="80" align="center" />
              <el-table-column prop="status" label="位置状态" width="80" align="center">
                <template #default="scope">
                  <span class="isGreen" v-if="scope.row.status == 0">空闲</span>
                  <span class="isYellow" v-else>占用</span>
                </template>
              </el-table-column>
              <el-table-column prop="is_empty_spot" label="空料车停放点" width="120" align="center">
                <template #default="scope">
                  <span class="isGreen" v-if="scope.row.is_empty_spot == 0">是</span>
                  <span class="isYellow" v-else>否</span>
                </template>
              </el-table-column>
              <el-table-column prop="is_stock_total" label="加入库存统计" width="120" align="center">
                <template #default="scope">
                  <span class="isGreen" v-if="scope.row.is_stock_total == 1">是</span>
                  <span class="isYellow" v-else>否</span>
                </template>
              </el-table-column>
              <el-table-column prop="cart_title" label="当前停放料车" width="200" align="center">
                <template #default="scope">{{ computedFn(scope.row.cart_title) }}</template>
              </el-table-column>
              <el-table-column prop="cart_status" label="料车状态" width="80" align="center">
                <template #default="scope">
                  <span class="isGreen" v-if="computedFn(scope.row.cart_status) == 0">空闲</span>
                  <span class="isYellow" v-else-if="computedFn(scope.row.cart_status) == 1">任务中</span>
                  <span class="isYellow" v-else-if="computedFn(scope.row.cart_status) == 2">已装载</span>
                  <span class="" v-else></span>
                </template>
              </el-table-column>
              <!-- <el-table-column prop="" label="物料明细" width="120" align="center">
                  <template #default="scope">
                    <a href="#" @click="details(scope.row)" class="isBlue" v-if="scope.row.type == '7' && scope.row.cart_status == 2">查看明细</a>
                  </template>
                </el-table-column> -->
              <el-table-column prop="is_status" label="是否启用" width="80" align="center">
                <template #default="scope">
                  <el-switch v-model="scope.row.is_status" @change="changeStatus($event, scope.row.id)"
                    :active-value="0" :inactive-value="1" active-color="#13ce66" inactive-color="#EDEFF4" />
                </template>
              </el-table-column>
              <el-table-column prop="" label="操作" align="center">
                <template #default="scope">
                  <span class="isBlue Cursor m-r-16" @click="editClick(scope.row.id)">编辑</span>
                  <span class="Cursor" @click="deleteClick(scope.row)">删除</span>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination class="pagination m-t-16" :current-page="filter.page" :page-sizes="[10, 20, 30, 40]"
              :page-size="filter.size" layout="total, sizes, prev, pager, next, jumper" :total="total"
              @size-change="changePageSize" @current-change="changePage">
            </el-pagination>
          </el-col>
        </el-row>
      </div>
    </div>
    <!--新增弹窗-->
    <el-dialog class="addDialog" v-model="dialogAdd" :title="dialogTitle" width="348px" :before-close="handleClose">
      <el-form :model="addForm" label-width="80px" label-position="left">
        <el-form-item label="位置编码">
          <el-input v-model="addForm.address_code" placeholder="请输入位置编码" :disabled="isDisable"></el-input>
        </el-form-item>
        <el-form-item label="位置名称">
          <el-input v-model="addForm.address_title" placeholder="请输入位置名称"></el-input>
        </el-form-item>
        <el-form-item label="位置类型">
          <el-select v-model="addForm.type" placeholder="请选择位置状态">
            <el-option v-for="(item, index) in types" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优先顺序">
          <el-input-number v-model="addForm.order_by" :min="1" :max="10" controls-position="right"
            @change="handleChange" />
        </el-form-item>
        <div class="">
          <el-checkbox v-model="is_spots" @change="changeSpots">空料车停放点</el-checkbox>
          <el-checkbox v-model="is_join" @change="changeJoin">加入库存统计</el-checkbox>
        </div>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button class="m-r-16" size="small" @click="handleClose">取消</el-button>
          <el-button type="primary" size="small" @click="comfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
    <!--物料明细-->
    <el-dialog v-model="dialogMate" title="物料明细" width="760px" height="600px">
      <el-row class="m-b-16">
        <el-col :span="12">位置：{{ address }}</el-col>
        <el-col :span="12">料车：{{ cart }}</el-col>
      </el-row>
      <el-table :data="materialData" style="width: auto;" max-height="500" border :header-cell-style="header_style">
        <el-table-column prop="FNumber" label="物料编码" width="140" align="center" />
        <el-table-column prop="sku" label="规格型号（sku）" width="200" align="left" />
        <el-table-column prop="product_info" label="物料名称" width="200" align="center" />
        <el-table-column prop="transport_num" label="库存数量" width="80" align="center" />
        <el-table-column prop="amount" label="库存单位" align="center" />
      </el-table>
    </el-dialog>
  </main>
</template>

<script>
import { exportExcel } from '/src/utils/exportExcel.js'
import axios from 'axios'
import { ElMessageBox } from 'element-plus'
export default {
  name: '',
  data () {
    return {
      isDisable: false,
      dialogAdd: false,
      dialogMate: false,
      dialogTitle: '添加位置',
      header_style: {
        'background-color': '#F5F7FA',
        'color': '#606266',
        'fontSize': '14px',
        'fontWeight': '600',
        'textAlign': 'center',
      },
      is_status: true,
      filter: {
        type: '-1', //-1为全部，0半生产入库上料点/成品出库下料点，1半成品入库下料点，2成品出库上料点，3半成品出库上料点，4半成品出库下料点，5生产入库上料点，6成品仓，7成品仓仓位，8AGV充电位
        status: '-1', //-1为全部，0为空闲，1为占用
        address_code: '',
        address_title: '',
        is_status: '0', //-1为全部，0为启用，1为禁用
        size: 10,
        page: 1,
        export: 0
      },
      total: 0,
      currentPage: 1,
      types: [{
        id: '0',
        name: '半生产入库上料点/成品出库下料点'
      },
      {
        id: '1',
        name: '半成品入库下料点'
      },
      {
        id: '2',
        name: '成品出库上料点'
      },
      {
        id: '3',
        name: '半成品出库上料点'
      },
      {
        id: '4',
        name: '半成品出库下料点'
      },
      {
        id: '5',
        name: '生产入库上料点'
      },
      {
        id: '6',
        name: '成品仓'
      },
      {
        id: '7',
        name: '成品仓仓位'
      },
      {
        id: '8',
        name: 'AGV充电位'
      }
      ],
      statusData: [
        {
          id: '-1',
          name: '全部'
        },
        {
          id: '0',
          name: '空闲'
        },
        {
          id: '1',
          name: '占用'
        }
      ],
      tableData: [],
      materialData: [],
      is_spots: false,
      is_join: false,
      addForm: {
        type: '',
        order_by: '1',
        address_code: '',
        address_title: '',
        is_empty_spot: '0',
        is_stock_total: '1',
        dotype: ''
      },
      address: '',
      cart: ''
    }
  },
  computed: {
    computedFn () {
      return function (data) {
        return data == null ? '' : data.split(',')[0]
      }
    }
  },
  created () {
    this.filter.is_status = this.is_status == true ? '0' : '-1'
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 列表数据
    getDataList () {
      this.$http.post('Agv/addressList', this.filter).then(res => {
        if (res.status == 200) {
          this.tableData = res.data.results
          this.total = res.data.count_limit
        }
      })
    },
    setClassName ({ row, index }) {
      if (row.cart_data_list.length <= 1) {
        return 'row-expand-cover'
      }
    },
    clearInput (key) {  //清空输入框
      this.filter[`${key}`] = ''
      this.getDataList()
    },
    // clearSelect(key) {  //清空选择框
    //   this.filter[`${key}`] = '-1'
    //   this.getDataList()
    // },
    // 重置搜索条件
    resetData () {
      this.filter = {
        type: '-1',
        status: '-1',
        address_code: '',
        address_title: '',
        is_status: '0',
        size: 10,
        page: 1
      }
      this.getDataList()
    },
    // 搜索
    searchClick () {
      this.getDataList()
    },
    // 每页条数
    changePageSize (val) {
      this.filter.size = val
      this.getDataList()
    },
    // 翻页
    changePage (val) {
      this.filter.page = val
      this.getDataList()
    },
    // 选择位置类型
    changeType (val) {
      this.filter.type = val
      this.getDataList()
    },
    // 选择位置状态
    changeSiteStatus (val) {
      this.filter.status = val
      this.getDataList()
    },
    // 搜索编码
    changeCode () {
      this.getDataList()
    },
    // 搜索位置名称
    changeSiteName () {
      this.getDataList()
    },
    // 显示启用料车
    changeCheckbox (val) {
      this.filter.is_status = val == true ? '0' : '-1'
      this.getDataList()
    },
    // 新增
    addHoppercar () {
      this.dialogAdd = true
      this.dialogTitle = '添加位置'
      this.addForm.dotype = 'add'
      this.isDisable = false
      //生成编码
      // this.$http.post('Agv/addCode', { type: 'WZ' }).then(res => {
      //   if (res.status == 200) {
      //     this.addForm.address_code = res.data.str_num
      //   }
      // })
    },
    // 是否是空物料停放点（0为是，1为否）
    changeSpots (val) {
      if (val == true) {
        this.addForm.is_empty_spot = '0'
      } else {
        this.addForm.is_empty_spot = '1'
      }
    },
    // 是否加入库存统计（0为否，1为加入）
    changeJoin (val) {
      if (val == true) {
        this.addForm.is_stock_total = '1'
      } else {
        this.addForm.is_stock_total = '0'
      }
    },
    // 确定
    comfirm () {
      this.$http.post('Agv/editAddress', this.addForm).then(res => {
        if (res.status == 200) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.dialogAdd = false
          this.resetForm()
          this.getDataList()
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    },
    resetForm () {
      this.addForm = {
        type: '',
        order_by: '1',
        address_code: '',
        address_title: '',
        is_empty_spot: '0',
        is_stock_total: '1',
        dotype: ''
      }
    },
    handleClose () {
      this.resetForm()
      this.dialogAdd = false
      this.dialogMate = false
    },
    // 修改禁用/启用状态
    changeStatus (status, id) {
      this.$http.post('Agv/editStatus', { type: 1, id: id, is_status: status }).then(res => {
        if (res.status == 200) {
          this.getDataList()
          this.$message({
            type: 'success',
            message: res.message
          })
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    },
    // 物料明细
    details (row, address_title) {
      let code = row.cart_code
      this.address = address_title
      this.cart = row.cart_title
      this.dialogMate = true
      this.$http.post('Agv/productList', { cart_code: code }).then(res => {
        if (res.status == 200) {
          this.materialData = res.data.results
        }
      })
    },
    //编辑
    editClick (id) {
      this.dialogAdd = true
      this.dialogTitle = '编辑位置'
      this.isDisable = true
      this.$http.post('Agv/detailInfo', { type: 1, id: id }).then(res => {
        if (res.status == 200) {
          let data = res.data.result
          this.addForm = data
          this.is_spots = data.is_empty_spot == '0' ? true : false
          this.is_join = data.is_stock_total == '1' ? true : false
          this.addForm = {
            dotype: 'edit',
            id: data.id,
            order_by: data.order_by,
            address_code: data.address_code,
            address_title: data.address_title,
            is_empty_spot: this.is_spots == true ? '0' : '1',
            is_stock_total: this.is_join == true ? '1' : '0',
            type: data.type + ''  //转为字符类型
          }
        }
      })
    },
    //删除
    deleteClick (row) {
      ElMessageBox.confirm('您确定要直接删除所选位置吗？该操作不可恢复，请谨慎操作！', '提示', {
        type: 'warning',
        confirmButtonText: '确定删除',
        cancelButtonText: '我点错了',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            this.$http.post('Agv/doDel', { id: row.id, type: 1 }).then(res => {
              if (res.status == 200) {
                this.getDataList()
              } else {
                this.$message({
                  type: 'error',
                  message: res.message
                })
              }
            })
          }
          done()
        }
      })
    },
    //导出
    exportHandle () {
      this.filter.export = 1
      let url = '/Agv/addressList'
      exportExcel(url, this.filter)
    }
  },
}
</script>

<style lang="scss" scoped>
@import "@/views/agv/agv.scss";

::v-deep(.el-table .row-expand-cover .cell .el-table__expand-icon) {
  display: none;
}
</style>