<!-- 待办事项 -->
<template>
  <div class="container">
    <my-table
      :data="data"
      :columns="columns"
      :table-filter="tableFilter"
      :table-page="params"
      selectKey="linkId"
      @event-handle="eventHandle"
      @on-select="getSelect"
      @on-change="changePage"
    >
    </my-table>
    <purchase-form
      v-model="purchaseShow"
      :data="formData"
      @getlist="getList"
    ></purchase-form>
    <contract-form
      v-model="contractShow"
      :data="formData"
      @getlist="getList"
    ></contract-form>
    <pay-form v-model="payShow" :data="formData" @getlist="getList"></pay-form>
    <material-approve-form
      v-model="materialApproveShow"
      :data="formData"
      @getlist="getList"
    ></material-approve-form>
    <provider-price-form
      v-model="providerPriceShow"
      :data="formData"
      @getlist="getList"
    ></provider-price-form>
    <qty-form
      v-model="qtyCheckShow"
      :data="formData"
      @getlist="getList"
    ></qty-form>
    <apply-material-form
      v-model="applyMaterialShow"
      :data="formData"
      @getlist="getList"
    ></apply-material-form>
    <other-form
      v-model="otherShow"
      :data="formData"
      @getlist="getList"
    ></other-form>
    <return-form
      v-model="returnShow"
      :data="formData"
      @getlist="getList"
    ></return-form>
    <check-form
      v-model="checkShow"
      :data="formData"
      @getlist="getList"
    ></check-form>
    <new-check-form
      v-model="newCheckShow"
      :data="formData"
      @getlist="getList"
    ></new-check-form>
    <profit-form
      v-model="profitShow"
      :data="formData"
      @getlist="getList"
    ></profit-form>
    <del-contract-form
      v-model="delContractShow"
      :data="formData"
      @getlist="getList"
    ></del-contract-form>
    <no-check-form
      v-model="noCheckShow"
      :data="formData"
      @getlist="getList"
    ></no-check-form>
    <provider-contract-over-form
      v-model="providerContractOverShow"
      :data="formData"
      @getlist="getList"
    ></provider-contract-over-form>
    <provider-tag-del-form
      v-model="providerTagDelShow"
      :data="formData"
      @getlist="getList"
    ></provider-tag-del-form>
    <provider-shipment-form
      v-model="providerShipmentShow"
      :data="formData"
      @getlist="getList"
    ></provider-shipment-form>
    <profitTagForm
      v-model="profitTagFormShow"
      :data="formData"
      @getlist="getList"
    />
    <forOutbound
      v-model="forOutboundShow"
      :data="formData"
      @getlist="getList"
    />
    
    <!-- 批量审批 -->
    <modal
      v-model="dispatchShow"
      class-name="vertical-center-modal"
      width="500px"
    >
      <div class="title" slot="header">审批详情</div>
      <div class="footer" slot="footer">
        <Button @click="pass(0)" class="btn" type="primary" size="small"
          >通过</Button
        >
        <Button @click="pass(1)" class="btn">驳回</Button>
      </div>
      <div class="sub-content">
        <div class="wrap">
          <div class="row">
            <div class="col">
              <div class="label" style="font-size: 14px;">审核意见：</div>
              <div class="value">
                <Input v-model="suggestions" type="textarea" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </modal>
  </div>
</template>

<script>
import { GetDealWithItem } from '@/api/user'
import {
  GetApplyTakePageList,
  GetApplyStockOutBill,
  GetApplyStockInBackRequestDto,
  GetStockExemptCheck
} from '@/api/stock'
import {
  ApprovalBillFlow,
  GetApplyPurchasePageList,
  GetPurContractPageList,
  GetQuantityCheckLake,
  GetPurchaseContractDel
} from '@/api/applyPurchase'
import { GetChangePriceCheck } from '@/api/baseData'

