<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="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="审批状态">
              <j-dict-select-tag placeholder="请选择审批状态" v-model="queryParam.demandStatus"
                dictCode="procurement_approval_status" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="采购类别">
              <j-dict-select-tag placeholder="请选择审批状态" v-model="queryParam.procurementCategory"
                dictCode="purchasing_categories" />
            </a-form-item>
          </a-col>
          <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>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <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"
      @change="handleTableChange">

      <template slot="htmlSlot" slot-scope="text, record, index">
        <a-button @click="handleButtonClick(record)">详情</a-button>
      </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" v-if="record.isReceiving == 0">
        <a-button v-if="record.demandStatus == 3" type="primary" @click="pass(record)">通过</a-button>
        <a-button v-else-if="record.demandStatus == 2" type="primary" @click="openNoPassModal(record)">不通过</a-button>
        <div v-else>
          <a-button type="primary" @click="pass(record)">通过</a-button>
          <a-divider type="vertical" />
          <a-button type="primary" @click="openNoPassModal(record)">不通过</a-button>
        </div>
      </span>
      <span v-else>
        <a>已被接单,不可修改</a>
      </span>

    </a-table>
    <a-modal title="采购内容详情" :visible="see" @ok="handleOk" @cancel="handleOk">
      <a-table :columns="contentColumns" :dataSource="content" rowKey="name"></a-table>
    </a-modal>

    <a-modal title="不通过原因" :visible="noPassSee" @ok="noPassOk" @cancel="noPassCancel">
      <a-textarea v-model="tempRecord.auditResults" placeholder="请输入不通过原因" />
    </a-modal>

    <purchase-request-modal ref="modalForm" @ok="modalFormOk"></purchase-request-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import PurchaseRequestModal from './modules/PurchaseRequestModal.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { postAction } from '@/api/manage'
import { USER_INFO } from "@/store/mutation-types"
import Vue from 'vue'

export default {
  name: 'PurchaseRequestList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    PurchaseRequestModal
  },
  data() {
    return {
      description: '采购表管理页面',
      // 表头
      see: false,
      noPassSee: false,
      columns: [
        {
          title: '审批状态',
          align: "center",
          dataIndex: 'demandStatus_dictText'
        },
        {
          title: '审核结果',
          align: "center",
          dataIndex: 'auditResults'
        },
        {
          title: '申请人',
          align: "center",
          dataIndex: 'createBy_dictText'
        },
        {
          title: '申请日期',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '需求编号',
          align: "center",
          dataIndex: 'requirementNumber',
          customRender: (text, record, index) => {
            //如果为空则不加前缀
            if (text == null || text == '') {
              return ''
            }
            return 'HL-' + text
          }
        },
        {
          title: '申请部门',
          align: "center",
          dataIndex: 'sysOrgCode'
        },
        {
          title: '采购类别',
          align: "center",
          dataIndex: 'procurementCategory_dictText'
        },
        {
          title: '采购方向',
          align: "center",
          dataIndex: 'procurementDirection'
        },
        {
          title: '采购内容',
          align: 'center',
          dataIndex: 'procurementContent',
          scopedSlots: { customRender: 'htmlSlot' }
        },
        {
          title: '采购预算',
          align: "center",
          dataIndex: 'procurementBudget'
        },
        {
          title: '附件',
          align: "center",
          dataIndex: 'annex',
          scopedSlots: { customRender: 'fileSlot' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/Try/purchaseRequest/list",
        edit: "/Try/purchaseRequest/edit",
        delete: "/Try/purchaseRequest/delete",
        deleteBatch: "/Try/purchaseRequest/deleteBatch",
        exportXlsUrl: "/Try/purchaseRequest/exportXls",
        importExcelUrl: "Try/purchaseRequest/importExcel",
      },
      dictOptions: {},
      superFieldList: [],
      content: [
        {
          name: '',
          number: '',
          unit: '',
        },
      ],
      contentColumns: [
        {
          title: '名称',
          dataIndex: 'name',
          key: 'name',
          width: 200
        },
        {
          title: '数量',
          dataIndex: 'number',
          key: 'number',
          width: 80
        },
        {
          title: '单位',
          dataIndex: 'unit',
          key: 'unit',
          width: 80
        }
      ],
      //暂存表单
      tempRecord: {},
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig() {
    },
    openNoPassModal(record) {
      this.noPassSee = true
      this.tempRecord = Object.assign({}, record);
      this.tempRecord.auditResults = '';
    },
    noPassOk() {
      this.noPassSee = false
      //保存的逻辑
      let record = Object.assign({}, this.tempRecord);
      record.demandStatus = 3;
      record.approvedBy = Vue.ls.get(USER_INFO).username;
      record.auditResults = this.tempRecord.auditResults;
      postAction(this.url.edit, record).then(res => {
        if (res.success) {
          this.$message.success('重新审批成功');
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      })
    },
    noPassCancel() {
      this.noPassSee = false
    },
    handleOk() {
      this.see = false
    },
    handleButtonClick(record) {
      this.see = true
      if (record.procurementContent == null || record.procurementContent == '') {
        this.content = {
          name: '',
          number: '',
          unit: '',
        }
      } else {
        this.content = JSON.parse(record.procurementContent)
      }
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({ type: 'sel_user', value: 'createBy', text: '申请人' })
      fieldList.push({ type: 'datetime', value: 'createTime', text: '申请日期' })
      fieldList.push({ type: 'string', value: 'requirementNumber', text: '需求编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sysOrgCode', text: '申请部门', dictCode: '' })
      fieldList.push({ type: 'int', value: 'demandStatus', text: '需求状态', dictCode: 'procurement_approval_status' })
      fieldList.push({ type: 'sel_user', value: 'updateBy', text: '审批人' })
      fieldList.push({ type: 'datetime', value: 'updateTime', text: '更新日期' })
      fieldList.push({ type: 'int', value: 'procurementCategory', text: '采购类别', dictCode: 'purchasing_categories' })
      fieldList.push({ type: 'string', value: 'procurementDirection', text: '采购方向', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'procurementContent', text: '采购内容', dictCode: '' })
      fieldList.push({ type: 'string', value: 'procurementBudget', text: '采购预算', dictCode: '' })
      fieldList.push({ type: 'string', value: 'annex', text: '附件', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'auditResults', text: '审核结果', dictCode: '' })
      this.superFieldList = fieldList
    },
    pass(record) {
      record.demandStatus = 2;
      record.approvedBy = Vue.ls.get(USER_INFO).username;
      record.auditResults = '通过';
      postAction(this.url.edit, record).then(res => {
        if (res.success) {
          this.$message.success('重新提交成功');
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>