<template>
  <div>
    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :toolkit="[]"
      :datasource="tableForm.detailList"
      row-key="id">
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <el-button
          v-if="form.type&&dialogType!='view'&&!isOtherSourceFlag"
          size="small"
          type="primary"
          icon="el-icon-plus"
          class="ele-btn-icon"
          @click="handleSelectAccount"
        >
          选择关联类型
        </el-button>
      </template>
    </ele-pro-table>
    <!--对账单-->
    <account-statement-dialog ref="accountStatementDialogRef"
                              :form="form"
                              v-if="accountStatementDialogFlag"
                              :accountstatement-dialog-flag.sync="accountStatementDialogFlag"
                              @getAccountData="getAccountData">
    </account-statement-dialog>
  </div>
</template>
<script>
import accountStatementDialog from "./accountstatementDialog.vue";

export default {
  name: "tableInfo",
  components: {
    accountStatementDialog
  },
  props: {
    form: {
      type: Object,
      default: () => {
        return {
          detailList: []
        }
      }
    },
    dialogType: {
      type: String,
      default: ""
    },
    isOtherSourceFlag: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      accountStatementDialogFlag: false,
      columns: [
        {
          width: 45,
          type: 'index',
          columnKey: 'index',
          align: 'center',
          fixed: 'left'
        },

        {
          width: 100,
          prop: 'typeName',
          label: '类型',
          slot: 'typeName',
          align: "center"
        },
        {
          minWidth: 100,
          prop: 'sourceCode',
          label: '类型编码',
          slot: 'sourceCode',
          align: "center"
        },
        // {
        //   width: 100,
        //   prop: 'productCategoryName',
        //   label: '分类',
        //   slot: 'productCategoryName',
        //   align: "center"
        // },
        // {
        //   width: 100,
        //   prop: 'productCode',
        //   label: '编码',
        //   slot: 'productCode',
        //   align: "center"
        // },
        {
          minWidth: 100,
          prop: 'productName',
          label: '名称',
          slot: 'productName',
          align: "center"
        },
        {
          minWidth: 100,
          prop: 'modelType',
          label: '型号',
          slot: 'modelType',
          align: "center"
        },

        {
          minWidth: 100,
          prop: 'specification',
          label: '规格',
          slot: 'specification',
          align: "center"
        },
        // {
        //   width: 100,
        //   prop: 'productBrand',
        //   label: '牌号',
        //   slot: 'productBrand',
        //   align: "center"
        // },
        {
          width: 100,
          prop: 'measuringUnit',
          label: '单位',
          slot: 'measuringUnit',
          align: "center"
        },
        {
          width: 80,
          prop: 'totalCount',
          label: '数量',
          slot: 'totalCount',
          align: "center"
        },

        {
          minWidth: 80,
          prop: 'singlePrice',
          label: '单价',
          slot: 'singlePrice',
          align: "center"
        },
        {
          minWidth: 80,
          prop: 'totalPrice',
          label: '金额',
          slot: 'totalPrice',
          align: "center"
        },

        // {
        //   width: 130,
        //   prop: 'modelType',
        //   label: '品牌',
        //   slot: 'modelType'
        // },

        // {
        //   width: 100,
        //   prop: 'sourceType',
        //   label: '来源类型',
        //   slot: 'sourceType',
        //   align: "center",
        //   formatter: (row, column) => {
        //     return row.sourceType == 2 ? '对账销售订单' : '对账采购订单';
        //   }
        // },

      ],
      tableForm: {
        detailList: [],
        link: []
      },
      typeList: [
        {
          label: '销售发货',
          value: '10',
        },
        {
          label: '销售退货',
          value: '11',
        },
        {
          label: '采购收货',
          value: '20',
        }, {
          label: '采购退货',
          value: '21',
        },

      ],

    }
  },
  mounted() {
    this.tableForm = this.form
  },
  methods: {
    //对账单
    async handleSelectAccount() {
      this.accountStatementDialogFlag = true
    },
    //获取选择的对账单数据
    async getAccountData(params) {
      if (params.children.orderType == 6) {
        this.tableForm.detailList = params.children.detailList
        this.tableForm.detailList.forEach((item, index) => {
          item.sourceCode = params.children.orderNo
          item.sourceId = params.children.id
          item.sourceType = params.type == 1 ? 2 : 3
          item.type = 12
          item.singlePrice = item.discountSinglePrice
          item.totalPrice = item.discountTotalPrice
          item.typeName = '销售赔付'
        })
      } else {
        this.tableForm.detailList = []
        params.children.subList.forEach((item, index) => {
          item.detailList.forEach((i, n) => {
            i.sourceCode = item.statementSubOrderCode
            i.sourceId = params.children.id
            i.sourceType = params.type == 1 ? 2 : 3
            i.type = item.subType
            console.log(item.subType);
            i.typeName = this.typeList.find(i => i.value == item.subType).label
            item.singlePrice = item.discountSinglePrice
            item.totalPrice = item.discountTotalPrice
          })
          this.tableForm.detailList.push(...item.detailList)
        })
        this.$refs.table.reload()
      }
      this.$emit('setPrice', params.children.amountTotalPrice)
      let row = {
        id: params.id,
        name: params.statementNo,
        code: params.statementNo,
        linkType: params.type == 1 ? 190 : 290,
        linkTypeName: params.type == 1 ? '销售对账单' : '采购对账单',
      }
      this.setSelectData(row)
    },
    clearTable() {
      this.tableForm = {
        detailList: [],
        link: []
      }
    },
    getTableValidate() {
      return new Promise((resolve, reject) => {
        if (this.tableForm.detailList.length == 0) return this.$message.warning('请选择关联信息')
        resolve(this.tableForm)
      })
    },
    setSelectData(val) {
      this.tableForm.link = [{}]
      this.$set(this.tableForm.link[0], 'linkId', val.id)
      this.$set(this.tableForm.link[0], 'linkName', val.name)
      this.$set(this.tableForm.link[0], 'linkCode', val.code)
      this.$set(this.tableForm.link[0], 'linkType', val.linkType)
      this.$set(this.tableForm.link[0], 'linkTypeName', val.linkTypeName)
    },
    getTableData() {

    }
  }
}
</script>


<style scoped lang="scss">

</style>
