<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :optionsEvensGroup="optionsEvensGroup"
    :columnParsing="columnParsing"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :moreParameters="moreParameters"
    :screeningAnalysis="screeningAnalysis"
    :filterMethod="filterMethod"
    :replaceOrNotMap="true"
  >
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import operation_mixin from '@/components/template/operation_mixin.js'
import { listPageV2 as listPageV3 } from '@/api/workOrder/settlementStandardInstall'
import { listPageV2 as listPageV2Repair } from '@/api/workOrder/settlementStandardRepair'
import {
  orderBaseList2,
  orderBaseStatusCount2,
  orderBaseSettleNormDetail2,
  settleNormInstallQuery,
  orderBaseSettleNormConfirm,
  settleNormOtherQuery,
  orderBaseSettleNormReset
} from '@/api/workOrderPool.js'
import { listPageV2 } from '@/api/workOrder/orderType'
import orderListColumn from '@/mixin/orderListColumn'
import { required } from '@/components/template/rules_verify.js'
export default {
  components: {
    TemplatePage
  },
  mixins: [import_mixin, operation_mixin, orderListColumn],
  data() {
    return {
      // 表格属性
      tableAttributes: {
        columnExpand: this.columnExpand
      },
      // 表格事件
      tableEvents: {
        'expand-change': this.expandChange
      },
      orderTypeList: [],
      orderStatusList: []
    }
  },
  computed: {
    moreParameters() {
      return [
        {
          name: '工单类型',
          key: 'orderSmallTypeText',
          value: '',
          conditions: [
            {
              label: '全部',
              value: ''
            },
            ...this.orderTypeList
          ]
        },
        {
          name: '工单状态',
          key: 'orderStatus',
          value: this.$route.query.orderStatus || '',
          conditions: [
            {
              label: '全部',
              value: ''
            },
            ...this.orderStatusList
          ]
        }
      ]
    },
    // 用户信息
    userInfo() {
      return JSON.parse(localStorage.getItem('greemall_user'))
    },
    // 事件组合
    optionsEvensGroup() {
      return []
    }
  },
  created() {
    // 获取工单类型
    listPageV2({ pageNum: 1, pageSize: -1, params: [{ param: 'a.status', compare: '=', value: 'true' }] }).then(res => {
      var obj = {}
      res.data.records.map(item => {
        if (!obj[item.orderSmallTypeText]) {
          obj[item.orderSmallTypeText] = {
            value: item.orderSmallTypeText,
            label: item.orderSmallTypeText
          }
        }
      })
      this.orderTypeList = Object.values(obj)
    })
  },
  methods: {
    screeningAnalysis(jname, val) {
      if (jname == 'orderFlags') {
        return (val || []).map(item => item.tagName).join(',')
      } else {
        return val
      }
    },

    filterMethod(value, row, column) {
      if (column['property'] == 'orderFlags') {
        return (row[column['property']] || []).map(item => item.tagName).join(',') === value
      }
      return row[column['property']] === value
    },

    // 获取统计
    getOrderBaseStatusCount(...p) {
      orderBaseStatusCount2(...p).then(res => {
        this.orderStatusList = [
          {
            label: '已完工待结算',
            value: 'YWG'
          },
          {
            label: '已结算',
            value: 'YJS'
          }
        ].map(item => {
          var data = res.data.find(val => val.orderStatus == item.value)
          if (data) {
            item.label = `${item.label}(${data.total})`
          }
          return item
        })
      })
    },

    // 列表请求函数
    getList(p, cb) {
      var pam = JSON.parse(JSON.stringify(p))
      try {
        if (pam.orderStatus) {
          pam.params.push({ param: 'a.order_status', compare: '=', value: pam.orderStatus })
        }
        if (pam.orderSmallTypeText) {
          pam.params.push({ param: 'a.order_small_type_text', compare: '=', value: pam.orderSmallType })
        }
        cb && cb(pam)
        return orderBaseList2(pam)
      } catch (err) {
      } finally {
        this.$nextTick(() => {
          this.getOrderBaseStatusCount({ orderSmallType: pam.orderSmallType || '' })
        })
      }
    },

    expandChange(row) {
      if (!row.expandData) {
        orderBaseSettleNormDetail2({
          id: row.id,
          orderProductId: row.pgOrderProductId
        }).then(res => {
          this.zhuzhuang(res.data).then(res => {
            row.expandData = res
          })
        })
      }
    },

    columnExpand(h, { row, column, index }) {
      var expandDataForm = row.expandData
      var rowZhu = row
      return expandDataForm ? (
        <div style="box-sizing: border-box;padding: 10px 10px 0px 50px;">
          <zj-form-container
            form-data={expandDataForm}
            formAttributes={{ 'label-position': 'top' }}
            styleSwitch={false}
          >
            <zj-form-module
              title="费用结算"
              showHade={false}
              form-data={expandDataForm}
              form-items={[
                {
                  md: 24,
                  name: 'slot-component',
                  formItemAttributes: {
                    label: '费用结算',
                    prop: 'normList'
                  },
                  render: (h, { props, onInput }) => {
                    var { formData } = props
                    var columns = [
                      {
                        columnAttributes: {
                          label: '工单类型',
                          prop: 'typeName'
                        }
                      },
                      {
                        columnAttributes: {
                          label: '结算工单来源',
                          prop: 'channelId',
                          width: '110'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item
                                label=""
                                label-width="0px"
                                prop={`normList.${index}.${column.columnAttributes.prop}`}
                                rules={required}
                              >
                                <el-select
                                  disabled={expandDataForm.normListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  onChange={val => {
                                    row.parentCategoryId = ''
                                    row.parentCategoryName = ''
                                    row.categoryId = ''
                                    row.categoryName = ''
                                    row.label = ''
                                    row.normAmount = ''
                                    row.repairAmount = ''
                                    row.settleNormId = ''
                                    if (val) {
                                      var data = this.classifyListChuLi(
                                        expandDataForm.classifyList,
                                        'orderChannelId',
                                        'orderChannelText'
                                      ).find(item => item.value == val)
                                      row.channelText = data.label
                                    } else {
                                      row.channelText = ''
                                    }
                                  }}
                                  placeholder="请选择"
                                >
                                  {this.classifyListChuLi(
                                    expandDataForm.classifyList,
                                    'orderChannelId',
                                    'orderChannelText'
                                  ).map((item, index_) => (
                                    <el-option key={index_} label={item.label} value={item.value}></el-option>
                                  ))}
                                </el-select>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '结算产品大类',
                          prop: 'parentCategoryId',
                          width: '110'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item
                                label=""
                                label-width="0px"
                                prop={`normList.${index}.${column.columnAttributes.prop}`}
                                rules={required}
                              >
                                <el-select
                                  disabled={expandDataForm.normListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  onChange={val => {
                                    row.categoryId = ''
                                    row.categoryName = ''
                                    row.label = ''
                                    row.normAmount = ''
                                    row.repairAmount = ''
                                    row.settleNormId = ''
                                    if (val) {
                                      var data = this.classifyListChuLi(
                                        expandDataForm.classifyList,
                                        'parentCategoryId',
                                        'parentCategoryName',
                                        item => item.orderChannelId == row.channelId
                                      ).find(item => item.value == val)
                                      row.parentCategoryName = data.label
                                    } else {
                                      row.parentCategoryName = ''
                                    }
                                  }}
                                  placeholder="请选择"
                                >
                                  {this.classifyListChuLi(
                                    expandDataForm.classifyList,
                                    'parentCategoryId',
                                    'parentCategoryName',
                                    item => item.orderChannelId == row.channelId
                                  ).map((item, index_) => (
                                    <el-option key={index_} label={item.label} value={item.value}></el-option>
                                  ))}
                                </el-select>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '结算产品小类',
                          prop: 'categoryId',
                          width: '110'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item
                                label=""
                                label-width="0px"
                                prop={`normList.${index}.${column.columnAttributes.prop}`}
                                rules={required}
                              >
                                <el-select
                                  disabled={expandDataForm.normListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  onChange={val => {
                                    row.label = ''
                                    row.normAmount = ''
                                    row.repairAmount = ''
                                    row.settleNormId = ''
                                    if (val) {
                                      var data = this.classifyListChuLi(
                                        expandDataForm.classifyList,
                                        'categoryId',
                                        'categoryName',
                                        item =>
                                          item.orderChannelId == row.channelId &&
                                          item.parentCategoryId == row.parentCategoryId
                                      ).find(item => item.value == val)
                                      row.categoryName = data.label
                                    } else {
                                      row.categoryName = ''
                                    }
                                  }}
                                  placeholder="请选择"
                                >
                                  {this.classifyListChuLi(
                                    expandDataForm.classifyList,
                                    'categoryId',
                                    'categoryName',
                                    item =>
                                      item.orderChannelId == row.channelId &&
                                      item.parentCategoryId == row.parentCategoryId
                                  ).map((item, index_) => (
                                    <el-option key={index_} label={item.label} value={item.value}></el-option>
                                  ))}
                                </el-select>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      ...(() => {
                        if (expandDataForm.orderType == 'INSTALL') {
                          return [
                            {
                              columnAttributes: {
                                label: '结算功率',
                                prop: 'label'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">
                                    <el-form-item
                                      label=""
                                      label-width="0px"
                                      prop={`normList.${index}.${column.columnAttributes.prop}`}
                                      rules={required}
                                    >
                                      <el-select
                                        disabled={expandDataForm.normListBj || !row.bianji}
                                        value={row[column.columnAttributes.prop]}
                                        onInput={val => {
                                          row[column.columnAttributes.prop] = val
                                        }}
                                        onChange={val => {
                                          if (val) {
                                            var data = this.getflList(expandDataForm.gonglv, row).find(
                                              item => item.label == val
                                            )
                                            row.normAmount = data.normAmount
                                            row.settleNormId = data.id
                                          } else {
                                            row.normAmount = ''
                                            row.settleNormId = ''
                                          }
                                        }}
                                        placeholder="请选择"
                                      >
                                        {this.getflList(expandDataForm.gonglv, row).map((item, index_) => (
                                          <el-option key={index_} label={item.label} value={item.label}></el-option>
                                        ))}
                                      </el-select>
                                    </el-form-item>
                                  </div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '结算标准(元/套)',
                                prop: 'normAmount',
                                width: '160px'
                              }
                            },
                            {
                              columnAttributes: {
                                label: '结算数量',
                                prop: 'settleNum',
                                width: '160px'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">
                                    <el-form-item
                                      label=""
                                      label-width="0px"
                                      prop={`normList.${index}.${column.columnAttributes.prop}`}
                                      rules={required}
                                    >
                                      <el-input
                                        type="number"
                                        disabled={expandDataForm.normListBj || !row.bianji}
                                        value={row[column.columnAttributes.prop]}
                                        onInput={val => {
                                          row[column.columnAttributes.prop] = val
                                        }}
                                        placeholder="请输入内容"
                                      ></el-input>
                                    </el-form-item>
                                  </div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '结算金额(元)',
                                prop: 'settleAmount',
                                width: '160px'
                              },
                              render: (h, { row, column, index }) => {
                                row.settleAmount = (
                                  (Number(row.normAmount || 0) * Number(row.settleNum || 0) * 100) /
                                  100
                                ).toFixed(2)
                                return this.panfuan(row) ? <div class="redbordererr">{row.settleAmount}</div> : null
                              }
                            }
                          ]
                        }
                        return []
                      })(),
                      ...(() => {
                        if (expandDataForm.orderType == 'REPAIR') {
                          return [
                            {
                              columnAttributes: {
                                label: '质保类型',
                                prop: 'label'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">
                                    <el-form-item
                                      label=""
                                      label-width="0px"
                                      prop={`normList.${index}.${column.columnAttributes.prop}`}
                                      rules={required}
                                    >
                                      <el-select
                                        disabled={expandDataForm.normListBj || !row.bianji}
                                        value={row[column.columnAttributes.prop]}
                                        onInput={val => {
                                          row[column.columnAttributes.prop] = val
                                        }}
                                        onChange={val => {
                                          if (val) {
                                            var data = this.getflList(expandDataForm.gonglr, row).find(
                                              item => item.label == val
                                            )
                                            row.repairAmount = data.repairAmount
                                            row.normAmount = data.normAmount
                                            row.settleNormId = data.id
                                          } else {
                                            row.repairAmount = ''
                                            row.normAmount = ''
                                            row.settleNormId = ''
                                          }
                                        }}
                                        placeholder="请选择"
                                      >
                                        {this.getflList(expandDataForm.gonglr, row).map((item, index_) => (
                                          <el-option
                                            key={index_}
                                            label={{ INSIDE: '保内', OUT: '保外' }[item.label]}
                                            value={item.label}
                                          ></el-option>
                                        ))}
                                      </el-select>
                                    </el-form-item>
                                  </div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '上门费（元/单）',
                                prop: 'repairAmount',
                                width: '110'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">
                                    <el-form-item
                                      label=""
                                      label-width="0px"
                                      prop={`normList.${index}.${column.columnAttributes.prop}`}
                                      rules={required}
                                    >
                                      <el-input
                                        disabled={expandDataForm.normListBj || !row.bianji}
                                        type="number"
                                        value={row[column.columnAttributes.prop]}
                                        onInput={val => {
                                          row[column.columnAttributes.prop] = val
                                        }}
                                        placeholder="请输入内容"
                                      ></el-input>
                                    </el-form-item>
                                  </div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '维修费结算比例%',
                                prop: 'normAmount',
                                width: '110'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">{row[column.columnAttributes.prop]}%</div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '维修费用结算',
                                prop: 'repairNormAmount',
                                width: '110'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">
                                    <el-form-item
                                      label=""
                                      label-width="0px"
                                      prop={`normList.${index}.${column.columnAttributes.prop}`}
                                      rules={required}
                                    >
                                      <el-input
                                        disabled={expandDataForm.normListBj || !row.bianji}
                                        type="number"
                                        value={row[column.columnAttributes.prop]}
                                        onInput={val => {
                                          row[column.columnAttributes.prop] = val
                                        }}
                                        placeholder="请输入内容"
                                      ></el-input>
                                    </el-form-item>
                                  </div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '结算数量',
                                prop: 'settleNum',
                                width: '160px'
                              },
                              render: (h, { row, column, index }) => {
                                return this.panfuan(row) ? (
                                  <div class="redbordererr">
                                    <el-form-item
                                      label=""
                                      label-width="0px"
                                      prop={`normList.${index}.${column.columnAttributes.prop}`}
                                      rules={required}
                                    >
                                      <el-input
                                        type="number"
                                        disabled={expandDataForm.normListBj || !row.bianji}
                                        value={row[column.columnAttributes.prop]}
                                        onInput={val => {
                                          row[column.columnAttributes.prop] = val
                                        }}
                                        placeholder="请输入内容"
                                      ></el-input>
                                    </el-form-item>
                                  </div>
                                ) : null
                              }
                            },
                            {
                              columnAttributes: {
                                label: '结算金额(元)',
                                prop: 'settleAmount',
                                width: '160px'
                              },
                              render: (h, { row, column, index }) => {
                                row.settleAmount = (
                                  ((Number(row.repairAmount || 0) +
                                    (Number(row.normAmount || 0) / 100) *
                                      Number(row.repairNormAmount || 0) *
                                      Number(row.settleNum || 0)) *
                                    100) /
                                  100
                                ).toFixed(2)
                                return this.panfuan(row) ? <div class="redbordererr">{row.settleAmount}</div> : null
                              }
                            }
                          ]
                        }
                        return []
                      })(),
                      {
                        columnAttributes: {
                          label: '备注',
                          prop: 'remark',
                          width: '200px'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item label="" label-width="0px">
                                <el-input
                                  disabled={expandDataForm.normListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  placeholder="请输入内容"
                                ></el-input>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '工程师名称',
                          prop: 'workerName',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '联系电话',
                          prop: 'workerMobile',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '结算费用',
                          prop: 'settleAmount',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr">
                              <el-form-item
                                label=""
                                label-width="0px"
                                prop={`normList.${row.pfuqinid}.workerList.${row.zijideid}.${column.columnAttributes.prop}`}
                                rules={required}
                              >
                                <el-input
                                  type="number"
                                  disabled={
                                    !expandDataForm.normList[row.pfuqinid].bianji ||
                                    expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                      'poolStatus'
                                    ] == 'YES'
                                  }
                                  value={
                                    expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                      column.columnAttributes.prop
                                    ]
                                  }
                                  onInput={val => {
                                    expandDataForm.normList[row.pfuqinid].workerList[row.zijideid][
                                      column.columnAttributes.prop
                                    ] = val
                                  }}
                                  placeholder="请输入内容"
                                ></el-input>
                              </el-form-item>
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '确认人',
                          prop: 'confirmBy',
                          width: '100px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '确认时间',
                          prop: 'confirmTime',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '汇总状态',
                          prop: 'poolStatus',
                          width: '100px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                { NO: '未汇总', YES: '已汇总' }[
                                  expandDataForm?.normList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                    column.columnAttributes.prop
                                  ]
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '操作',
                          prop: '',
                          fixed: 'right',
                          width: '120px'
                        },
                        render: (h, { row, column, index }) => {
                          return expandDataForm.settleStatus == 'YWG' ||
                            (expandDataForm.settleStatus == 'YJS' &&
                              expandDataForm?.normList[row.pfuqinid]?.workerList?.find(
                                item => item.poolStatus == 'NO'
                              )) ? (
                            <div class="redbordererr redbordererranniuright" style="padding:0 6px;">
                              {expandDataForm.normList[row.pfuqinid].bianji ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    var yjsje = [
                                      0,
                                      0,
                                      0,
                                      expandDataForm?.normList[row.pfuqinid]?.workerList
                                        ?.filter(item => item.poolStatus == 'YES')
                                        .map(item => item.settleAmount)
                                    ].reduce(function (prev, curr, idx, arr) {
                                      return prev + curr
                                    })
                                    var nou = expandDataForm?.normList[row.pfuqinid]?.workerList?.filter(
                                      item => item.poolStatus == 'NO'
                                    )
                                    var qian = (
                                      (((Number(expandDataForm?.normList[row.pfuqinid]['settleAmount']) - yjsje) /
                                        nou.length) *
                                        100) /
                                      100
                                    ).toFixed(2)
                                    var rongyu = (
                                      Number(expandDataForm?.normList[row.pfuqinid]['settleAmount']) -
                                      yjsje -
                                      qian * nou.length
                                    ).toFixed(2)
                                    nou.map((item, index) => {
                                      if (index === 0) {
                                        item.settleAmount = (Number(qian) + Number(rongyu)).toFixed(2)
                                      } else {
                                        item.settleAmount = qian
                                      }
                                    })
                                  }}
                                >
                                  平分费用
                                </el-button>
                              ) : null}
                              {expandDataForm.normList[row.pfuqinid].bianji ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    this.itemOrderBaseSettleNormReset(rowZhu, 'normList', () => {
                                      expandDataForm.normList[row.pfuqinid].bianji = false
                                    })
                                  }}
                                >
                                  确定
                                </el-button>
                              ) : null}
                              {!expandDataForm.normList[row.pfuqinid].bianji ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    expandDataForm.normList[row.pfuqinid].bianji = true
                                  }}
                                >
                                  修改
                                </el-button>
                              ) : null}
                              {!expandDataForm.showOtherNormList ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    expandDataForm.showOtherNormList = true
                                  }}
                                >
                                  添加其他费用
                                </el-button>
                              ) : null}
                            </div>
                          ) : null
                        }
                      }
                    ]
                    return (
                      <zj-table
                        columns={columns}
                        table-data={formData.normList}
                        tableAttributes={{
                          'span-method': ({ row, column, rowIndex, columnIndex }) => {
                            if (columnIndex === columns.length - 1) {
                              if (row.zijideid == 0) {
                                return {
                                  rowspan: formData.normList[row.pfuqinid].workerList?.length,
                                  colspan: 1
                                }
                              } else {
                                return {
                                  rowspan: 0,
                                  colspan: 0
                                }
                              }
                            }
                          }
                        }}
                      />
                    )
                  }
                },
                {
                  md: 24,
                  isShow: expandDataForm.showOtherNormList,
                  name: 'slot-component',
                  formItemAttributes: {
                    label: '其他费用结算',
                    prop: 'otherNormList'
                  },
                  render: (h, { props, onInput }) => {
                    var { formData } = props
                    var columns = [
                      {
                        columnAttributes: {
                          label: '结算工单来源',
                          prop: 'channelId',
                          width: '110'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item
                                label=""
                                label-width="0px"
                                prop={`normList.${index}.${column.columnAttributes.prop}`}
                                rules={required}
                              >
                                <el-select
                                  disabled={expandDataForm.otherNormListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  onChange={val => {
                                    row.type = ''
                                    row.label = ''
                                    row.normAmount = ''
                                    row.settleNormId = ''
                                    row.settleNum = ''
                                    if (val) {
                                      var data = this.classifyListChuLi(
                                        expandDataForm.otherList,
                                        'orderChannelId',
                                        'orderChannelText'
                                      ).find(item => item.value == val)
                                      row.channelText = data.label
                                    } else {
                                      row.channelText = ''
                                    }
                                  }}
                                  placeholder="请选择"
                                >
                                  {this.classifyListChuLi(
                                    expandDataForm.otherList,
                                    'orderChannelId',
                                    'orderChannelText'
                                  ).map((item, index_) => (
                                    <el-option key={index_} label={item.label} value={item.value}></el-option>
                                  ))}
                                </el-select>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '费用类型',
                          prop: 'type'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item label="" label-width="0px">
                                <el-select
                                  disabled={expandDataForm.otherNormListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  onChange={val => {
                                    row.label = ''
                                    row.normAmount = ''
                                    row.settleNormId = ''
                                    row.settleNum = ''
                                  }}
                                  placeholder="请选择"
                                >
                                  {this.classifyListChuLi(
                                    expandDataForm.otherList,
                                    'type',
                                    'typeName',
                                    item => item.orderChannelId == row.channelId
                                  ).map((item, index_) => (
                                    <el-option key={index_} label={item.label} value={item.value}></el-option>
                                  ))}
                                </el-select>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '规格',
                          prop: 'label'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item label="" label-width="0px">
                                <el-select
                                  disabled={expandDataForm.otherNormListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  onChange={val => {
                                    row.settleNum = ''
                                    if (val) {
                                      var data = this.classifyListChuLi(
                                        expandDataForm.otherList,
                                        'label',
                                        'label',
                                        item => {
                                          return item.orderChannelId == row.channelId && item.type == row.type
                                        },
                                        'normAmount',
                                        'id'
                                      ).find(item => item.label == val)
                                      row.normAmount = data.normAmount
                                      row.settleNormId = data.id
                                    } else {
                                      row.normAmount = ''
                                      row.settleNormId = ''
                                    }
                                  }}
                                  placeholder="请选择"
                                >
                                  {this.classifyListChuLi(
                                    expandDataForm.otherList,
                                    'label',
                                    'label',
                                    item => item.orderChannelId == row.channelId && item.type == row.type
                                  ).map((item, index_) => (
                                    <el-option key={index_} label={item.label} value={item.value}></el-option>
                                  ))}
                                </el-select>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '单价(元/套)',
                          prop: 'normAmount',
                          width: '160px'
                        }
                      },
                      {
                        columnAttributes: {
                          label: '数量',
                          prop: 'settleNum',
                          width: '160px'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item label="" label-width="0px">
                                <el-input
                                  type="number"
                                  disabled={expandDataForm.otherNormListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  placeholder="请输入内容"
                                ></el-input>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '结算金额(元)',
                          prop: 'settleAmount',
                          width: '160px'
                        },
                        render: (h, { row, column, index }) => {
                          row.settleAmount = (
                            (Number(row.normAmount || 0) * Number(row.settleNum || 0) * 100) /
                            100
                          ).toFixed(2)
                          return this.panfuan(row) ? <div class="redbordererr">{row.settleAmount}</div> : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '备注',
                          prop: 'remark',
                          width: '200px'
                        },
                        render: (h, { row, column, index }) => {
                          return this.panfuan(row) ? (
                            <div class="redbordererr">
                              <el-form-item label="" label-width="0px">
                                <el-input
                                  disabled={expandDataForm.otherNormListBj || !row.bianji}
                                  value={row[column.columnAttributes.prop]}
                                  onInput={val => {
                                    row[column.columnAttributes.prop] = val
                                  }}
                                  placeholder="请输入内容"
                                ></el-input>
                              </el-form-item>
                            </div>
                          ) : null
                        }
                      },
                      {
                        columnAttributes: {
                          label: '工程师名称',
                          prop: 'workerName',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '联系电话',
                          prop: 'workerMobile',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '结算费用',
                          prop: 'settleAmount',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr">
                              <el-form-item
                                label=""
                                label-width="0px"
                                prop={`otherNormList.${row.pfuqinid}.workerList.${row.zijideid}.${column.columnAttributes.prop}`}
                                rules={required}
                              >
                                <el-input
                                  type="number"
                                  disabled={
                                    !expandDataForm.otherNormList[row.pfuqinid].bianji ||
                                    expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                      'poolStatus'
                                    ] == 'YES'
                                  }
                                  value={
                                    expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                      column.columnAttributes.prop
                                    ]
                                  }
                                  onInput={val => {
                                    expandDataForm.otherNormList[row.pfuqinid].workerList[row.zijideid][
                                      column.columnAttributes.prop
                                    ] = val
                                  }}
                                  placeholder="请输入内容"
                                ></el-input>
                              </el-form-item>
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '确认人',
                          prop: 'confirmBy',
                          width: '100px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '确认时间',
                          prop: 'confirmTime',
                          width: '140px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                  column.columnAttributes.prop
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '汇总状态',
                          prop: 'poolStatus',
                          width: '100px'
                        },
                        render: (h, { row, column, index }) => {
                          return (
                            <div class="redbordererr" style="padding:0 6px;display:flex;align-items:center;">
                              {
                                { NO: '未汇总', YES: '已汇总' }[
                                  expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.[row.zijideid]?.[
                                    column.columnAttributes.prop
                                  ]
                                ]
                              }
                            </div>
                          )
                        }
                      },
                      {
                        columnAttributes: {
                          label: '操作',
                          prop: '',
                          fixed: 'right',
                          width: '120px'
                        },
                        render: (h, { row, column, index }) => {
                          return expandDataForm.settleStatus == 'YWG' ||
                            (expandDataForm.settleStatus == 'YJS' &&
                              expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.find(
                                item => item.poolStatus == 'NO'
                              )) ? (
                            <div
                              class="redbordererr redbordererranniuright"
                              style="padding:0 6px;display:flex;align-items:center;"
                            >
                              {expandDataForm.otherNormList[row.pfuqinid].bianji ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    var yjsje = [
                                      0,
                                      0,
                                      0,
                                      expandDataForm?.otherNormList[row.pfuqinid]?.workerList
                                        ?.filter(item => item.poolStatus == 'YES')
                                        .map(item => item.settleAmount)
                                    ].reduce(function (prev, curr, idx, arr) {
                                      return prev + curr
                                    })
                                    var nou = expandDataForm?.otherNormList[row.pfuqinid]?.workerList?.filter(
                                      item => item.poolStatus == 'NO'
                                    )
                                    var qian = (
                                      (((Number(expandDataForm?.otherNormList[row.pfuqinid]['settleAmount']) - yjsje) /
                                        nou.length) *
                                        100) /
                                      100
                                    ).toFixed(2)
                                    var rongyu = (
                                      Number(expandDataForm?.otherNormList[row.pfuqinid]['settleAmount']) -
                                      yjsje -
                                      qian * nou.length
                                    ).toFixed(2)
                                    nou.map((item, index) => {
                                      if (index === 0) {
                                        item.settleAmount = (Number(qian) + Number(rongyu)).toFixed(2)
                                      } else {
                                        item.settleAmount = qian
                                      }
                                    })
                                  }}
                                >
                                  平分费用
                                </el-button>
                              ) : null}
                              {expandDataForm.otherNormList[row.pfuqinid].bianji ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    this.itemOrderBaseSettleNormReset(rowZhu, 'otherNormList', () => {
                                      expandDataForm.otherNormList[row.pfuqinid].bianji = false
                                    })
                                  }}
                                >
                                  确定
                                </el-button>
                              ) : null}
                              {!expandDataForm.otherNormList[row.pfuqinid].bianji ? (
                                <el-button
                                  type="text"
                                  onClick={() => {
                                    expandDataForm.otherNormList[row.pfuqinid].bianji = true
                                  }}
                                >
                                  修改
                                </el-button>
                              ) : null}
                            </div>
                          ) : null
                        }
                      }
                    ]
                    return (
                      <zj-table
                        columns={columns}
                        table-data={formData.otherNormList}
                        tableAttributes={{
                          'span-method': ({ row, column, rowIndex, columnIndex }) => {
                            if (columnIndex === columns.length - 1) {
                              if (row.zijideid == 0) {
                                return {
                                  rowspan: formData.otherNormList[row.pfuqinid].workerList?.length,
                                  colspan: 1
                                }
                              } else {
                                return {
                                  rowspan: 0,
                                  colspan: 0
                                }
                              }
                            }
                          }
                        }}
                      />
                    )
                  }
                }
              ]}
            />
          </zj-form-container>
        </div>
      ) : null
    },

    itemOrderBaseSettleNormReset(data, type, cb) {
      try {
        data.expandData.normList
          .filter(item => !!item.id)
          .map(item => {
            if (
              [0, 0, 0, ...item.workerList?.map(val => Number(val.settleAmount))].reduce(function (
                prev,
                curr,
                idx,
                arr
              ) {
                return prev + curr
              }) !== Number(item.settleAmount)
            ) {
              throw new Error(
                `费用信息-${item.channelText}-${item.parentCategoryName}-${item.categoryName}-${item.label}:合计结算总金额与总金额不等`
              )
            }
          })
        data.expandData.otherNormList
          .filter(item => !!item.id)
          .map(item => {
            if (
              [0, 0, 0, ...item.workerList?.map(val => Number(val.settleAmount))].reduce(function (
                prev,
                curr,
                idx,
                arr
              ) {
                return prev + curr
              }) !== Number(item.settleAmount)
            ) {
              throw new Error(`其他费用信息-${item.channelText}-${item.type}-${item.label}:合计结算总金额与总金额不等`)
            }
          })
      } catch (err) {
        this.$message.warning(err.message)
        return
      }

      var { classifyList, gonglr, gonglv, otherList, normListBj, otherNormListBj, showOtherNormList, ...pam } =
        data.expandData
      var pams = {
        ...pam,
        ...(() => {
          if (type == 'normList') {
            return {
              normList: pam.normList.filter(item => !!item.id)
            }
          }
          return {
            normList: []
          }
        })(),
        ...(() => {
          if (type == 'otherNormList') {
            return {
              otherNormList: pam.otherNormList.filter(item => !!item.id)
            }
          }
          return {
            otherNormList: []
          }
        })()
      }
      ;[orderBaseSettleNormConfirm, orderBaseSettleNormReset]
        [
          pams[type]?.map(item => item.workerList?.find(resdata => !!resdata.settleOrderId))?.find(item => item) ? 1 : 0
        ](pams)
        .then(res => {
          orderBaseSettleNormDetail2({
            id: data.id,
            orderProductId: data.pgOrderProductId
          }).then(res => {
            this.zhuzhuang(res.data).then(res => {
              data.expandData = res
            })
          })
          cb && cb()
          this.$message({
            type: 'success',
            message: '结算成功'
          })
        })
    },

    zhuzhuang(data) {
      return new Promise((r, j) => {
        var orderChannelId
        var normList = []
        var pfuqinid = 0
        data.normListBj = false
        data.otherNormListBj = false
        data.showOtherNormList = false
        data.normList.map((item, index) => {
          if (!orderChannelId) {
            orderChannelId = item.orderChannelId
          }
          item.settleNum = item.settleNum || ''
          normList.push({
            ...item,
            pfuqinid: pfuqinid,
            zijideid: 0,
            bianji: !item.workerList?.find(resdata => !!resdata.settleOrderId)
          })
          item.workerList?.map((resdata, ind_) => {
            if (resdata.settleOrderId) {
              data.normListBj = true
            }
            if (ind_ !== 0) {
              normList.push({ pfuqinid: pfuqinid, zijideid: ind_ })
            }
            if (ind_ == item.workerList?.length - 1) {
              pfuqinid += ind_ + 1
            }
          })
        })
        var otherNormList = []
        var qtpfuqinid = 0
        data.otherNormList.map((item, index) => {
          item.settleNum = item.settleNum || ''
          otherNormList.push({
            ...item,
            pfuqinid: qtpfuqinid,
            zijideid: 0,
            bianji: !item.workerList?.find(resdata => !!resdata.settleOrderId)
          })
          if (!data.showOtherNormList) {
            data.showOtherNormList = !!item.workerList?.find(resdata => !!resdata.settleOrderId)
          }
          item.workerList?.map((resdata, ind_) => {
            if (resdata.settleOrderId) {
              data.otherNormListBj = true
            }
            if (ind_ !== 0) {
              otherNormList.push({ pfuqinid: qtpfuqinid, zijideid: ind_ })
            }
            if (ind_ == item.workerList?.length - 1) {
              qtpfuqinid += ind_ + 1
            }
          })
        })
        Promise.all([
          listPageV3({ pageNum: 1, pageSize: -1 }),
          listPageV2Repair({ pageNum: 1, pageSize: -1 }),
          settleNormInstallQuery({
            settleNormType: data.orderType,
            type: data.orderSmallType
          }),
          settleNormOtherQuery()
        ])
          .then(([res1, res2, res3, res4]) => {
            r({
              ...data,
              normList,
              otherNormList,
              gonglv: res1.data.records,
              gonglr: res2.data.records,
              classifyList: res3.data,
              otherList: res4.data
            })
          })
          .catch(j)
      })
    },

    panfuan(item) {
      if (!!item.id) {
        return true
      } else {
        return false
      }
    },

    classifyListChuLi(list, value, label, func, ...keys) {
      var obj = {}
      list.map(item => {
        if (func && !func(item)) {
          return
        }
        obj[item[value]] = {
          label: item[label]
        }
        for (var key of keys) {
          obj[item[value]][key] = item[key]
        }
      })
      return Object.keys(obj).map(value => {
        return {
          value: value,
          ...obj[value]
        }
      })
    },

    getflList(list = [], row = {}) {
      return list.filter(
        item =>
          item.type == row.type && item.parentCategoryId == row.parentCategoryId && item.categoryId == row.categoryId
      )
    }
  }
}
</script>

<style lang="scss">
.redbordererranniuright {
  .el-button {
    margin-left: 0px;
    margin-right: 10px;
  }
}
</style>
