<template>
  <div class="tenant">
    <byTable
      :hideTable="true"
      :hidePagination="true"
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      highlight-current-row
      @get-list="getList">
    </byTable>
    <div style="padding: 0 20px 20px 20px; background-color: white">
      <el-table v-loading="loading" :data="sourceList.data">
        <el-table-column label="合同编号" prop="code" width="160" fixed />
        <el-table-column label="客户名称" prop="customerName" min-width="200" fixed />
        <el-table-column label="业务员" prop="userName" width="140" fixed />
        <el-table-column label="合同金额" width="140">
          <template #default="{ row }">
            <div>{{ row.amount }}</div>
          </template>
        </el-table-column>
        <el-table-column label="收入">
          <el-table-column label="到账金额" prop="sumClaimMoney" width="140" />
          <el-table-column label="退税金额" prop="refundableAmount" width="140" />
          <el-table-column label="其他收入" prop="otherIncomeAmount" width="140" />
        </el-table-column>
        <el-table-column label="采购合同金额" prop="sumPurchaseContractMoney" width="140" />
        <el-table-column label="支出">
          <el-table-column label="已付货款" prop="accountPaid" width="140" />
          <el-table-column label="代理费" prop="agencyFee" width="140" />
          <el-table-column label="拖车费" prop="trailerFee" width="140" />
          <el-table-column label="报关费" prop="customsFee" width="140" />
          <el-table-column label="港杂费" prop="portMixedFee" width="140" />
          <el-table-column label="验货红包" prop="inspectionRedPack" width="140" />
          <el-table-column label="佣金" prop="commission" width="140" />
          <el-table-column label="检测费" prop="checkout" width="140" />
          <el-table-column label="验货费" prop="inspectionCharge" width="140" />
          <el-table-column label="运费" prop="freight" width="140" />
          <el-table-column label="产地证费" prop="certificateOfOrigin" width="140" />
          <el-table-column label="其他" prop="other" width="140" />
        </el-table-column>
        <el-table-column label="备注" prop="remark" width="200" />
        <el-table-column label="统计">
          <el-table-column label="收入合计" prop="incomeAmount" width="140" />
          <el-table-column label="支出合计" prop="expenditureAmount" width="140" />
          <el-table-column label="毛利" prop="gross" width="140" />
          <el-table-column label="毛利率" width="140">
            <template #default="{ row }">
              <div>{{ row.grossRate }}%</div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="{ row }">
            <div>
              <el-button type="primary" @click="changeAgencyFee(row)" link>代理费</el-button>
              <el-button type="primary" @click="changeRemark(row)" link>备注</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="padding: 20px" justify="end" type="flex">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="sourceList.pagination.pageNum"
          :page-size="sourceList.pagination.pageSize"
          :total="sourceList.pagination.total"
          @size-change="handleSizeChange"
          @current-change="handlePageChange" />
      </el-row>
    </div>

    <el-dialog title="代理费" v-if="openAgencyFee" v-model="openAgencyFee" width="400">
      <byForm :formConfig="formAgencyFeeConfig" :formOption="formOption" v-model="formAgencyFeeData.data" ref="agencyFee">
        <template #agencyFee>
          <div style="width: 100%">
            <el-form-item label="代理费" prop="agencyFee">
              <el-input-number onmousewheel="return false;" v-model="formAgencyFeeData.data.agencyFee" :precision="2" :controls="false" :min="0" />
            </el-form-item>
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="openAgencyFee = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitAgencyFeeForm()" size="large">确 定</el-button>
      </template>
    </el-dialog>

    <el-dialog title="备注" v-if="openRemark" v-model="openRemark" width="400">
      <byForm :formConfig="formRemarkConfig" :formOption="formOption" v-model="formRemarkData.data" ref="remark">
        <template #remark>
          <div style="width: 100%">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formRemarkData.data.remark" :rows="4" type="textarea" />
            </el-form-item>
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="openRemark = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitRemarkForm()" size="large">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import useUserStore from "@/store/modules/user";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const accountCurrency = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    userId: "",
  },
});
const loading = ref(false);
const config = computed(() => {
  return [];
});
const getDict = () => {
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "account_currency",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        accountCurrency.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/contract/getProfitClearingPage", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();
const handleSizeChange = (val) => {
  sourceList.value.pagination.pageNum = 1;
  sourceList.value.pagination.pageSize = val;
  getList();
};
const handlePageChange = (val) => {
  sourceList.value.pagination.pageNum = val;
  getList();
};
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const openAgencyFee = ref(false);
const agencyFee = ref(null);
const formAgencyFeeData = reactive({
  data: {},
});
const formAgencyFeeConfig = computed(() => {
  return [
    {
      type: "slot",
      slotName: "agencyFee",
    },
  ];
});
const changeAgencyFee = (row) => {
  formAgencyFeeData.data = {
    id: row.id,
    agencyFee: row.agencyFee,
  };
  openAgencyFee.value = true;
};
const submitAgencyFeeForm = () => {
  proxy.post("/contract/edit", formAgencyFeeData.data).then(() => {
    ElMessage({
      message: "保存成功",
      type: "success",
    });
    openAgencyFee.value = false;
    getList();
  });
};
const openRemark = ref(false);
const remark = ref(null);
const formRemarkData = reactive({
  data: {},
});
const formRemarkConfig = computed(() => {
  return [
    {
      type: "slot",
      slotName: "remark",
    },
  ];
});
const changeRemark = (row) => {
  formRemarkData.data = {
    id: row.id,
    remark: row.remark,
  };
  openRemark.value = true;
};
const submitRemarkForm = () => {
  proxy.post("/contract/edit", formRemarkData.data).then(() => {
    ElMessage({
      message: "保存成功",
      type: "success",
    });
    openRemark.value = false;
    getList();
  });
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
