<template>
  <div>
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="关键字" prop="ticketNo">
        <el-input style="width: 20rem" type="text" placeholder="工单号、车牌号、联系电话" v-model="queryParams.ticketNo"/>
      </el-form-item>
      <el-form-item label="时间" prop="searchTimeArray">
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          v-model="queryParams.searchTimeArray"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </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">
        <template scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="ticketNo" width="200"/>
      <el-table-column label="维修类别" align="center" prop="repairType" width="180">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.REPAIR_TYPE" v-model="scope.row.repairType"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="userName" width="180"/>
      <el-table-column label="车牌号" align="center" prop="carNo" width="180"/>
      <el-table-column label="车系" align="center" prop="carBrandName" width="180"/>
      <el-table-column label="手机号" align="center" prop="userMobile" width="180"/>
      <el-table-column label="操作" fixed="right" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)"
          >查看
          </el-button>
          <el-button v-if="scope.row.ticketsWorkStatus === '01' && (userRole === 3 || userRole === 4)" size="mini" type="text" icon="el-icon-check" @click="handleTake(scope.row)">
            接单
          </el-button>
          <el-button v-if="userRole === 2 && scope.row.ticketsWorkStatus === '01'" size="mini" type="text" icon="el-icon-check" @click="handleNotify(scope.row)">
            通知施工
          </el-button>
          <el-dropdown @command="(command) => handleCommand(command, scope.$index, scope.row)">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="!isFinish && scope.row.ticketsWorkStatus === '02'" command="handleGet" size="mini" type="text" icon="el-icon-document-add"
              >申请领料
              </el-dropdown-item>
              <el-dropdown-item v-if="isFinish" @click="handleBack(scope.row)" size="mini" type="text" icon="el-icon-document-delete"
              >申请退料
              </el-dropdown-item>
              <el-dropdown-item v-if="userRole !== 4" command="handleReTake" size="mini" type="text" icon="el-icon-refresh"
              >重新指派
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="listTickets"
    />
    <TicketsShow ref="ticketsShow"/>
    <UpdateRepair ref="updateRepair" @success="listTickets"/>
    <TWOperate ref="twOperate" @success="listTickets"/>
  </div>
</template>

<script>
import {getPageType, updateTake} from "@/api/repair/tickets/Tickets";
import TicketsShow from "@/views/repair/tickets/Components/TicketsShow.vue";
import UpdateRepair from "@/views/repair/tickets/form/UpdateRepair.vue";
import TWOperate from "@/views/repair/tickets/form/TWOperate.vue";

export default {
  name: "TicketManagerItem",
  components: {TWOperate, UpdateRepair, TicketsShow},
  props: {
    isFinish: {
      type: Boolean,
    },
    userRole: {
      type: Number,
      default: -1,
      required: true
    }
  },
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        ticketNo: null,
        searchTimeArray: [],
        isFinish: this.isFinish ? "1" : "0"
      },
      showSearch: true,
      loading: false,
      list: [],
      total: 0
    }
  },
  mounted() {
    this.listTickets()
  },
  methods: {
    async listTickets() {
      try {
        this.loading = true
        const res = await getPageType(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.listTickets()
    },
    // 重置
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 查看
    handleShow(row) {
      this.$refs.ticketsShow.open(row)
    },
    // 配件申请
    handleGet(row) {
      row = {
        ...row,
        type: true
      }
      this.$refs.twOperate.open(row)
    },
    // 接单
    async handleTake(row) {
      try {
        this.loading = true
        await this.$modal.confirm("确认接单吗？")
        await updateTake(row.id)
        this.$modal.msgSuccess("接单成功")
        await this.listTickets()
      }finally {
        this.loading = false
      }
    },
    // 退料
    handleBack(row){

    },
    // 重新指派
    handleReTake(row){
      row = {
        ...row,
        isLeads: true
      }
      this.$refs.updateRepair.open(row)
    },
    // 指派员工，通知施工
    handleNotify(row){
      this.$refs.updateRepair.open(row)
    },
    // 更多操作
    handleCommand(command, index, row){
      switch (command){
        case 'handleReTake':
          this.handleReTake(row)
          break
        case 'handleGet':
          this.handleGet(row)
          break
        default:
          break
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>
