<template>
  <div class="mod-config">
    <!-- 条件查询 -->
    <el-form :inline="true" label-position="top" :model="searchData">
      <el-form-item label="BU">
        <el-select v-model="searchData.buNo" placeholder="请选择" clearable style="width: 100px">
          <el-option
            v-for="i in userBuList"
            :key="i.buNo"
            :label="i.buDesc"
            :value="i.buNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签条码">
        <el-input v-model="searchData.carrierNo" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="载具类型名称">
        <el-input v-model="searchData.carrierTypeName" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchData.status" placeholder="请选择" clearable style="width: 100px">
          <el-option label="全部" value=""></el-option>
          <el-option label="空闲" value="1"></el-option>
          <el-option label="占用" value="2"></el-option>
          <el-option label="维修" value="3"></el-option>
          <el-option label="报废" value="4"></el-option>
          <el-option label="外借" value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addModal()">新增</el-button>
        <el-button type="primary" @click="exportExcel()">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- 展示列表 -->
    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column prop="buNo" label="BU" header-align="center" align="center" min-width="80"></el-table-column>
      <el-table-column prop="carrierNo" label="标签条码" header-align="center" align="left" min-width="120"></el-table-column>
      <el-table-column prop="carrierTypeCode" label="载具类型编码" header-align="center" align="center" min-width="120"></el-table-column>
      <el-table-column prop="carrierTypeName" label="载具类型名称" header-align="center" align="left" min-width="150"></el-table-column>
      <el-table-column prop="specification" label="规格描述" header-align="center" align="left" min-width="150"></el-table-column>
      <el-table-column prop="dimensions" label="尺寸" header-align="center" align="center" min-width="120"></el-table-column>
      <el-table-column prop="statusDesc" label="状态" header-align="center" align="center" min-width="80"></el-table-column>
      <el-table-column prop="currentLocation" label="当前位置" header-align="center" align="left" min-width="120"></el-table-column>
      <el-table-column prop="totalUsageCount" label="使用次数" header-align="center" align="right" min-width="100"></el-table-column>
      <el-table-column prop="createDate" label="创建时间" header-align="center" align="center" min-width="150"></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="130"
        label="操作">
        <template slot-scope="scope">
          <el-link style="cursor: pointer" @click="updateModal(scope.row)">修改</el-link>
          <el-link style="cursor: pointer" @click="deleteModal(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top: 0px"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 200, 500]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 新增和修改对话框 -->
    <el-dialog :title="modalTitle" :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="800px">
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" ref="modalForm" style="margin-left: 7px;">
        <!-- 第一行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="BU" prop="buNo">
              <el-select v-model="modalData.buNo" placeholder="请选择" :disabled="modalDisableFlag" style="width: 100%">
                <el-option
                  v-for="i in userBuList"
                  :key="i.buNo"
                  :label="i.sitename"
                  :value="i.buNo">
                  <span style="float: left">{{ i.sitename }}</span>
                  <span style="float: right; color: #8492a6; font-size: 11px">{{ i.buDesc }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签条码" prop="carrierNo">
              <el-input v-model="modalData.carrierNo" :disabled="modalDisableFlag" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        
        <!-- 第二行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="载具类型编码" prop="carrierTypeCode">
              <el-input v-model="modalData.carrierTypeCode" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="载具类型名称" prop="carrierTypeName">
              <el-input v-model="modalData.carrierTypeName" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="固定资产编号">
              <el-input v-model="modalData.assetNo" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格描述">
              <el-input v-model="modalData.specification" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第四行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="尺寸(长×宽×高mm)">
              <el-input v-model="modalData.dimensions" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大承重(kg)">
              <el-input-number v-model="modalData.maxWeight" :controls="false" :precision="2" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第五行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="预期使用次数">
              <el-input-number v-model="modalData.expectedLifeCycles" :controls="false" :precision="0" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预期使用寿命(天)">
              <el-input-number v-model="modalData.expectedLifeDays" :controls="false" :precision="0" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第六行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="modalData.status" placeholder="请选择" style="width: 100%">
                <el-option label="空闲" :value="1"></el-option>
                <el-option label="占用" :value="2"></el-option>
                <el-option label="维修" :value="3"></el-option>
                <el-option label="报废" :value="4"></el-option>
                <el-option label="外借" :value="5"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前位置">
              <el-input v-model="modalData.currentLocation" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第七行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购日期">
              <el-date-picker v-model="modalData.purchaseDate" type="date" value-format="yyyy-MM-dd" style="width: 100%"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注">
              <el-input v-model="modalData.remark" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      
      <el-footer style="height:35px;margin-top: 15px;text-align:center">
        <el-button type="primary" @click="saveData()">保存</el-button>
        <el-button type="primary" @click="modalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>
  </div>
</template>

<script>
import {
  fixedCarrierSearch,
  fixedCarrierSave,
  fixedCarrierUpdate,
  fixedCarrierDelete,
  getSiteAndBuByUserName
} from "@/api/fixedCarrier/fixedCarrier.js"
import excel from "@/utils/excel-util.js"

export default {
  data() {
    return {
      searchData: {
        site: this.$store.state.user.site,
        userName: this.$store.state.user.name,
        buNo: '',
        carrierNo: '',
        carrierTypeName: '',
        status: '',
        page: 1,
        limit: 20
      },
      pageIndex: 1,
      pageSize: 20,
      totalPage: 0,
      height: 500,
      dataList: [],
      dataListLoading: false,
      modalFlag: false,
      modalDisableFlag: false,
      modalTitle: '新增固定载具',
      modalData: {
        site: this.$store.state.user.site,
        buNo: '',
        carrierNo: '',
        carrierTypeCode: '',
        carrierTypeName: '',
        assetNo: '',
        specification: '',
        dimensions: '',
        maxWeight: null,
        expectedLifeCycles: null,
        expectedLifeDays: null,
        status: 1,
        currentLocation: '',
        purchaseDate: '',
        createBy: this.$store.state.user.name,
        remark: '',
        flag: ''
      },
      userBuList: [],
      rules: {
        buNo: [{ required: true, message: '请选择BU', trigger: 'change' }],
        carrierNo: [{ required: true, message: '请输入标签条码', trigger: 'blur' }],
        carrierTypeCode: [{ required: true, message: '请输入载具类型编码', trigger: 'blur' }],
        carrierTypeName: [{ required: true, message: '请输入载具类型名称', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  mounted() {
    this.getSiteAndBu()
    this.getDataList()
  },
  methods: {
    // 获取用户的BU
    getSiteAndBu() {
      let tempData = {
        username: this.$store.state.user.name
      }
      getSiteAndBuByUserName(tempData).then(({data}) => {
        if (data.code === 0) {
          this.userBuList = data.rows
        }
      })
    },

    // 查询列表
    getDataList() {
      this.dataListLoading = true
      this.searchData.page = this.pageIndex
      this.searchData.limit = this.pageSize
      
      fixedCarrierSearch(this.searchData).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list.map(item => {
            // 状态描述转换
            const statusMap = {
              1: '空闲',
              2: '占用',
              3: '维修',
              4: '报废',
              5: '外借'
            }
            item.statusDesc = statusMap[item.status] || ''
            return item
          })
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      }).catch(() => {
        this.dataListLoading = false
      })
    },

    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },

    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },

    // 新增
    addModal() {
      this.modalTitle = '新增固定载具'
      this.modalFlag = true
      this.modalDisableFlag = false
      this.modalData = {
        site: this.$store.state.user.site,
        buNo: '',
        carrierNo: '',
        carrierTypeCode: '',
        carrierTypeName: '',
        assetNo: '',
        specification: '',
        dimensions: '',
        maxWeight: null,
        expectedLifeCycles: null,
        expectedLifeDays: null,
        status: 1,
        currentLocation: '',
        purchaseDate: '',
        createBy: this.$store.state.user.name,
        remark: '',
        flag: 'add'
      }
      this.$nextTick(() => {
        this.$refs.modalForm && this.$refs.modalForm.clearValidate()
      })
    },

    // 修改
    updateModal(row) {
      this.modalTitle = '修改固定载具'
      this.modalFlag = true
      this.modalDisableFlag = true
      this.modalData = {
        ...row,
        flag: 'update'
      }
      this.$nextTick(() => {
        this.$refs.modalForm && this.$refs.modalForm.clearValidate()
      })
    },

    // 保存
    saveData() {
      this.$refs.modalForm.validate((valid) => {
        if (valid) {
          if (this.modalData.flag === 'add') {
            fixedCarrierSave(this.modalData).then(({data}) => {
              if (data && data.code === 0) {
                this.$message.success('新增成功')
                this.modalFlag = false
                this.getDataList()
              } else {
                this.$message.error(data.msg || '新增失败')
              }
            })
          } else {
            fixedCarrierUpdate(this.modalData).then(({data}) => {
              if (data && data.code === 0) {
                this.$message.success('修改成功')
                this.modalFlag = false
                this.getDataList()
              } else {
                this.$message.error(data.msg || '修改失败')
              }
            })
          }
        }
      })
    },

    // 删除
    deleteModal(row) {
      this.$confirm('确定删除该固定载具吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        fixedCarrierDelete(row).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('删除成功')
            this.getDataList()
          } else {
            this.$message.error(data.msg || '删除失败')
          }
        })
      }).catch(() => {})
    },

    // 导出
    exportExcel() {
      const exportData = this.dataList.map(item => ({
        'BU': item.buNo,
        '标签条码': item.carrierNo,
        '载具类型编码': item.carrierTypeCode,
        '载具类型名称': item.carrierTypeName,
        '规格描述': item.specification,
        '尺寸': item.dimensions,
        '状态': item.statusDesc,
        '当前位置': item.currentLocation,
        '使用次数': item.totalUsageCount,
        '创建时间': item.createDate
      }))
      
      excel.export_json_to_excel({
        header: ['BU', '标签条码', '载具类型编码', '载具类型名称', '规格描述', '尺寸', '状态', '当前位置', '使用次数', '创建时间'],
        data: exportData.map(item => Object.values(item)),
        filename: '固定载具_' + this.dayjs().format('YYYYMMDDHHmmss'),
        autoWidth: true,
        bookType: 'xlsx'
      })
    }
  }
}
</script>

<style scoped>
</style>



