<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 v-if="~[0, 1].indexOf(formDialogType)" size="mini" @click="formConfirm" type="primary"
            >确 定</el-button
          >
          <el-button v-if="~[3].indexOf(formDialogType)" size="mini" @click="passExamination('OK')" type="primary"
            >审核通过</el-button
          >
          <el-button v-if="~[3].indexOf(formDialogType)" size="mini" @click="passExamination('FAIL')" 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 operation_mixin from '@/components/template/operation_mixin.js'
import {
  websitPurchaseRetList,
  websitPurchaseRetListExport,
  websitPurchaseRetAdd,
  websitPurchaseRetEdit,
  websitPurchaseRetConfirm,
  websitPurchaseRetDetail
} from '@/api/purchasingManagement.js'
export default {
  props: {
    storageType: {
      type: String,
      default: ''
    }
  },
  components: { TemplatePage },
  mixins: [import_mixin, form_tpl, operation_mixin],
  data() {
    return {
      formData: {
        companyWechatName: JSON.parse(localStorage.getItem('greemall_user')).companyName,
        companyWechatId: '',
        confirmBy: '',
        confirmTime: '',
        createBy: '',
        createTime: '',
        flag: '',
        goodsType: this.storageType,
        purchaseId: '',
        purchaseRetId: '',
        remark: '',
        retTime: '',
        retTotalAmount: 0,
        retTotalQty: 0,
        updateBy: '',
        updateTime: '',
        venderId: '',
        venderName: '',
        storageId: '',
        websitId: '',
        websitName: '',
        items: [],
        imageUrl: []
      }
    }
  },
  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 websitPurchaseRetList(pam)
    },
    // 列表导出函数
    exportList: websitPurchaseRetListExport,

    operation() {
      return this.operationBtn({
        edit: {
          conditions: ({ row, index, column }) => {
            return row.flag == 'SAVE'
          },
          click: ({ row, index, column }) => {
            this.getDetail(row.purchaseRetId, 1)
          }
        },
        detail: {
          click: ({ row, index, column }) => {
            this.getDetail(row.purchaseRetId, 2)
          }
        },
        examine: {
          conditions: ({ row, index, column }) => {
            return row.flag == 'SAVE'
          },
          click: ({ row, index, column }) => {
            this.getDetail(row.purchaseRetId, 3)
          }
        }
      })
    },

    getDetail(purchaseRetId, type) {
      websitPurchaseRetDetail({ purchaseRetId }).then(res => {
        Object.assign(this.formData, res.data, {
          imageUrl: res.data.imageUrl ? [{ url: res.data.imageUrl }] : [],
          items: res.data.items.map(item => ({
            ...item,
            isEditRow: false
          }))
        })
        this.formDialogType = type
        this.openForm()
      })
    },
    formConfirm() {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          ;[websitPurchaseRetAdd, websitPurchaseRetEdit]
            [this.formDialogType]({
              ...this.formData,
              goodsType: this.storageType,
              imageUrl: this.formData.imageUrl.map(item => item.url).join(',')
            })
            .then(res => {
              this.$message({ type: 'success', message: `${this.formDialogTitles[this.formDialogType]}成功!` })
              this.formCancel()
              this.$refs.pageRef.refreshList()
            })
        }
      })
    },
    passExamination(flag) {
      this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
        if (valid) {
          websitPurchaseRetConfirm({
            purchaseRetId: this.formData.purchaseRetId,
            flag: flag,
            storageId: this?.formData?.storageId
          }).then(res => {
            this.$message({ type: 'success', message: flag == 'OK' ? `审核通过!` : '驳回通过' })
            this.formCancel()
            this.$refs.pageRef.refreshList()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.redbordererr {
  .el-form-item {
    margin: 0 !important;
    overflow: hidden;
  }
}
</style>
