<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="!(userRole === 'service_advisor' && scope.row.ticketsWorkStatus === '03')" size="mini" type="text" icon="el-icon-edit-outline" @click="handleDispose(scope.row)">
            处理
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-refresh" v-if="scope.row.ticketStatus !== '03'" @click="handleReTake(scope.row)">
            重新指派
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-finished" v-if="userRole === 'service_advisor' && scope.row.ticketsWorkStatus === '03'">
            通知客户取车
          </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"
    />
    <TicketsShow ref="ticketsShow" :user-role="userRole"/>

    <el-dialog :title="userRole === 'service_advisor' ? '出厂检验' : '总检'" :visible.sync="dialogVisible" width="60%" v-dialogDrag append-to-body >
      <el-form :model="formData" ref="formRef" :rules="formRules" :loading="formLoading" :inline="true"
               label-width="15rem">
        <el-row :gutter="1">
          <el-col :span="24">
            <el-form-item label="描述" prop="remark">
              <el-input style="width: 30rem" type="textarea" v-model="formData.remark"
                        :autosize="{ minRows: 4, maxRows: 8}"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="1">
          <el-col :span="24">
            <el-form-item label="附件" prop="image">
              <!--              <FileUpload v-model="formData.image" />-->
              <ImageUpload v-model="formData.image"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="userRole === 'service_advisor'" @click="handleConfirm">
          确定
        </el-button>
        <el-button type="success" v-if="userRole === 'general_inspection'" @click="handleFinish">
          确定并完成工单
        </el-button>
      </div>
    </el-dialog>

    <UpdateRepair ref="updateRepair" @success="getList"/>
  </div>
</template>

<script>
import {getPageByRole, inspection, confirm} from "@/api/repair/tickets/Tickets";
import TicketsShow from "@/views/repair/tickets/Components/TicketsShow.vue";
import UpdateRepair from "@/views/repair/tickets/form/UpdateRepair.vue";

export default {
  name: "TicketFinishManager",
  components: {UpdateRepair, TicketsShow},
  props:{
    userRole: String
  },
  data(){
    return{
      queryParams:{
        pageNo: 1,
        pageSize: 10,
        ticketNo: null,
        searchTimeArray: [],
      },
      showSearch: true,
      loading: false,
      list: [],
      total: 0,
      dialogVisible: false,
      formData:{
        image: null,
        remark: null,
        id: null
      },
      formRules:{},
      formLoading:{}
    }
  },
  mounted() {
    this.getList()
  },
  methods:{
    async getList(){
      try {
        this.loading = true
        const res = await getPageByRole(this.queryParams)
        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()
    },
    handleShow(row){
      this.formData = {
        image: null,
        remark: null,
        id: null
      }
      this.$refs.ticketsShow.open(row)
    },
    handleDispose(row){
      this.formData.id = row.id
      this.dialogVisible = true
    },
    async handleFinish(){
      try {
        if (this.formData.image){
          const data = this.formData.image.split(",")
          this.formData.image = data.map(item => {
            return item.replace(process.env.VUE_APP_FILE_API, "")
          }).join(",")
        }
        await inspection(this.formData)
        this.dialogVisible = false
        this.$modal.msgSuccess("操作成功")
        await this.getList()
      }catch{}
    },
    async handleConfirm(){
      try {
        if (this.formData.image){
          const data = this.formData.image.split(",")
          this.formData.image = data.map(item => {
            return item.replace(process.env.VUE_APP_FILE_API, "")
          }).join(",")
        }
        await confirm(this.formData)
        this.dialogVisible = false
        this.$modal.msgSuccess("操作成功")
        await this.getList()
      }catch{}
    },
    handleReTake(row){
      this.$refs.updateRepair.open(row)
    }
  }
}
</script>

<style scoped lang="scss">

</style>
