<template>
  <div>
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="关键字" prop="query">
        <el-input style="width: 20rem" type="text" placeholder="工单号、车牌号、联系电话" v-model="queryParams.query"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作 -->
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" width="80">
        <template scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据号" align="center" prop="no" />
      <el-table-column label="服务顾问" align="center" prop="adviserName" />
      <el-table-column label="申请人" align="center" prop="repairName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.TICKET_WARES_STATUS" v-model="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleDispose(scope.row)" icon="el-icon-edit">
            处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />

    <el-dialog title="单据处理" :visible.sync="dialogVisible" width="80%" v-dialogDrag append-to-body>
      <el-table el-table v-loading="dialogLoading" :data="items" :stripe="true" :show-overflow-tooltip="true" :row-class-name="getClass" @selection-change="handleSelect">
        <el-table-column type="selection" width="80" align="center" />
        <el-table-column label="名称" align="center" prop="waresName" :show-overflow-tooltip="true"/>
        <el-table-column label="规格" align="center" prop="wares.model" width="180"/>
        <el-table-column label="申请数量" align="center" prop="waresCount" width="180"/>
        <el-table-column label="库存数量" align="center" prop="wares.stock" width="180"/>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handlePass(false)" :disabled="selections.length === 0 || selections.filter(item => !item.isStock).length !== 0">通过选择</el-button>
        <el-button type="success" @click="handlePass(true)"
                   :disabled="this.items.filter(item => !item.isStock).length !== 0"
                   :title="this.items.filter(item => !item.isStock).length !== 0 ? '有配件库存不足':''">通过全部</el-button>
        <el-button type="primary" @click="handleCreate(false)" :disabled="selections.length === 0">选择生成采购单</el-button>
        <el-button type="primary" @click="handleCreate(true)">全部生成采购单</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getPage, pass} from "@/api/repair/tickets/TicketWares";
import {listTwItem} from "@/api/repair/tickets/TWItem";
import {createUniqueCodeByHead} from "@/utils/createUniqueCode";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "WaresItem",
  props:{
    type: Boolean,
  },
  data(){
    return{
      queryParams:{
        pageNo: 1,
        pageSize: 10,
        query: null,
        type: this.type ? "01" : "02"
      },
      showSearch: true,
      loading: false,
      list:[],
      total: 0,
      dialogVisible: false,
      items:[],
      dialogLoading: false,
      selections:[],
      formData:{}
    }
  },
  mounted() {
    this.getList()
  },
  methods:{
    // 通过 true是全部、false是选择
    async handlePass(flag){
      // 生成领料单
      this.formData.repairSo = {
        soType: "02",
        soNo: createUniqueCodeByHead("LL"),
        userId: this.formData.repairId,
        userName: this.formData.repairName,
        soTime: parseTime(Date.now(), '{y}-{m}-{d}'),
        soStatus: "04",
        purchaseType: "01"
      }
      if (flag){
        this.formData.repairSois = [...this.items.map(item => {
          return {
            soiType: '02',
            goodsId: item.waresId,
            goodsCount: item.waresCount,
          }
        })]
        this.formData.items = [...this.items.map(item => {
          return {
            id: item.id,
            waresStatus: "04"
          }
        })]
      }else {
        this.formData.repairSois = [...this.selections.map(item => {
          return {
            soiType: '02',
            goodsId: item.waresId,
            goodsCount: item.waresCount,
          }
        })]
        this.formData.items = [...this.selections.map(item => {
          return {
            id: item.id,
            waresStatus: "04"
          }
        })]
      }
      try {
        await pass(this.formData)
        this.$modal.msgSuccess("处理成功")
        await this.getList()
      }finally {
        this.dialogVisible = false
      }
    },
    // todo 生成采购 true是全部、false是选择
    async handleCreate(flag){
      if (flag){

      }else {

      }
    },
    async getList(){
      try {
        this.loading = true
        const res = await getPage(this.queryParams)
        if (res.data){
          this.list = res.data.records
          this.total = res.data.total
        }
      }finally {
        this.loading = false
      }
    },
    handleQuery(){
      this.queryParams.pageNo = 1
      this.getList()
    },
    resetQuery(){
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDispose(row){
      this.formData = {}
      this.formData = {
        ...row
      }
      this.dialogVisible = true
      try {
        this.dialogLoading = true
        const res = await listTwItem({twId: row.twId})
        this.items = res.data
        // 库存判断
        this.items.forEach(item => {
          item.isStock = item.waresCount <= item.wares.stock
        })
        // 只要未领料的
        if (this.type){
          this.items = this.items.filter(item => item.waresStatus === '02')
        }else {
          // 未领料的不要
          this.items = this.items.filter(item => item.waresStatus !== '02')
        }
      }finally {
        this.dialogLoading = false
      }
    },
    getClass(row){
      if (this.type && !row.row.isStock){
        return 'stock'
      }
      return ''
    },
    handleSelect(row){
      this.selections = row
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-table .stock td{
  background-color: #ff0000 !important; /* 红色背景 */
}
</style>
