<template>
  <template-page ref="pageRef" :get-list="getList" :exportList="exportList" :table-attributes="tableAttributes"
    :table-events="tableEvents" :options-evens-group="optionsEvensGroup" :moreParameters="moreParameters"
    :column-parsing="columnParsing" :operation="operation" :replaceOrNotMap="false">
    <div class="cartographer">
      <el-dialog :title="({ M: '辅材入库单', P: '配件入库单' })[storageType]" width="100%" :modal="false" :visible.sync="formDialog"
        :before-close="formCancel">
        <zj-form-container v-if="formDialog" ref="formRef" :form-data="formData" :styleSwitch="false">
          <zj-form-module title="单据信息" label-width="120px" :showPackUp="false" :form-data="formData"
            :form-items="formItems1">
          </zj-form-module>
          <zj-form-module :title="({ M: '辅材信息', P: '配件信息' })[storageType]" label-width="120px" :showPackUp="false"
            :form-data="formData" :form-items="formItems2">
          </zj-form-module>
        </zj-form-container>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="formCancel">取 消</el-button>
          <el-button size="mini" @click="formConfirm" type="primary">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import form_tpl from "../mixins/form_tpl.js"
import { websitPurchaseInList, websitPurchaseInListExport, websitPurchaseInAdd, websitPurchaseInEdit, websitPurchaseInConfirm, websitPurchaseInDetail } from "@/api/purchasingManagement.js"
export default {
  props: {
    storageType: {
      type: String,
      default: ""
    }
  },
  components: { TemplatePage },
  mixins: [import_mixin, form_tpl],
  data() {
    return {
      formData: {
        items: []
      },
    }
  },
  methods: {
    // 列表请求函数
    getList(p, cb) {
      var pam = JSON.parse(JSON.stringify(p))
      pam.params.push({ "param": "a.goods_type", "compare": "=", "value": this.storageType })
      if (pam.flag) {
        pam.params.push({ "param": "a.flag", "compare": "=", "value": pam.flag })
      }
      cb && cb(pam)
      return websitPurchaseInList(pam)
    },
    // 列表导出函数
    exportList: websitPurchaseInListExport,
    // 操作按钮
    operation(h, { row, index, column }) {
      return (
        <div class='operation-btns'>
          <el-button type="text" onClick={() => {
            Object.assign(this.formData, row)
            this.formDialogType = 1
            this.openForm()
          }}>编辑</el-button>
          <el-button type="text" onClick={() => {
            Object.assign(this.formData, row)
            this.formDialogType = 2
            this.openForm()
          }}>详情</el-button>
          <el-button type="text" onClick={() => {
            Object.assign(this.formData, row)
            this.formDialogType = 3
            this.openForm()
          }}>审核</el-button>
        </div>
      )
    },
    formConfirm() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          ([websitPurchaseInAdd, websitPurchaseInEdit][this.formDialogType])(this.formData).then(res => {
            this.$message({ type: 'success', message: `${this.formDialogTitles[this.formDialogType]}成功!` })
            this.formCancel()
            this.$refs.pageRef.refreshList()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
