<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="5" :sm="15">
            <a-form-item label="关键词">
              <a-input placeholder="请输入关键词" v-model="queryParam.explain" />
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属分类">
              <a-select :label-in-value="true"
                        v-decorator="['typeId', {rules: [{ required: true, message: '请选择所属分类' }]}]"
                        @change="selectDevice">
                <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="报修设备">
              <a-select :label-in-value="true"
                        v-decorator="['deviceId', {rules: [{ required: true, message: '请选择所属设备' }]}]"
                        @change="getDeviceName">
                <a-select-option v-for="item in deviceList" :key="item.id" :value="item.id">{{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="请选择状态">
              <a-select v-model="queryParam.status"
                        v-decorator="['status']">
                <a-select-option v-for="item in options" :key="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="故障等级">
              <a-select v-model="queryParam.repairLevel"
                        v-decorator="['repairLevel']">
                <a-select-option v-for="item in repairLevelOptions" :key="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>

          </a-col>
          <a-col :md="5" :sm="15">
            <a-form-item label="时间范围">
              <a-range-picker @change="onChange" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="getRepairList()">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button v-if="addEnable" type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
    </div>
    <a-table
      size="default"
      ref="table"
      rowKey="id"
      :columns="columns"
      :data-source="loadData"
    >
      <span slot="status" slot-scope="text">
        {{ text | statusFilter }}
      </span>
      <span slot="remark" slot-scope="text">
      <a-tag v-if="text == '5110'" color="blue">
        {{ text }}
      </a-tag>
      <a-tag v-else color="cyan">
        普通报修
      </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <a v-if="editEnabel" @click="handleView(record.id)">详情</a>
        <!--        <a v-if="editEnabel" @click="handleEdit(record)">指派</a>-->
        <!--        <a-divider type="vertical" />-->
      </span>
    </a-table>
    <repair-modal ref="modal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getRepairList, complete } from '@/api/admin/repair'
import { oneWorkerList, oneFloorList } from '@/api/admin/repair/repairStats'
import RepairModal from './modules/RepairModal.vue'
import { checkPermission } from '@/utils/permissions'
import { getRepairDeviceList } from '@/api/admin/repair/repairDevice'
import { getRepairTypeList } from '@/api/admin/repair/repairDeviceType'

export default {
  name: 'TableList',
  components: {
    STable,
    RepairModal
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      loadData: [],
      typeList: [],
      deviceList: [],
      repairLevelOptions: [{
        value: '一级',
        label: '一级'
      }, {
        value: '二级',
        label: '二级'
      }, {
        value: '三级',
        label: '三级'
      }, {
        value: '四级',
        label: '四级'
      }, {
        value: '五级',
        label: '五级'
      }
      ],
      options: [{
        value: '1',
        label: '待分配'
      }, {
        value: '3',
        label: '重新派单'
      }, {
        value: '5',
        label: '已派单'
      }, {
        value: '7',
        label: '处理中'
      }, {
        value: '9',
        label: '已完成,待评价'
      }, {
        value: '11',
        label: '已关闭'
      }, {
        value: '13',
        label: '已评价'
      }
      ],
      // 表头
      columns: [
        {
          title: '维修单号',
          dataIndex: 'sn'
        },
        {
          title: '所属分类',
          dataIndex: 'typeName'
        },
        {
          title: '设备名称',
          dataIndex: 'deviceName'
        },
        {
          title: '报修来源',
          dataIndex: 'remark',
          scopedSlots: { customRender: 'remark' }
        },
        {
          title: '工单状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '故障等级',
          dataIndex: 'repairLevel'
        },
        {
          title: '提交人',
          dataIndex: 'name'
        },
        {
          title: '报修时间',
          dataIndex: 'createTime',
          sorter: true
        },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      // loadData: parameter => {
      //   return getRepairList(Object.assign(parameter, this.queryParam))
      // },
      addEnable: checkPermission('admin:repair:add'),
      editEnabel: checkPermission('admin:repair:edit'),
      removeEnable: checkPermission('admin:repair:list')
    }
  },
  filters: {
    // 状态:1 待派单,3 重新派单,5 已派单,7 处理中, 9已完成 待评价, 11 已关闭 13 已评价
    statusFilter (status) {
      const statusMap = {
        '1': '待分配',
        '3': '重新派单',
        '5': '已派单',
        '7': '处理中',
        '9': '已完成,待评价',
        '11': '已关闭',
        '13': '已评价'
      }
      return statusMap[status]
    }
  },
  created () {
    this.getRepairList()
    this.selectType()
  },
  methods: {
    onChange (value, dateString) {
      this.queryParam.startTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
    selectType () {
      getRepairTypeList().then(res => {
        this.typeList = res.rows
      })
    },
    selectDevice (item) {
      getRepairDeviceList({ 'typeId': item.key }).then(res => {
        this.deviceList = res.rows
      })
      this.queryParam.typeId = item.key
      console.log(this.queryParam.typeId)
    },
    getDeviceName (item) {
      console.log(item)
      this.deviceName = item.label
      this.queryParam.deviceId = item.key
    },
    getRepairList () {
      if (this.$route.query.repairUserId != null) {
        oneWorkerList({ workerId: this.$route.query.repairUserId }).then(res => {
          this.loadData = res.rows
        })
      } else if (this.$route.query.floorId != null) {
        oneFloorList({ floorId: this.$route.query.floorId }).then(res => {
          this.loadData = res.rows
        })
      } else {
        console.log(this.deviceId)
        let datas = {
          'role': 7,
          'type': 'all',
          'repair': {
            'explain': this.queryParam.explain,
            'typeId': this.queryParam.typeId,
            'deviceId': this.queryParam.deviceId,
            'status': this.queryParam.status,
            'repairLevel': this.queryParam.repairLevel,
            'beginTime': this.queryParam.startTime,
            'endTime': this.queryParam.endTime
          }
        }
        getRepairList(datas).then(res => {
          this.loadData = res.rows
        })
      }

    },
    handleAdd () {
      this.$refs.modal.add()
    },
    handleView (repairId) {
      this.$router.push({ name: 'repairView', query: { repairId: repairId } })
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    handleComplete (id) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '确认要完成工单吗?',
        okText: '是',
        okType: 'warning',
        cancelText: '否',
        onOk () {
          complete(id).then(res => {
            if (res.code === 0) {
              _this.$message.success('操作成功')
              _this.$refs.table.refresh(true)
            } else {
              _this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          })
        },
        onCancel () {
        }
      })
    }
  },
  watch: {}
}
</script>
