<!--suppress ALL -->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-suffix=":"
          label-width="80px"
        >

          <el-form-item label="车间">
            <el-select
              v-model="query.workshop_id"
              class="filter-item"
              clearable
              placeholder="全部"
              size="mini"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.product_area"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="需求日期">
            <el-date-picker
              v-model="query.createTime"
              :default-time="['00:00:00', '23:59:59']"
              align="center"
              end-placeholder="结束日期"
              start-placeholder="开始日期"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="生成方式">
            <el-select
              v-model="query.create_mode"
              class="filter-item"
              clearable
              filterable
              placeholder="生成方式"
              size="mini"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.ST_CREATE_MODE"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料">
            <el-input
              v-model="query.material_search"
              clearable
              placeholder="编码、名称"
              size="mini"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation/>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          :disabled="crud.selections.length === 0"
          class="filter-item"
          icon="el-icon-check"
          size="mini"
          type="warning"
          @click="commit(crud.selections)"
        >
          提交采购
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        :summary-method="getSummaries"
        highlight-current-row
        show-summary
        size="mini"
        style="width: 100%;"
        @select-all="crud.selectAllChange"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column label="车间" prop="workshop_id" show-overflow-tooltip/>
        <el-table-column label="物料编号" prop="material_code" show-overflow-tooltip width="130"/>
        <el-table-column :min-width="flexWidth('material_name', crud.data, '物料名称')" label="物料名称" prop="material_name" show-overflow-tooltip/>
        <el-table-column label="要求到货日期" prop="askTime" show-overflow-tooltip width="150"/>
        <el-table-column label="重量" min-width="120" prop="show_qty" show-overflow-tooltip/>
        <!--        <el-table-column show-overflow-tooltip min-width="120" prop="biz_date" label="安全库存" />-->
        <!--        <el-table-column label="总重量" align="center" prop="total_qty"/>-->
        <el-table-column :formatter="stateFormat" align="center" label="状态" prop="status"/>
        <el-table-column :formatter="createModeFormat" align="center" label="生成方式" prop="create_mode" width="110"/>
        <el-table-column align="center" label="创建人" prop="create_name" width="150"/>
        <el-table-column align="center" label="创建时间" prop="createTime" width="150"/>
        <el-table-column align="center" label="备注" prop="remark"/>
        <el-table-column v-permission="[]" align="center" fixed="right" label="操作" width="120px">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
    <AddDialog @AddChanged="querytable"/>
  </div>
</template>

<script>
import crudPurchase from '@/views/wms/pcs/purchaseorder/purchase'
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker/index'
import AddDialog from '@/views/wms/pcs/purchaseorder/AddDialog'
import crudStorattr from '@/views/wms/storage_manage/basedata/basedata'

export default {
  name: 'PurchaseOrder',
  components: { AddDialog, crudOperation, rrOperation, udOperation, pagination, DateRangePicker },
  cruds() {
    return CRUD({
      title: '',
      optShow: { add: true, reset: true },
      idField: 'id',
      query: {
        'status': '0'
      },
      url: '/api/pcsIfPurchaseorder/query',
      crudMethod: { ...crudPurchase }
    })
  },
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ['PURCHASE_STATUS', 'product_area', 'ST_CREATE_MODE'],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      permission: {},
      disShow: false,
      viewShow: false,
      mstrow: {},
      divShow: false,
      openParam: [],
      currentRow: null,
      storlist: [],
      storId: null
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  created() {
    crudStorattr.getStor({ 'stor_type': '3' }).then(res => {
      this.storlist = res.content
    })
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (column.property === 'show_qty') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(3) + ' KG'
        }
      })
      return sums
    },
    bill_typeFormat(row, column) {
      return this.dict.label.ST_INV_RAW_IN_TYPE[row.bill_type]
    },
    commit(rows) {
      crudPurchase.commit(rows).then(res => {
        this.crud.notify('单据确认成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    stateFormat(row, column) {
      return this.dict.label.PURCHASE_STATUS[row.status]
    },
    createModeFormat(row, column) {
      return this.dict.label.ST_CREATE_MODE[row.create_mode]
    },
    querytable() {
      this.onSelectAll()
      this.crud.toQuery()
    }
  }
}
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
