<!--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-width="80px"
          label-suffix=":"
        >

          <el-form-item label="车间">
            <el-select
              v-model="query.workshop_id"
              clearable
              size="mini"
              placeholder="全部"
              class="filter-item"
              @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"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="center"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              @change="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              v-model="query.bill_status"
              clearable
              size="mini"
              placeholder="单据状态"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.IO_BILL_STATUS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="生成方式">
            <el-select
              v-model="query.create_mode"
              clearable
              filterable
              size="mini"
              placeholder="生成方式"
              class="filter-item"
              @change="crud.toQuery"
            >
              <el-option
                v-for="item in dict.ST_INV_CREATE"
                :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"
              size="mini"
              clearable
              placeholder="编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation/>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-upload2"
          size="mini"
          @click="downdtl"
        >
          导出EXCEL
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="uploadShow = true"
        >
          导入EXCEL
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-finished"
          :disabled="crud.selections.length !== 1"
          size="mini"
          @click="commit(crud.selections)"
        >
          确认采购
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-check"
          :disabled="crud.selections.length === 0"
          size="mini"
          @click="commit(crud.selections)"
        >
          强制完成
        </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        size="mini"
        :data="crud.data"
        highlight-current-row
        show-summary
        :summary-method="getSummaries"
        style="width: 100%;"
        @select-all="crud.selectAllChange"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column show-overflow-tooltip prop="workshop_id" label="车间"/>
        <el-table-column show-overflow-tooltip prop="material_code" width="130" label="物料编号"/>
        <el-table-column show-overflow-tooltip prop="material_name" width="130" label="物料名称"/>
        <el-table-column prop="ask_time" label="要求到货日期" width="130" show-overflow-tooltip/>
        <el-table-column show-overflow-tooltip prop="qty" min-width="120" label="重量"/>
        <el-table-column show-overflow-tooltip prop="agree_time" min-width="120" label="确认交货日期"/>
        <el-table-column show-overflow-tooltip prop="po_code" min-width="120" label="订单编号"/>
        <el-table-column show-overflow-tooltip prop="instor_qty" min-width="120" label="入库重量"/>
        <!--        <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 label="状态" align="center" prop="status" :formatter="stateFormat"/>
        <el-table-column label="备注" align="center" prop="remark"/>
        <el-table-column label="生成方式" align="center" prop="create_mode" :formatter="createModeFormat"/>
        <el-table-column label="创建人" align="center" prop="create_name" width="150"/>
        <el-table-column label="创建时间" align="center" prop="create_time" width="150"/>
        <el-table-column label="提交人" align="center" prop="audit_namne" width="150"/>
        <el-table-column label="提交时间" align="center" prop="audit_time" width="140px"/>
        <el-table-column label="采购确认人" align="center" prop="purchase_name" width="150"/>
        <el-table-column label="采购确认时间" align="center" prop="purchase_time" width="140px"/>
        <el-table-column label="完成人" align="center" prop="confirm_name" width="150"/>
        <el-table-column label="完成时间" align="center" prop="confirm_time" width="140px"/>
        <!--        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
                  <template slot-scope="scope">
                    <udOperation
                      :data="scope.row"
                      :permission="permission"
                    />
                  </template>
                </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
    <AddDialog @AddChanged="querytable"/>
    <UploadDialog :dialog-show.sync="uploadShow" @tableChanged3="crud.toQuery()"/>
  </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'
import { download } from '@/api/data'
import { downloadFile } from '@/utils'
import UploadDialog from "@/views/wms/pcs/purchaseplan/UploadDialog";

export default {
  name: 'PurchasePlan',
  components: {UploadDialog, AddDialog, crudOperation, rrOperation, udOperation, pagination, DateRangePicker},
  cruds() {
    return CRUD({
      title: '',
      optShow: {reset: true},
      idField: 'id',
      query: {
        'status': '1'
      },
      url: '/api/pcsIfPurchaseorder',
      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: [],
      uploadShow: false,
      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 === 'qty' || column.property === 'instor_qty') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' KG';
        }
      });
      return sums;
    },
    bill_typeFormat(row, column) {
      return this.dict.label.ST_INV_RAW_IN_TYPE[row.bill_type]
    },
    downdtl() {
      crud.downloadLoading = true
      download('/api/pcsIfPurchaseorder/download', this.crud.query
      ).then(result => {
        debugger
        downloadFile(result, '原料需求', 'xlsx')
        crud.downloadLoading = false
      }).catch(() => {
        crud.downloadLoading = false
      })
    },
    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 rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 10px;
}
</style>