import { GetSupplierShipFlow } from '@/api/supplierShip'
import MyTable from '_c/my-table/my-table'
import PurchaseForm from './purchase-form'
import ContractForm from './contract-form'
import PayForm from './pay-form'
import MaterialApproveForm from './material-approve-form'
import ProviderPriceForm from './provider-price-form'
import ApplyMaterialForm from './apply-material-form'
import QtyForm from './qty-form'
import OtherForm from './other-form'
import ReturnForm from './return-form'
import CheckForm from './check-form'
import NewCheckForm from './new-check-form'
import ProfitForm from './profit-form'
import DelContractForm from './del-contract-form'
import NoCheckForm from './no-check-form'
import ProviderContractOverForm from './provider-contract-over-form'
import ProviderTagDelForm from './provider-tag-del-form'
import ProviderShipmentForm from './provider-shipment-form'
import profitTagForm from './profit-tag-form'
import forOutbound from './for-outbound'

import { exportExcel } from '@/libs/util'
export default {
  name: 'schedule',
  components: {
    MyTable,
    PurchaseForm,
    ContractForm,
    PayForm,
    MaterialApproveForm,
    ProviderPriceForm,
    QtyForm,
    ApplyMaterialForm,
    OtherForm,
    ReturnForm,
    CheckForm,
    NewCheckForm,
    ProfitForm,
    DelContractForm,
    NoCheckForm,
    ProviderContractOverForm,
    ProviderTagDelForm,
    ProviderShipmentForm,
    profitTagForm,
    forOutbound
  },
  data () {
    return {
      selectList: [],
      suggestions: '',
      formData: {},
      providerShipmentShow: false,
      providerContractOverShow: false,
      providerTagDelShow: false,
      noCheckShow: false,
      delContractShow: false,
      profitShow: false,
      newCheckShow: false,
      dispatchShow: false,
      checkShow: false,
      returnShow: false,
      otherShow: false,
      applyMaterialShow: false,
      qtyCheckShow: false,
      providerPriceShow: false,
      materialApproveShow: false,
      purchaseShow: false,
      contractShow: false,
      payShow: false,
      profitTagFormShow: false,
      forOutboundShow:false,
      data: [],
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '待办类型',
          key: 'dealWithTypeStr',
          minWidth: 150
        },
        {
          title: '待办事项',
          key: 'dealWithItem',
          minWidth: 250,
          render: (h, params) => {
            let _this = this
            return h(
              'a',
              {
                on: {
                  click (e) {
                    e.stopPropagation()
                    _this.formData = {}
                    console.log(params.row.dealWithType)
                    if (params.row.dealWithType === 0) {
                      // 请购单审批
                      GetApplyPurchasePageList({
                        id: params.row.linkId,
                        pageIndex: 1,
                        pageSize: 1
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = {
                            ...res.result.list[0],
                            ...params.row
                          }
                          _this.purchaseShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 1) {
                      // 合同审批
                      GetPurContractPageList({
                        id: params.row.linkId,
                        pageIndex: 1,
                        pageSize: 1
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = {
                            ...res.result.list[0],
                            ...params.row
                          }
                          _this.contractShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 2) {
                      // 付款审批
                      _this.formData = { ...params.row }
                      _this.payShow = true
                    } else if (params.row.dealWithType === 3) {
                      // 领料申请
                      GetApplyTakePageList({
                        id: params.row.linkId,
                        pageIndex: 1,
                        pageSize: 1
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = {
                            ...res.result.list[0],
                            ...params.row
                          }
                          _this.applyMaterialShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 4) {
                      // 其他出库退货/报损
                      GetApplyStockOutBill({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.otherShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 5) {
                      // 到货质检数量修正
                      GetQuantityCheckLake({
                        checkAdjustId: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.qtyCheckShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 6) {
                      // 物料审批
                      _this.formData = { ...params.row }
                      _this.materialApproveShow = true
                    } else if (params.row.dealWithType === 7) {
                      // 供应商调价
                      GetChangePriceCheck({
                        priceChangeId: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.providerPriceShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 8) {
                      // 审核盘点
                      _this.formData = { ...params.row }
                      _this.profitShow = true
                    } else if (params.row.dealWithType === 9) {
                      // 归还入库
                      GetApplyStockInBackRequestDto({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.returnShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 10) {
                      // 合同删除
                      GetPurchaseContractDel({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.delContractShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 11) {
                      // 免检申请
                      GetStockExemptCheck({
                        exemptCheckId: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.noCheckShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 12) {
                      // 供应商审批
                      GetSupplierShipFlow({
                        flowCheckId: params.row.flowChekId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          if (res.result.supplierApplyType === 0) {
                            _this.providerShipmentShow = true
                          } else if (res.result.supplierApplyType === 1) {
                            _this.providerContractOverShow = true
                          } else if (res.result.supplierApplyType === 2) {
                            _this.providerTagDelShow = true
                          }
                        }
                      })
                    } else if (params.row.dealWithType === 13) {
                      // 盘点标签处置
                      _this.formData = { ...params.row }
                      _this.profitTagFormShow = true
                    } else if(params.row.dealWithType === 14){
                      _this.formData = { ...params.row }
                      _this.forOutboundShow = true
                    }
                  }
                }
              },
              params.row.dealWithItem
            )
          }
        },
        {
          title: '审批状态',
          key: 'checkStateStr',
          minWidth: 150
        },
        {
          title: '日期',
          key: 'createTime',
          minWidth: 150
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'export'
        },
        {
          name: 'Select',
          value: 'dealWithType',
          placeholder: '待办类型',
          list: [
            { label: '物料申购', value: 0 },
            { label: '合同管理', value: 1 },
            { label: '付款管理', value: 2 },
            { label: '出库管理', value: 3 },
            { label: '其他出库（退货/报损）', value: 4 },
            { label: '质检数量修正', value: 5 },
            { label: '物料修改', value: 6 },
            { label: '物料价格调整', value: 7 },
            { label: '库存盘点', value: 8 },
            { label: '归还入库', value: 9 },
            { label: '合同删除', value: 10 },
            { label: '免检申请', value: 11 },
            { label: '供应商审批', value: 12 },
            { label: '盘点标签处置', value: 13 }
          ]
        },
        {
          name: 'RadioGroup',
          value: 'approvalFlowState',
          list: [
            { label: '待审批', value: 0 },
            { label: '已审批', value: 1 },
            { label: '已驳回', value: 2 },
            { label: '已发起', value: 3 }
          ]
        },
        {
          name: 'Button',
          type: 'primary',
          align: 'right',
          text: '批量审核',
          e: 'dispath'
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    pass (type) {
      ApprovalBillFlow({
        linkCheckId: this.selectList,
        updateAction: type,
        suggestions: this.suggestions,
        isBatchApproval: true
      }).then(res => {
        if (res.code === 0) {
          this.dispatchShow = false
          this.suggestions = ''
          this.selectList = []
          this.$Message.info(res.msg || '操作成功！')
          this.getList()
        }
      })
    },
    getSelect (selection, data) {
      this.selectList = selection.map(item => item.flowChekId)
      console.log(this.selectList)
    },
    cfm (type, formData) {
      if (type === 'add') {
        FacPriceAdd({
          ...formData,
          materialCode: formData.code
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      } else if (type === 'edit') {
        FacPriceChange({
          ...formData,
          factoryPriceId: formData.id
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.keyWord = option.key
          this.params.approvalFlowState = option.approvalFlowState
          this.params.dealWithType = option.dealWithType
          this.getList()
          break
        case 'export':
          GetDealWithItem({
            ...this.params,
            pageIndex: 1,
            pageSize: 99999
          }).then(res => {
            if (res.code === 0) {
              let data = res.result.list
              if (data.length < 1) return this.$Message.error('数据为空！')
              exportExcel(this.columns, data, '待办事项')
            }
          })
          break
        case 'dispath':
          this.dispatchShow = true
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      GetDealWithItem(this.params).then(res => {
        if (res.code === 0) {
          this.data = res.result.list
          this.params.total = res.result.totalCount
        }
      })
    },
  },
  mounted () {
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
/deep/ .ivu-modal-body {
  background: transparent;
}
</style>
