<!-- 产品管理 -->
<template>
  <div class="purchase">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
      <el-form-item label="说明">
        <el-input v-model="dataForm.notes" placeholder="说明" clearable/>
      </el-form-item>
      <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-date-picker
          v-model="dataForm.applyDate"
          value-format="yyyy-MM-dd"
          type="date">
        </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 type="primary" @click="addOrUpdateHandle(0, false)">创建新产品</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="purchaseCode"
        header-align="center"
        align="center"
        min-width="100"
        label="采购编码">
      </el-table-column>
      <el-table-column
        prop="materialName"
        header-align="center"
        align="center"
        min-width="120"
        label="物品名称">
      </el-table-column>
      <el-table-column
        prop="purchaseType"
        header-align="center"
        align="center"
        :formatter="formatType"
        label="采购类别">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        min-width="160"
        label="申请时间">
      </el-table-column>
      <el-table-column
        prop="applierName"
        header-align="center"
        align="center"
        min-width="100"
        label="申请人">
      </el-table-column>
      <el-table-column
        prop="deptName"
        header-align="center"
        align="center"
        min-width="100"
        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
        prop="purchaseState"
        header-align="center"
        align="center"
        :formatter="formatState"
        label="当前状态">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.purchaseId, true)">查看</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.purchaseId, false)">编辑</el-button>
          <el-button style="color: red" type="text" size="small" @click="revokeHandle(scope.row.purchaseId)">撤回</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>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './purchase-add-or-update'
  import { getDictList } from '@/api/dict'
  import { getPurchaseList, revokePurchase } from '@/api/sale'
  export default {
    name: 'purchase',
    components: {
      AddOrUpdate
    },
    data () {
      return {
        addOrUpdateVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsType: [],
        optionsState: [
          {
            code: '0',
            value: '未审批'
          },
          {
            code: '1',
            value: '审批通过'
          }
        ]
      }
    },
    created () {
      this.getTypeList()
      this.getDataList()
    },
    methods: {
      // 获取采购类别字典
      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,
          'applyDate': this.dataForm.applyDate ? this.dataForm.applyDate : null,
          'notes': this.dataForm.notes ? this.dataForm.notes : null,
          'purchaseState': this.dataForm.purchaseState ? this.dataForm.purchaseState : null,
          'purchaseType': this.dataForm.purchaseType ? this.dataForm.purchaseType : 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.purchaseState) return ''
        const item1 = this.optionsState.find((item) => item.code === row.purchaseState.toString())
        return item1 ? item1.value : ''
      }
    }
  }
</script>

<style scoped>

</style>
