<template>
  <a-card :bordered="false">
    <div v-show="visible">
      <div class="table-page-search-wrapper" @keyup.enter="handleEnter">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="关键字">
                <a-input v-model.trim="queryParam.keyword" placeholder="请输入名称/类型名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8 || 24" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator" style="margin-bottom: 8px;">
        <a-button v-if="$auth('purchase-purchase-orders-add')" type="primary" icon="plus" @click="$refs.baseForm.base()">采购计划</a-button>
        <a-button v-if="$auth('purchase-purchase-orders-add')" style="margin-left: 8px" type="primary" icon="plus" @click="$refs.addSpareForm.base()">新增物料</a-button>
        <!-- <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0 && $auth('purchase-purchase-orders-del')">
          <a-menu slot="overlay">
            <a-popconfirm title="是否要删除所选数据？" @confirm="batchDelete()">
              <a-menu-item key="1"><a-icon type="delete" /><a>删除</a></a-menu-item>
            </a-popconfirm>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown> -->
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto"
      >
        <span slot="action" slot-scope="record">
          <a @click="handleView(record)">查看</a>
          <operation-button
            v-if="record.status===1"
            @click="handleEdit(record)"
          >修改</operation-button>
          <operation-button
            v-if=" record.status===1"
            @click="handlePush(record)"
          >推送</operation-button>
          <operation-button
            v-if=" record.status>=3"
            :type="2"
            title="是否要删除该条数据？"
            @confirm="batchDelete(record.yongYouId)">删除</operation-button>
          <operation-button
            v-if=" record.status==1||record.status==2"
            :type="2"
            title="是否要废弃该条数据？"
            @confirm="batchWaste(record)">废弃</operation-button>
        </span>
        <template #status="text">
          <badge :text="BaseTool.Object.getField(statusMap,text)" :status="DictCache.COLOR.LONG_YAN_PURCHASE_ORDER_STATUS[text]"/>
        </template>
      </s-table>
    </div>
    <BaseForm ref="baseForm" @ok="handleOk"/>
    <Detail ref="detail" @ok="handleOk"/>
    <AddSpareForm ref="addSpareForm" />
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getPurchaseOrderPage2, deletePurchaseOrder2, getPurchaseOrderInfo, pushPurchaseOrder, wastePurchaseOrder } from '@/api/purchase/purchase-order'
import BaseForm from './modules/BaseForm.vue'
import Detail from './modules/Detail.vue'
import AddSpareForm from '@/views/store/sparestore/modules/AddSpareForm'
export default {
  name: 'PurchaseOrderList',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    AddSpareForm,
    Detail
  },
  data () {
    return {
      // 查询参数
      queryParam: {
      },
      visible: true,
      user: this.$store.getters.userInfo,
      dollarExchangeRate: this.$store.getters.rmbRate,
      cptcodeMap: {},
      cbustypeMap: {},
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },

        {
          title: '计划名称',
          dataIndex: 'orderName',
          checked: true,
          width: 150
        },
        {
          title: '计划类型',
          dataIndex: 'type',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.typeMap, text)
          }
        },
        {
          title: '采购类型',
          dataIndex: 'cptcode',
          checked: true,
          width: 130,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.cptcodeMap, text)
          }
        },
        {
          title: '业务类型',
          dataIndex: 'cbustype',
          checked: true,
          width: 150,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.cbustypeMap, text)
          }
        },
        {
          title: '关联设备',
          dataIndex: 'positionNo',
          checked: true,
          width: 150
        },
        {
          title: '采购数量',
          dataIndex: 'totalNum',
          checked: true,
          width: 150
        },
        {
          title: '申请人',
          dataIndex: 'createdUserName',
          checked: true,
          width: 150
        },
        {
          title: '需求日期',
          dataIndex: 'needDate',
          checked: true,
          width: 150
        },
        {
          title: '计划到货日期',
          dataIndex: 'planGetDate',
          checked: true,
          width: 150
        },
        {
          title: '申请时间',
          dataIndex: 'updateTime',
          checked: true,
          width: 150
        },
        {
          title: '计划备注',
          checked: true,
          width: 100,
          dataIndex: 'remark'
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: '100px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 下拉框map
      typeMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getPurchaseOrderPage2(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys
        }
      },
      optionAlertShow: false,
      statusMap: {}
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.LONG_YAN_PURCHASE_TYPE)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.LONG_YAN_PURCHASE_ORDER_STATUS)
    this.cptcodeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.LONG_YAN_PURCHASE_ORDER_PLAN_TYPE)
    this.cbustypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.LONG_YAN_PURCHASE_BUSINESS_TYPE)
    console.log(this.cptcodeMap)
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    batchDelete (id) {
      deletePurchaseOrder2({ id }).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
      })
    },
    batchWaste (record) {
      wastePurchaseOrder(record).then(res => {
        this.$message.info('废弃成功')
        this.handleOk()
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    },
    handleEdit (record) {
      getPurchaseOrderInfo(record).then(res => {
        this.$refs.baseForm.base(res.data)
      })
    },
    handleView (record) {
      getPurchaseOrderInfo(record).then(res => {
        this.$refs.detail.base(res.data)
      })
    },
    handlePush (record) {
      pushPurchaseOrder(record).then(res => {
        this.$message.info('推送成功')
        this.handleOk()
        this.$refs.table.clearSelected()
      })
    }
  }
}
</script>
