<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" size="mini" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="ticketsId"
        header-align="center"
        align="center"
        label="工单ID">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="小车类型">
        <template slot-scope="scope">
          {{ dictData[0] | findByValue(scope.row.carType) }}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="异常类型">
        <template slot-scope="scope">
          {{ dictData[1] | findByValue(scope.row.errorType) }}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="合同编号">
        <template slot-scope="scope">
          {{ dictData[2] | findByValue(scope.row.contractId) }}
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="客户">
        <template slot-scope="scope">
          {{ dictData[3] | findByValue(scope.row.clientId) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="description"
        header-align="center"
        align="center"
        label="故障描述">
      </el-table-column>
      <el-table-column
        prop="deptPeople"
        header-align="center"
        align="center"
        min-width="90px"
        label="工单对接人">
      </el-table-column>
      <el-table-column
        prop="deptPhone"
        header-align="center"
        align="center"
        min-width="100px"
        label="客户联系电话">
      </el-table-column>
      <el-table-column
        prop="createUser"
        header-align="center"
        align="center"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        min-width="90px"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="工单状态">
        <template slot-scope="scope">
          {{ statusOpt | findByValue(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        min-width="100px"
        label="工单审批完成时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="170"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.ticketsId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.ticketsId)">删除</el-button>
          <el-button v-if="!scope.row.status" type="text" size="small" @click="startFlowHandle(scope.row.ticketsId)">指派</el-button>
          <el-button type="text" size="small" @click="$router.push(`/tickets-detail?id=${scope.row.ticketsId}`)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" :dictData="dictData" @refreshDataList="getDataList"></add-or-update>
    <el-dialog
      title="发起流程"
      :visible.sync="dialogFormVisible"
      width="30%">
      <el-form :model="flowForm"  label-width="100px" class="demo-dynamic">
        <el-form-item
          v-for="(form) in dynamiForm"
          :label="form.name"
          :key="form.key"
          :prop="form.key"
        >
          <el-select v-if="form.type == 'assignee'" v-model="flowForm[form.key]" placeholder="请选择">
            <el-option
              v-for="item in users"
              :key="item.userId"
              :label="item.nickname+'['+item.username+']'"
              :value="item.userId">
            </el-option>
          </el-select>
          <el-select v-if="form.type == 'candidateUsers'" v-model="flowForm[form.key]" placeholder="请选择">
            <el-option
              v-for="item in users"
              :key="item.userId"
              :label="item.nickname+'['+item.username+']'"
              :value="item.userId">
            </el-option>
          </el-select>
          <el-select v-if="form.type == 'candidateGroups'" v-model="flowForm[form.key]" placeholder="请选择">
            <el-option
              v-for="item in roles"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取消</el-button>
        <el-button size="mini" type="primary" @click="submitStartFlow()">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import AddOrUpdate from '../tickets/tickets-add-or-update'
  import { apiUtils } from '@/utils/dict'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dictConfigs: [{url: '/car/car/list', type: 'list', value: 'carId', label: 'carName'}, {type: 'dict', code: 'error_type'}, {url: '/flow/contract/list', type: 'list', value: 'contractId', label: 'contractNumber'}, {url: '/client/client/list', type: 'list', value: 'clientId', label: 'clientName'}],
        dictData: [],
        dialogFormVisible: false,
        flowForm:{ticketsId: null},
        dynamiForm:[],
        users: [],
        roles: [],
        statusOpt: [{value: '0', label: '未开始'}, {value: '1', label: '已指派'}, {value: '2', label: '处理中'}, {value: '3', label: '已完成'}]
      }
    },
    mixins: [apiUtils],
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/tickets/tickets/listType'),
          method: 'get',
          params: this.$http.adornParams({
            'type': 1,
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.ticketsId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/tickets/tickets/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      startFlowHandle(id){
        this.flowForm = {}
        this.flowForm.ticketsId = id
        this.$http({
          url: this.$http.adornUrl('/flow/deploy/flowDef'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dynamiForm = [...data.data]
            this.users = [...data.users]
            this.roles = [...data.roles]
            this.dialogFormVisible = true
          }
        })
      },
      submitStartFlow(){
        // 提交表单数据
        this.$http({
          url: this.$http.adornUrl(`/flw/instance/startFlowInstance`),
          method: 'get',
          params: this.$http.adornParams(this.flowForm)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.dialogFormVisible = false
                this.flowForm = {}
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
