import { goodsPurchaseRetDetail } from '@/api/merchandisePurchaseReturn.js'
import { commonTemplateDownload } from '@/api/common.js'
export default {
  data() {
    return {}
  },
  computed: {
    formItems2() {
      return [
        {
          md: 24,
          isShow: true,
          name: 'slot-component',
          formItemAttributes: {
            label: '',
            prop: '',
            'label-width': '0px'
          },
          render: (h, { props, onInput }) => {
            var { value } = props
            return (
              <div>
                {this.activeName == 'goodsInfo'
                  ? [
                    this.formDialogType == 1 && this.joinCode ? (
                      <div style="margin-bottom:10px">
                        <el-button type="primary" onClick={this.domlMban}>
                          下载导入模板
                        </el-button>
                      </div>
                    ) : null,
                    <zj-table
                      columns={this.storage_goods}
                      tableData={this.formData.items}
                      tableAttributes={{
                        size: 'mini',
                        border: true
                      }}
                    />
                  ]
                  : this.activeName == 'codeInfo' && this.joinCode
                    ? [
                      this.formDialogType == 1 ? (
                        <div style="margin-bottom:10px">
                          <el-button type="primary" onClick={this.addCodeInfo} disabled={this.isEditIndex != -1}>
                            添加
                          </el-button>
                        </div>
                      ) : null,
                      <zj-table
                        columns={this.storage_codes}
                        tableData={this.formData.codeInfoList}
                        tableAttributes={{
                          size: 'mini',
                          border: true
                        }}
                      />
                    ]
                    : null}
              </div>
            )
          }
        }
      ]
    }
  },
  watch: {
    activeName(newVal) {
      this.isEditIndex = -1
      if (newVal == 'codeInfo') {
        // 获取单据下的条码
        this.getGoodsPurchaseRetCodeList()
      } else {
        if (this.formData.id) {
          // 更新表单信息
          goodsPurchaseRetDetail({ id: this.formData.id }).then(res => {
            Object.assign(this.formData, res.data, {
              fileUrl: res.data.fileUrl ? [{ url: res.data.fileUrl }] : [],
              items: res.data.items.map(item => ({ ...item, details: {} }))
            })
          })
        }
        // 清空条码数据列表
        this.formData.codeInfoList = []
      }
    }
  },
  methods: {
    domlMban() {
      commonTemplateDownload({ name: '商品采购条码模板.xlsx' }, `${this.$route.meta.title}`)
        .then(res => {
          this.$message({
            message: '下载成功',
            type: 'success'
          })
        })
        .catch(err => {
          this.$message.error('下载失败')
        })
    }
  }
}
