<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="5" :md="7" :sm="24">
            <a-form-item label="工单">
              <a-input placeholder="请输入工单号" v-model="queryParam.workOrder"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="5" :md="7" :sm="24">
            <a-form-item label="作业人">
              <j-dict-select-tag v-model="queryParam.operateBy" dictCode="sys_user,realname,id" placeholder="请选择作业人"/>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="5" :md="7" :sm="24">
            <a-form-item label="审批人">
              <j-dict-select-tag v-model="queryParam.aduitBy" dictCode="sys_user,realname,id" placeholder="请选择审批人"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="8" :lg="10" :md="12" :sm="24">
              <a-form-item label="申请时间">
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"
                  class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"
                  class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-button type="primary" icon="download" @click="handleExportXls('工器具/物资申请记录')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>


      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-button type="primary" @click="handleSearchMyAduit">我的审批</a-button>
      <a-button type="primary" @click="handleSearchMyApply">我的申请</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length
          }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{ x: true }" bordered rowKey="id" :columns="columns"
        :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" class="j-table-force-nowrap"
        @expand="handleExpand" @change="handleTableChange">

        <a-table slot="expandedRowRender" slot-scope="text" v-loading="dloading" :columns="detailColumns"
          :data-source="details" :pagination="false" size="small">
        </a-table>
        <template slot="dictTagSlot" slot-scope="text">
          <j-dict-tag :text="text" dict-code="repair_range" />
        </template>
        <template slot="linkSlot" slot-scope="text">
          <a-button type="link" @click="workOrderView(text)">查看工单</a-button>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.status === 0" @click="handlePass(record)">通过</a>
          <a-divider type="vertical" />
          <a v-if="record.status === 0" style="color: red" @click="handleReject(record)">驳回</a>
        </span>

      </a-table>
    </div>

    <reject-reason-modal v-model="showReason" @ok="handleRejectApply" @cancel="handleRejectCancel" />
    <apply-record-modal ref="modalForm" @ok="modalFormOk"></apply-record-modal>
    <work-order-detail-model :order-id="workOrder" :visible="detailModelVisible"
      @close="closeDetailModel"></work-order-detail-model>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ApplyRecordModal from './modules/ApplyRecordModal'
import { getAction, postAction } from '@api/manage'
import RejectReasonModal from '@views/apply/modules/RejectReasonModal.vue'
import WorkOrderDetailModel from '@views/workOrder/components/WorkOrderDetailModel/index.vue'

export default {
  name: 'ApplyRecordList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    WorkOrderDetailModel,
    RejectReasonModal,
    ApplyRecordModal
  },
  data() {
    return {
      showReason: false,
      selectRecord: null,
      description: '工器具/物资申请记录管理页面',
      detailColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '名称',
          align: "center",
          dataIndex: 'itemName',
        },
        {
          title: '型号',
          align: "center",
          dataIndex: 'specification'
        },
        {
          title: '数量',
          align: "center",
          dataIndex: 'quantity'
        },
        {
          title: '单位',
          align: "center",
          dataIndex: 'unit_dictText'
        },
      ],
      details: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '工单',
          align: "center",
          dataIndex: 'workOrder',
          scopedSlots: { customRender: 'linkSlot' }
        },
        {
          title: '设备',
          align: "center",
          dataIndex: 'deviceId_dictText'
        },
        {
          title: '申请类型',
          align: "center",
          dataIndex: 'applyType_dictText'
        },
        {
          title: '修程',
          align: "center",
          dataIndex: 'regularId_dictText',
          scopedSlots: { customRender: 'dictTagSlot' }
        },
        {
          title: '申请状态',
          align: "center",
          dataIndex: 'status_dictText'
        },
        {
          title: '作业人',
          align: "center",
          dataIndex: 'operateBy_dictText'
        },
        {
          title: '申请时间',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '审批人',
          align: "center",
          dataIndex: 'aduitBy_dictText'
        },
        {
          title: '驳回理由',
          align: "center",
          dataIndex: 'rejectReson'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/apply/applyRecord/list",
        delete: "/apply/applyRecord/delete",
        deleteBatch: "/apply/applyRecord/deleteBatch",
        exportXlsUrl: "/apply/applyRecord/exportXls",
        importExcelUrl: "apply/applyRecord/importExcel",
        listDetails: "/apply/applyDetails/list/nopage",
        aduit: "/apply/applyRecord/aduit",
      },
      dictOptions: {},
      superFieldList: [],
      dloading: false,
      workOrder: null,
      detailModelVisible: false,
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    handleSearchMyAduit() {
      let user = this.$store.getters.userInfo;
      console.log(user)
      this.queryParam = {
        aduitBy: user.id
      }
      this.loadData();
    },
    handleSearchMyApply() {
      let user = this.$store.getters.userInfo;
      this.queryParam = {
        operateBy: user.id
      }
      this.loadData();
    },
    handleRejectApply(reason) {
      postAction(this.url.aduit, {
        applyId: this.selectRecord.id,
        status: 2,
        reason: reason
      }).then(res => {
        if (res.success) {
          this.$message.success("驳回成功！");
          this.loadData()
        } else {
          this.$message.warning(res.message);
        }
        this.showReason = false;
        this.selectRecord = null;
      })
    },
    handleRejectCancel() {
      this.showReason = false;
      this.selectRecord = null;
    },
    handleReject(record) {
      this.showReason = true;
      this.selectRecord = record;
    },
    handlePass(record) {
      postAction(this.url.aduit, {
        applyId: record.id,
        status: 1,
      }).then(res => {
        if (res.success) {
          this.$message.success("审批成功！");
          this.loadData()
        } else {
          this.$message.warning(res.message);
        }
      })
    },
    handleExpand(expand, record) {
      if (expand) {
        //加载详细信息
        this.dloading = true;
        getAction(this.url.listDetails, {
          applyId: record.id,
        }).then((res) => {
          this.details = res.result.records
          this.dloading = false;
        })
      }
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'string', value: 'workOrder', text: '工单', dictCode: '' })
      fieldList.push({ type: 'string', value: 'deviceId', text: '设备', dictCode: '' })
      fieldList.push({ type: 'int', value: 'applyType', text: '申请类型', dictCode: "apply_type,," })
      fieldList.push({ type: 'string', value: 'regularId', text: '修程', dictCode: '' })
      fieldList.push({ type: 'int', value: 'status', text: '申请状态', dictCode: "apply_status,," })
      fieldList.push({ type: 'string', value: 'aduitBy', text: '审批人', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'rejectReson', text: '驳回理由', dictCode: '' })
      this.superFieldList = fieldList
    },
    workOrderView(workOrderId) {
      this.workOrder = workOrderId;
      this.detailModelVisible = true;
    },
    closeDetailModel() {
      this.detailModelVisible = false;
      this.workOrder = null;
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>