<!-- 采购列表 -->
<template>
  <div class="purchase">
    <template v-if="!addOrUpdateVisible && !detailVisible && !operateVisible && !inboundVisible && !noticeChangeAttachVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="类别">
          <el-select
            v-model="dataForm.purchaseType"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsType"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物品名称">
          <el-input v-model="dataForm.materialName" placeholder="物品名称" clearable/>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
            v-model="dataForm.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="dataForm.purchaseState"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsState"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
          <el-button v-if="isAuth('purchase:detail:save')" type="primary" @click="addOrUpdateHandle(0, false)">新建采购申请</el-button>
          <el-button v-if="isAuth('purchase:detail:noteChangeConfig')" type="primary" @click="setNoticeChangeHandel()">工作提示通知设置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="procurementCode"
          header-align="center"
          align="center"
          min-width="180"
          :show-tooltip-when-overflow="true"
          label="采购编码">
        </el-table-column>
        <el-table-column
          prop="materialName"
          header-align="center"
          align="center"
          width="160"
          :show-tooltip-when-overflow="true"
          label="物品名称">
        </el-table-column>
        <el-table-column
          prop="purchaseType"
          header-align="center"
          align="center"
          :formatter="formatType"
          label="采购类别">
        </el-table-column>
        <el-table-column
          prop="specification"
          header-align="center"
          align="center"
          width="120"
          :show-tooltip-when-overflow="true"
          label="型号及规格">
        </el-table-column>
        <el-table-column
          prop="cnt"
          header-align="center"
          align="center"
          label="数量">
        </el-table-column>
        <el-table-column
          prop="qualifiedCnt"
          header-align="center"
          align="center"
          label="合格数量">
        </el-table-column>
        <el-table-column
          prop="unitName"
          header-align="center"
          align="center"
          min-width="100"
          :show-tooltip-when-overflow="true"
          label="单位">
        </el-table-column>
        <el-table-column
          prop="deadline"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="采购期限">
        </el-table-column>
        <el-table-column
          prop="arrivedTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="到料时间">
        </el-table-column>
        <el-table-column
          prop="batchNumber"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="批次号/用途">
        </el-table-column>
        <el-table-column
          prop="procurementBasis"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="采购依据">
          <template slot-scope="scope">
            <span>{{scope.row.procurementBasis?(Number(scope.row.procurementBasis) === 1?'合同采购':'计划采购'):''}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="supplierName"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="供应商">
        </el-table-column>
        <el-table-column
          prop="applyTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-tooltip-when-overflow="true"
          label="申请时间">
        </el-table-column>
        <el-table-column
          prop="applier"
          header-align="center"
          align="center"
          min-width="100"
          :show-tooltip-when-overflow="true"
          label="申请人">
        </el-table-column>
        <el-table-column
          prop="orgName"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="申请部门">
        </el-table-column>
        <el-table-column
          prop="approvalState"
          header-align="center"
          align="center"
          :formatter="formatState"
          label="审批状态">
        </el-table-column>
        <el-table-column
          prop="purchaseState"
          header-align="center"
          align="center"
          :formatter="formatPurchaseState"
          label="采购状态">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('purchase:detail:info')" type="text" size="small" @click="detailHandle(scope.row.procurementId)">查看</el-button>
            <el-button v-if="isAuth('purchase:detail:update') && (Number(scope.row.approvalState) === 1)" type="text" size="small" @click="addOrUpdateHandle(scope.row.procurementId, false)">编辑</el-button>
            <el-button v-if="isAuth('purchase:detail:updatePurchaseDetail') && (Number(scope.row.purchaseState) === 1)" type="text" size="small" @click="operateHandle(scope.row)">采购</el-button>
            <el-button v-if="isAuth('purchase:detail:infoPutIn') && (Number(scope.row.approvalState) === 3  && Number(scope.row.purchaseState) > 1)" type="text" size="small" @click="inboundHandle(scope.row)">入库</el-button>
            <el-button v-if="isAuth('purchase:detail:infoPutInAgain') && (Number(scope.row.purchaseState) === 5)" type="text" size="small" @click="inboundHandle(scope.row.procurementId)">再次入库</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @onChose="onChose"></add-or-update>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
    <!-- 采购操作 -->
    <operate v-if="operateVisible" ref="operate" @onChose="onChose" @refreshDataList="getDataList"/>
    <!-- 入库 -->
    <inbound v-if="inboundVisible" ref="inbound" @onChose="onChose" @refreshDataList="getDataList"/>
    <notice-change-setting v-if="noticeChangeAttachVisible" ref="noticeChangeSetting" @onChose="onChose"/>
<!--    <change-form v-if="changeFormVisible" ref="changeForm" @refreshDataList="getDataList" @onChose="onChose"/>-->
  </div>
</template>

<script>
  import AddOrUpdate from './purchase-add-or-update'
  import Detail from './purchase-detail'
  import Operate from './purchase-operate'
  import Inbound from '../warehouse/stock-order-inbound'
  import { getDictList } from '@/api/dict'
  import { getPurchaseList, revokePurchase } from '@/api/sale'
  import NoticeChangeSetting from './purchase-notice-change-setting'
  export default {
    name: 'purchase',
    components: {
      AddOrUpdate, Detail, Operate, Inbound, NoticeChangeSetting
    },
    data () {
      return {
        addOrUpdateVisible: false,
        detailVisible: false,
        operateVisible: false,
        inboundVisible: false,
        noticeChangeAttachVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsType: [],
        // 审批状态：0 待提交 1 待审批 2 审批中 3 审批完成 4 审批不通过
        optionsState: [],
        // 采购状态：1 待采购 2 采购中 3 已入库 4 入库中 5 入库异常
        optionsPurchaseState: [
          {code: null, value: '全部'},
          {code: '1', value: '待采购'},
          {code: '2', value: '采购中'},
          {code: '3', value: '已入库'},
          {code: '4', value: '入库中'},
          {code: '5', value: '入库异常'}
        ]
      }
    },
    created () {
      this.optionsState = this.$store.state.common.approveStates
      this.getTypeList()
      this.getDataList()
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
        this.detailVisible = false
        this.operateVisible = false
        this.inboundVisible = false
        this.noticeChangeAttachVisible = false
      },
      // 获取采购类别字典
      getTypeList () {
        getDictList({type: 'purchase_type'}).then(({data}) => {
          if (data) {
            this.optionsType = data
          }
        })
      },
      // 查询
      search () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.addOrUpdateVisible = false
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'materialName': this.dataForm.materialName ? this.dataForm.materialName : null,
          'purchaseState': this.dataForm.purchaseState ? this.dataForm.purchaseState : null,
          'purchaseType': this.dataForm.purchaseType ? this.dataForm.purchaseType : null,
          'beginTime': this.dataForm.date ? this.dataForm.date[0] : null,
          'endTime': this.dataForm.date ? this.dataForm.date[1] : null
        }
        getPurchaseList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      revokeHandle (id) {
        if (!id) return
        let data = {id: id}
        this.$confirm(`确定撤回?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          revokePurchase(data).then(({data}) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id, disable) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, disable)
        })
      },
      // 转换属性“类别”
      formatType (row) {
        if (this.optionsType && row.purchaseType) {
          const item1 = this.optionsType.find((item) => item.code === row.purchaseType.toString())
          return item1 ? item1.value : ''
        }
      },
      // 转换属性“审批状态”
      formatState (row) {
        if (!row.approvalState) return ''
        const item1 = this.optionsState.find((item) => item.code === row.approvalState.toString())
        return item1 ? item1.value : ''
      },
      // 转换属性“采购状态”
      formatPurchaseState (row) {
        if (!row.purchaseState) return ''
        const item1 = this.optionsPurchaseState.find((item) => item.code === row.purchaseState.toString())
        return item1 ? item1.value : ''
      },
      // 详情
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      },
      // 采购操作
      operateHandle (row) {
        this.operateVisible = true
        this.$nextTick(() => {
          this.$refs.operate.init(row)
        })
      },
      // 入库
      inboundHandle (row) {
        if (!row) return
        let dataForm = {
          ...row,
          buttonType: '1',
          sourceCategory: '1', // 入库依据类别
          // materialTypeId: row.purchaseType, // 类别
          source: row.supplierName, // 来源对应供应商
          tableId: row.purchaseDetailId,
          specifications: row.specification
        }
        this.inboundVisible = true
        this.$nextTick(() => {
          this.$refs.inbound.init(0, false, dataForm)
        })
      },
      // 变更通知人设置
      setNoticeChangeHandel () {
        this.noticeChangeAttachVisible = true
        this.$nextTick(() => {
          this.$refs.noticeChangeSetting.init()
        })
      }
    }
  }
</script>

<style scoped>

</style>
